| | |
reading a float to an int variable crashes me
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 7
Reputation:
Solved Threads: 0
I am teaching myself C++. I am writing a lotto program to read in dates and numbers and update a file. This code snippet:
works completely as expected when the user inputs an integer, any integer. If a float is entered, the loop becomes infinite. I attempted to correct by adding this line:
cin.clear(); right before the second attempt to cin>numberset[i] ;
Which did not help at all. I guess something is wrong with the istream, but i dont know what it is, or how to fix it. I do appreciate any help.
C++ Syntax (Toggle Plain Text)
int numberset[6]; cout<<endl<<"now, enter the 6 numbers. Hit return between each number:"; for(int i=0;i!=6;i++){ cin>>numberset[i]; while(numberset[i]<1 || numberset[i]>53){ cout<<"the number is not a valid, lotto number! please re-enter: "; cin>>numberset[i]; }
works completely as expected when the user inputs an integer, any integer. If a float is entered, the loop becomes infinite. I attempted to correct by adding this line:
cin.clear(); right before the second attempt to cin>numberset[i] ;
Which did not help at all. I guess something is wrong with the istream, but i dont know what it is, or how to fix it. I do appreciate any help.
•
•
Join Date: Feb 2008
Posts: 7
Reputation:
Solved Threads: 0
Well, I defined numberset[] as an INT becuase there are no floating point lotto numbers. I know that it is highly unlikely for a user to enter a number like 4.5 when asked to enter a lotto number, but I am trying to develop a style wherein my programs will work under all circumstances. If I define numberset[] as float, and someone enters 4.5 as a lotto number, how would my program then recognize it as invalid?
If it has to be an integer then use isdigit to check that each character of the input is a digit.
a simp,e way to do this is
this is only a suggestion, but may use it if you like it
a simp,e way to do this is
C++ Syntax (Toggle Plain Text)
char ch[2]; ch[1] = '\0'; int x = 0; const int MULTI = 10; std::cout<<"enter a number->"; while ((ch[0] = std::cin.get()) != '\n') { if (isdigit(ch[0])) { x = (x * MULTI) + strtol(ch, NULL, 10); } } std::cout<<"x->"<<x<<"\n";
this is only a suggestion, but may use it if you like it
Last edited by gerard4143; Feb 11th, 2008 at 9:52 pm.
•
•
Join Date: Feb 2008
Posts: 14
Reputation:
Solved Threads: 2
•
•
•
•
If I define numberset[] as float, and someone enters 4.5 as a lotto number, how would my program then recognize it as invalid?
c++ Syntax (Toggle Plain Text)
#include <cmath> #include <cstdlib> using std::abs; using std::floor; int numberset[6]; cout << endl << "now, enter the 6 numbers. Hit return between each number:"; for(int i=0;i!=6;i++) { for(;;) { double temp; cin >> temp; if( temp >= 1 && temp <= 53 ) { numberset[i] = abs( floor( temp ) ); break; } cin.ignore(); cout<<"the number is not a valid, lotto number! please re-enter: "; } }
Upon further thought there is a problem here. Assume the user enters an alpha string "1 25 55 AA 45 22", your program will stall b/c the alpha chars are retained in the cin buffer. You need to discard the cin buffer with cin.ignore()
Last edited by TimeFractal; Feb 11th, 2008 at 10:18 pm. Reason: cin.ignore()
•
•
Join Date: Feb 2008
Posts: 7
Reputation:
Solved Threads: 0
Well, I would not want to truncate, because if the user enters say, 4.5 I have no way of knowing if 4 is what they really want. All I want to do is report to them that their input is erroneus, and please re-enter it.
I am not really looking for a work around at this point(at least I don't think I am). I am trying to understand why the loop becomes infinite when a float is read in to any numberset[i].
Is it due to the state of Cin?
I am not really looking for a work around at this point(at least I don't think I am). I am trying to understand why the loop becomes infinite when a float is read in to any numberset[i].
Is it due to the state of Cin?
•
•
•
•
If it has to be an integer then use isdigit to check that each character of the input is a digit.
a simp,e way to do this is
C++ Syntax (Toggle Plain Text)
char ch[2]; ch[1] = '\0'; int x = 0; const int MULTI = 10; std::cout<<"enter a number->"; while ((ch[0] = std::cin.get()) != '\n') { if (isdigit(ch[0])) { x = (x * MULTI) + strtol(ch, NULL, 10); } else { std::cout<<ch[0]<<" error message\n"; //or you could throw and exception } } std::cout<<"x->"<<x<<"\n";
this is only a suggestion, but may use it if you like it
report any values that are not digits i.e not integers. Why the language allows this type of behavior (enter floats values for ints then looping forever)to keep it light wieght. i.e. if you wanted the functionality you should have to developed it into the application. That's my understanding
Please ignore all those bozos above.
The reason your loop becomes infinite is the '.' is not valid for an integer, therefore
1)
2) the next
3) the next
4) the next
etc.
This would also happen if you entered 23T65.
The easies way to handle this is to stop using
The reason your loop becomes infinite is the '.' is not valid for an integer, therefore
1)
cin reads the number up to the '.' and stops. The '.' is left in the input buffer2) the next
cin tries to read the '.' and can't so it returns an error which you never check, and leaves the '.' in the buffer3) the next
cin tries to read the '.' and can't so it returns an error which you never check, and leaves the '.' in the buffer4) the next
cin tries to read the '.' and can't so it returns an error which you never check, and leaves the '.' in the bufferetc.
This would also happen if you entered 23T65.
The easies way to handle this is to stop using
cin and use getline() to read the entire line as a string. Then you can pull off (convert to number) the integer part and actually throw out the rest of the line. The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
The easies way to handle this is to stop usingcinand usegetline()to read the entire line as a string. Then you can pull off (convert to number) the integer part and actually throw out the rest of the line.
C++ Syntax (Toggle Plain Text)
while ((ch[0] std::cin.get) != '\n') { if (isdigit(ch[0])) ....etc }
•
•
Join Date: Feb 2008
Posts: 7
Reputation:
Solved Threads: 0
I'm still working on this. I think i agree the problem is with the state of CIN. In all honesty, I have not yet worked through the code suggestions above, because, as I said, I need to understand just what is going wrong before I try to implement a new solution. Going to do some a bit more research on iostreams.
![]() |
Other Threads in the C++ Forum
- Previous Thread: text file to linked list
- Next Thread: File I/O (Reading from a Random-Access File)
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






