reading a float to an int variable crashes me

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 7
Reputation: SteveDee is an unknown quantity at this point 
Solved Threads: 0
SteveDee SteveDee is offline Offline
Newbie Poster

reading a float to an int variable crashes me

 
0
  #1
Feb 11th, 2008
I am teaching myself C++. I am writing a lotto program to read in dates and numbers and update a file. This code snippet:

  1. int numberset[6];
  2.  
  3. cout<<endl<<"now, enter the 6 numbers. Hit return between each number:";
  4.  
  5. for(int i=0;i!=6;i++){
  6. cin>>numberset[i];
  7. while(numberset[i]<1 || numberset[i]>53){
  8. cout<<"the number is not a valid, lotto number! please re-enter: ";
  9. cin>>numberset[i];
  10. }

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 222
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: reading a float to an int variable crashes me

 
0
  #2
Feb 11th, 2008
why not define numerset as a float?
it will take an integer too.
"I like beating by head against the wall because it feels so good when I stop"
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 7
Reputation: SteveDee is an unknown quantity at this point 
Solved Threads: 0
SteveDee SteveDee is offline Offline
Newbie Poster

Re: reading a float to an int variable crashes me

 
0
  #3
Feb 11th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 376
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 47
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz

Re: reading a float to an int variable crashes me

 
0
  #4
Feb 11th, 2008
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
  1. char ch[2];
  2. ch[1] = '\0';
  3. int x = 0;
  4. const int MULTI = 10;
  5.  
  6. std::cout<<"enter a number->";
  7. while ((ch[0] = std::cin.get()) != '\n')
  8. {
  9. if (isdigit(ch[0]))
  10. {
  11. x = (x * MULTI) + strtol(ch, NULL, 10);
  12. }
  13. }
  14. 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 14
Reputation: TimeFractal is an unknown quantity at this point 
Solved Threads: 2
TimeFractal TimeFractal is offline Offline
Newbie Poster

Re: reading a float to an int variable crashes me

 
0
  #5
Feb 11th, 2008
Originally Posted by SteveDee View Post
If I define numberset[] as float, and someone enters 4.5 as a lotto number, how would my program then recognize it as invalid?
You can simply truncate the fractional portion of the number.
  1. #include <cmath>
  2. #include <cstdlib>
  3. using std::abs;
  4. using std::floor;
  5.  
  6. int numberset[6];
  7. cout << endl << "now, enter the 6 numbers. Hit return between each number:";
  8.  
  9. for(int i=0;i!=6;i++)
  10. {
  11. for(;;)
  12. {
  13. double temp;
  14. cin >> temp;
  15. if( temp >= 1 && temp <= 53 )
  16. {
  17. numberset[i] = abs( floor( temp ) );
  18. break;
  19. }
  20. cin.ignore();
  21. cout<<"the number is not a valid, lotto number! please re-enter: ";
  22. }
  23. }

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()
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 7
Reputation: SteveDee is an unknown quantity at this point 
Solved Threads: 0
SteveDee SteveDee is offline Offline
Newbie Poster

Re: reading a float to an int variable crashes me

 
0
  #6
Feb 12th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 376
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 47
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz

Re: reading a float to an int variable crashes me

 
0
  #7
Feb 12th, 2008
Originally Posted by gerard4143 View Post
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
  1. char ch[2];
  2. ch[1] = '\0';
  3. int x = 0;
  4. const int MULTI = 10;
  5.  
  6. std::cout<<"enter a number->";
  7. while ((ch[0] = std::cin.get()) != '\n')
  8. {
  9. if (isdigit(ch[0]))
  10. {
  11. x = (x * MULTI) + strtol(ch, NULL, 10);
  12. }
  13. else
  14. {
  15. std::cout<<ch[0]<<" error message\n";
  16. //or you could throw and exception
  17. }
  18. }
  19. std::cout<<"x->"<<x<<"\n";

this is only a suggestion, but may use it if you like it
This does report the problem value all you had to do was put an else in the if statement. Plus this will
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: reading a float to an int variable crashes me

 
0
  #8
Feb 12th, 2008
Please ignore all those bozos above.

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 buffer
2) the next cin tries to read the '.' and can't so it returns an error which you never check, and leaves the '.' in the buffer
3) the next cin tries to read the '.' and can't so it returns an error which you never check, and leaves the '.' in the buffer
4) the next cin tries to read the '.' and can't so it returns an error which you never check, and leaves the '.' in the buffer
etc.

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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 376
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 47
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Whiz

Re: reading a float to an int variable crashes me

 
0
  #9
Feb 12th, 2008
Originally Posted by WaltP View Post
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.
If you read the prevous post you see that what I posted using
  1. while ((ch[0] std::cin.get) != '\n')
  2. {
  3. if (isdigit(ch[0]))
  4. ....etc
  5. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 7
Reputation: SteveDee is an unknown quantity at this point 
Solved Threads: 0
SteveDee SteveDee is offline Offline
Newbie Poster

Re: reading a float to an int variable crashes me

 
0
  #10
Feb 12th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC