Jump to content
  • Advertisement
Sign in to follow this  
Mahoutsukai

Newbie code problem!!

This topic is 5137 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why do I get an infinit loop with this code?
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
  int sells;
  double salary;
  
  cout << "Imput sells (-1 to end): ";
  cin >> sells;
  
  while ( sells != -1 ){
     salary = 200 + static_cast< double >( sells ) * 0.09;
     
     cout << setiosflags ( ios::fixed | ios::showpoint )
          << setprecision( 2 );
     cout << "salary is: " << salary << endl;
     
     cout << "Imput sells (-1 to end): ";
     cin >> sells;
  }
  
  return 0;
}

[Edited by - Mahoutsukai on September 21, 2004 1:50:32 AM]

Share this post


Link to post
Share on other sites
Advertisement
I don't know enough about coding to reply, so not sure why I am replying, but...


it is only infinite until you type -1

you should also double sells instead of int sells if salary is double. Otherwise if you type a decimal number it goes on and on and on...

Also, not sure why it keeps going if you type a letter. You need some error handling.

Share this post


Link to post
Share on other sites
Does this work any better?


#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
int sells;
double salary;

cout << "Imput sells (-1 to end): ";
char sellsTemp[10];
cin >> sellsTemp;
sells = atoi(sellsTemp);

while ( sells != -1 ){

salary = 200 + static_cast< double >( sells ) * 0.09;

cout << setiosflags ( ios::fixed | ios::showpoint )
<< setprecision( 2 );
cout << "salary is: " << salary << endl;

cout << "Imput sells (-1 to end): ";

//sellsTemp[10];
cin >> sellsTemp;
sells = atoi(sellsTemp);

}

return 0;
}




Your code worked fine, until I entered a character. Then it went into an infinite loop. Typing -1 exited fine. Perhaps the platform you are developing interprets -1 as a character... although Im only just getting to grips with C++ myself so I wouldn't take my word for it.

Share this post


Link to post
Share on other sites
change your while loop to the following:


while ( sells != -1 && !cin.fail() )


More elaborate error handling can be achieved if you want to look into cin.clear and cin.ignore.

Share this post


Link to post
Share on other sites
it wouldnt work on mine until i changed the
cout << "salary is: " << salary << endl;to
cout << "salary is: " << salary << "\n";

note: the loop is still infanint until inputting -1

Share this post


Link to post
Share on other sites
Add std::cin.ignore(std::cin.rdbuf()->in_avail()); before the std::cin in the while loop, you probably have a spare \n in the input buffer that is causing it not to block.

Share this post


Link to post
Share on other sites
as it has been mentioned, you definitely need some error handling. i also assume you dont want any negative input at all. so you could do some fancy looping while ( sells >= 0 ) [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!