Simple question of integer input (C++)
A class I'm in had a discussion concerning reading decimal, octal, and hexadecimal integers from the command line. Someone contends that you can read in all three formats using the following:
int x;
cin >> showbase >> x;
I didn't think this was correct, and told them that this only reads in decimal numbers, and breaks if you input a hex number. I gave the example of 0x10, which, using the above code, will result in x being assigned the value 0. I further explained that 0x10 could be properly read using something like:
int x;
cin >> hex >> x; // or cin >> setbase(16) >> x
But my professor still holds that the first example (cin >> showbase >> x) will work fine. I'm getting frustrated, as all of the example code they have provided fails like the example I gave above (0x10 being stored as 0). I've searched google for an example of this, but couldn't find anything.
So, I figured I would post here and hopefully be enlightened on the matter =)
Have him run the following program:
and have him enter hex, decimal, and oct numbers when he runs it. He should see that he's wrong then. Additionally, you may want to show him this page, and point out the section where it says:
You may also want to refresh his memory on just what std::showbase is and let him know it is for std::ostream, not std::istream.
#include <iostream>int main(){ int x; std::cin >> std::showbase >> x; std::cout << "You entered: " << x;}
and have him enter hex, decimal, and oct numbers when he runs it. He should see that he's wrong then. Additionally, you may want to show him this page, and point out the section where it says:
Quote: http://www.cplusplus.com/reference/iostream/istream/operator>>/
The manipulators which have an effect when used on standard istream objects are:Other manipulators may be applicable to standard istream objects, but have no effect.boolalpha Alphanumerical bool values (manipulator function)noboolalpha No alphanumerical bool values (manipulator function)skipws Skip whitespaces (manipulator function)noskipws Do not skip whitespaces (manipulator function)dec Use decimal base (manipulator function)hex Use hexadecimal base (manipulator function)oct Use octal base (manipulator function)ws Extract whitespaces (manipulator function)
You may also want to refresh his memory on just what std::showbase is and let him know it is for std::ostream, not std::istream.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Thanks MikeTacular.
I'm getting mighty frustrated...
Here is a post made by one of the students in my class
And this is my professor's response to this post:
And this is my reply to both of them:
Am I having some kind of reading comprehension / brain fart problem here, or is the professor giving wrong information??
I'm getting mighty frustrated...
Here is a post made by one of the students in my class
Quote:
Author: *********
Posted date: Friday, July 10, 2009 9:32:01 AM EDT
Last modified date: Friday, July 10, 2009 9:32:01 AM EDT
Total views: 23 Your views: 3
cin >> showbase >> integer;// read input
cout << endl <<"The integer value in decimal format is " << dec << showbase << integer << endl;
this is what i did for each instance and it allows hexadecimal input and produces the correct results
And this is my professor's response to this post:
Quote:
You beat me to the post. Yes, that is correct. However, the issue is how oct and dec basefield formats accept (input) and display (output) their respective base types correctly, but the hex basefield flag does not. (Though, assigning a hex value to a variable, rather than input, then displaying works as expected: i = 0x14; then, output i.)
When using the Borland compiler, and I'm assuming g++, the following code does not accept the leading 0x when entering a hexadecimal value--but dec, and oct do work as expected:
cout << "\nEnter hexadecimal value (e.g., 0x14): ";
//use showbase, rather than hex for hex values, problems with cin and leading 0x;
//that is (in Borland and g++), works with 14, but not 0x14
cin >> hex >> i;
The solution as you stated, is to use showbase, rather than the hex basefield flag:
cin >> showbase >> i;
Though, as mentioned, while VS.NET's compiler accepts the values as entered, it is one of the reasons why it is good to use various compilers to see how the same code runs (or doesn't) under different conditions.
And this is my reply to both of them:
Quote:
The cplusplus.com reference on operator>> has a section on "manipulators which have an effect when used on standard istream objects"
From above reference...
http://www.cplusplus.com/reference/iostream/istream/operator>>/
The manipulators which have an effect when used on standard istream objects are:
boolalpha Alphanumerical bool values (manipulator function)
noboolalpha No alphanumerical bool values (manipulator function)
skipws Skip whitespaces (manipulator function)
noskipws Do not skip whitespaces (manipulator function)
dec Use decimal base (manipulator function)
hex Use hexadecimal base (manipulator function)
oct Use octal base (manipulator function)
ws Extract whitespaces (manipulator function)
Other manipulators may be applicable to standard istream objects, but have no effect.
Also see this comp.lang.c++ discussion (this one too)
Also, see this output. Output is from compiling with vs2008, but same code and same results with g++ 3.4.3 (tested on program.cs.fsu.edu)
This is my output from ex 15.7: I've tested this on both VS2008 and g++ 3.4.3
First three lines of input:
int n[3];
cout << "Enter decimal: ";
cin >> dec >> n[0];
cout << "Enter octal: ";
cin >> oct >> n[1];
cout << "Enter hexadecimal: ";
cin >> hex >> n[2];
I'm not using borland, so I can't say that cin >> showbase doesn't work with that compiler, but it doesn't work with visual studio 2008 or g++ 3.4.3
This simple case (at least on vs2008 and g++ 3.4.3) shows this:
#include <iostream>
int main()
{
int x;
ctd::cout << "Enter number: ";
std::cin >> std::showbase >> x;
std::cout << "You entered: " << x;
}
I've presented my case for using hex, dec, and oct modifiers and why cin >> showbase does not work. If I'm wrong, I'm wrong, but I don't think I am.
Am I having some kind of reading comprehension / brain fart problem here, or is the professor giving wrong information??
Don't argue with your professor. Just get a good grade and move on. You aren't helping anyone. Your professor won't care, the other students won't believe you, and you won't help yourself grade-wise.
I had one class where I corrected the instructor after every lecture. I was nice about it too. I never confronted her while other students were there and just showed her in the C standard and whatnot how she was wrong. She hated me for it. I still received an A since all the tests were multiple choice... so she couldn't take BS points off.
I had one class where I corrected the instructor after every lecture. I was nice about it too. I never confronted her while other students were there and just showed her in the C standard and whatnot how she was wrong. She hated me for it. I still received an A since all the tests were multiple choice... so she couldn't take BS points off.
Quote: Original post by curtmax_0
Don't argue with your professor. Just get a good grade and move on.
Do you want a good grade, or a good job? In the real world, it's unlikely that people hiring you will ask you for a university transcript except perhaps if the position is specifically intended for new graduates and it's your first time out there.
If you actually know the material, you will get a decent grade pretty much no matter what.
Quote: You aren't helping anyone.
I'm inclined to disagree.
Quote: Your professor won't care
Maybe if your university is crap. The ones that are worth something will have professors who can admit mistakes and handle them gracefully (with humour, even). Besides, your professor is supposed to know the material. Otherwise, he has no business teaching the course.
Quote: the other students won't believe you,
If they won't believe something that can be demonstrated and supported by documentation, they have no business taking courses in the sciences.
Quote: I had one class where I corrected the instructor after every lecture. I was nice about it too. I never confronted her while other students were there and just showed her in the C standard and whatnot how she was wrong. She hated me for it. I still received an A since all the tests were multiple choice... so she couldn't take BS points off.
Guess what? Sucks for her.
Couldn't test it on Borland, so I don't know if the student's wrong, but your teacher definitely is, though that doesn't make him a bad person, just a person that is wrong.
The library documentation, GNU and MS agrees!
Now, ask your teacher how to detect a single character input on a stream [grin]
The library documentation, GNU and MS agrees!
Now, ask your teacher how to detect a single character input on a stream [grin]
It is I, the spectaculous Don Karnage! My bloodthirsty horde is on an intercept course with you. We will be shooting you and looting you in precisely... Ten minutes. Felicitations!
For the record the proper way to identify and parse decimal/hexadecimal/octal literals in C/C++ is to use strto(u)l with a base of zero.
Quote: Original post by curtmax_0
Don't argue with your professor. Just get a good grade and move on. You aren't helping anyone. Your professor won't care, the other students won't believe you, and you won't help yourself grade-wise.
God no, we don't need more students with a critical mind that are not afraid to question a professor's opinion. Especially if they take the effort to compile a comprehensive post with references, code samples and screenshots to proof their point.
[rolleyes]
I want an update on this. sirfooks, what was your teacher's response to your reply?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Professor's response:
I've been using VS 2008 and g++ 3.4.3 to compile my code, but decided to download the Borland 5.5 just out of curiosity. I tested this with the following code:
Here is the output from VS 2008, g++ 3.4.3, and Borland C++ 5.5.1:
Visual Studio 2008
g++ 3.4.3
Borland C++ 5.5.1
So, apparently this code does work with the Borland compiler... to me, this would be a reason to stay away from the Borland compiler (seeing as how, like Don Carnage said, the documentation, GNU, and MS agree on this, and Borland is off doing its own thing...). Oh well.
Quote:
Using showbase (Boland 5.5 and VS.NET 2003), and the following code works:
cin >> showbase >> i;
I've been using VS 2008 and g++ 3.4.3 to compile my code, but decided to download the Borland 5.5 just out of curiosity. I tested this with the following code:
#include <iostream>using std::cout;using std::cin;using std::endl;using std::showbase;using std::hex;using std::oct;using std::dec;int main(){ int d,o,h; cout << "Decimal: "; cin >> showbase >> d; cout << showbase; cout << "OUTPUT AS..." << endl; cout << "Hex: " << hex << d << endl; cout << "Oct: " << oct << d << endl << endl; cout << "Octal: "; cin >> showbase >> o; cout << "OUTPUT AS..." << endl; cout << "Hex: " << hex << o << endl; cout << "Dec: " << dec << o << endl << endl; cout << "Hex: "; cin >> showbase >> h; cout << "OUTPUT AS..." << endl; cout << "Dec: " << dec << h << endl; cout << "Oct: " << oct << h << endl << endl;}
Here is the output from VS 2008, g++ 3.4.3, and Borland C++ 5.5.1:
Visual Studio 2008
g++ 3.4.3
Borland C++ 5.5.1
So, apparently this code does work with the Borland compiler... to me, this would be a reason to stay away from the Borland compiler (seeing as how, like Don Carnage said, the documentation, GNU, and MS agree on this, and Borland is off doing its own thing...). Oh well.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement