3 Replies - 3726 Views - Last Post: 04 March 2009 - 05:58 AM Rate Topic: -----

#1 Damerios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-March 09

Problems with Float in a While Loop

Posted 04 March 2009 - 05:24 AM

Hi, I'm pretty much at my wit's end. I've been trying to get this simple loop to work for a couple hours to no avail.

float answer;
cin>>answer;

while(answer<=1 || answer>=1000){

cout<<"Sorry, that's an incorrect number. Please enter another one."<<endl;
cin>>answer;
cin.get(); //eats the delimiter
}

cout<<answer;


Instead of looping until a correct number is added, it will loop until a correct number is entered but then spit out the first number that was entered anyway, even if it was incorrect. How can I fix this?

This post has been edited by Damerios: 04 March 2009 - 05:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problems with Float in a While Loop

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Problems with Float in a While Loop

Posted 04 March 2009 - 05:41 AM

View PostDamerios, on 4 Mar, 2009 - 11:24 AM, said:

Hi, I'm pretty much at my wit's end. I've been trying to get this simple loop to work for a couple hours to no avail.

cin>>float;

while(float<=1 || float>=1000){

cout<<"Sorry, that's an incorrect number. Please enter another one."<<endl;
cin>>float;
cin.get(); //eats the delimiter
}

cout<<float;


Instead of looping until a correct number is added, it will loop until a correct number is entered but then spit out the first number that was entered anyway, even if it was incorrect. How can I fix this?

your code looks OK apart from using the identifer float which is a C++ keyword and should not be used by the programmer, see
http://www.cpprefere.../keywords/start
Was This Post Helpful? 0
  • +
  • -

#3 Damerios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-March 09

Re: Problems with Float in a While Loop

Posted 04 March 2009 - 05:47 AM

View Posthorace, on 4 Mar, 2009 - 04:41 AM, said:

View PostDamerios, on 4 Mar, 2009 - 11:24 AM, said:

Hi, I'm pretty much at my wit's end. I've been trying to get this simple loop to work for a couple hours to no avail.

cin>>float;

while(float<=1 || float>=1000){

cout<<"Sorry, that's an incorrect number. Please enter another one."<<endl;
cin>>float;
cin.get(); //eats the delimiter
}

cout<<float;


Instead of looping until a correct number is added, it will loop until a correct number is entered but then spit out the first number that was entered anyway, even if it was incorrect. How can I fix this?

your code looks OK apart from using the identifer float which is a C++ keyword and should not be used by the programmer, see
http://www.cpprefere.../keywords/start


Sorry, I didn't actually use float in my program, I took a snippet from my program and used float instead of my variable name without thinking. I replaced it with its actual variable name, which doesn't work either.

I edited my original post to reflect the correct variable name.

This post has been edited by Damerios: 04 March 2009 - 05:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Problems with Float in a While Loop

Posted 04 March 2009 - 05:58 AM

View PostDamerios, on 4 Mar, 2009 - 11:47 AM, said:

Sorry, I didn't actually use float in my program, I took a snippet from my program and used float instead of my variable name without thinking. I replaced it with its actual variable name, which doesn't work either.

I edited my original post to reflect the correct variable name.

you code works fine using gcc a run gave
3000
Sorry, that's an incorrect number. Please enter another one.
-20
Sorry, that's an incorrect number. Please enter another one.
1200
Sorry, that's an incorrect number. Please enter another one.
45


what compiler are you using? could you post your original code?

This post has been edited by horace: 04 March 2009 - 05:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1