| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
having problem with the output
This is program i refer from the book c++ for dummies 5 edition
by Stephen Randy davis, since i am just a beginner i try to type and compile it, it runs fine, but the problem is i could not get the correct output. The output for nArgs1 is suppose to be 0x1234, but what the output display in my screen was 0x4660 which is the decimal of hexadecimal 0x1234, can anyone help me with this? Your help is much appreciated.Thank you. //BitTest- initialize two variables and output the //results of applying the ~, &, | and ^ #include<cstdio> #include<cstdlib> #include<iostream> using namespace std; int main(int nNumberofArgs, char * pszArgs[]) { //set output format to hexadecimal cout.setf(cout.hex); //initialize two arguments int nArg1; nArg1=0x1234; int nArg2; nArg2=0x00ff; //now perform each operation in turn //first the unary NOT operator; cout<<"Arg1=0x"<<nArg1<<"\n"; cout<<"Arg2=0x"<<nArg2<<"\n"; cout<<"~nArg1=0x"<<~nArg1<<"\n"; cout<<"~nArg2=0x"<<~nArg2<<"\n"; //now the binary operators cout<<"nArg1 & nArg2=0x"<<(nArg1 & nArg2)<<"\n"; cout<<"nArg1 | nArg2=0x"<<(nArg1 | nArg2)<<"\n"; cout<<"nArg1 ^ nArg2=0x"<<(nArg1 ^ nArg2)<<"\n"; //wait until user is ready before terminating program //to allow the user to see the program results system("PAUSE"); return 0; } |
|
#2
|
|||
|
|||
|
Also a dummy
I ran into the problem too with the "for Dummmies" example program. I believe it's just a minor syntax problem. I have two fixes that worked for me.
The first regards this line: cout.setf(cout.hex); This line is supposed to change the output of the program into hexidecimal, but failed to do so for me. So, the alternative is: cout.setf(ios::hex, ios::basefield); now for my guess as to why this works and the other doesn't, if someone who knows better knows better, than please correct my erronious thinking. ios means that you're referring to the iostream file that you #include at the beginning. The iostream file contains the nessisary code to display something in hexidecimal, but for some reason or another can't figure out that the (cout.hex) means that you want all variables to be displayed in hex. I think that it doesn't realize that you want to CHANGE from the default output (which is decimal). Since 4660 (base 10) == 1234 (base 16). Therefore, the solution is to call the basefield aspect of the iostream to it's attention as a parameter for the cout command. The other fix is equally simple to implement, and even simpler to explain. on the four lines that read as follows: cout << "Arg1 = 0x" << nArg1 << "\n"; cout << "Arg2 = 0x" << nArg2 << "\n"; cout << "~nArg1 = 0x" << ~nArg1 << "\n"; cout << "~Arg2 = 0x" << ~nArg2 << "\n"; simply tell the first line that you want it to read in hexidecimal, and the rest should follow suit. Like so: cout << hex << "Arg1 = 0x" << nArg1 << "\n"; cout << "Arg2 = 0x" << nArg2 << "\n"; cout << "~nArg1 = 0x" << ~nArg1 << "\n"; cout << "~Arg2 = 0x" << ~nArg2 << "\n"; Man, I sure learned a lot about output today (I started the for dummies book yesterday, no C++ experience. If anyone else can provide insight or additional fixes, I'd appreciate it) |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > having problem with the output |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|