Mystery bug in simple program? - I am beginner
I am just beginning to write C++ and have come accross a bug in a simple MS-DOS program. The program is a basic calculator. when it is run and a non-numeric entry is entered into a 'float' field, the text goes haywire and keeps on repeating itself.
Please, Please, Please will somebody help me as this is my first real program. Thank You, Archie
Here is the program, created using Bloodshed Dev-C++:
Code:
#include <iostream>
using namespace std;
int main()
{
for ( float x = 0; x < 1; x + 0 ) {
float number;
cout<<"CALCULATOR\nArchie Lodge\n\nTo multiply, type '1'\nTo divide, type '2'\nTo add, type '3'\nTo subtract, type '4'\nType number and then press Enter...";
cin>> number;
cin.ignore();
if ( number == 1 ) {//MULTIPLY
float thisisanumber;
float thisisnumbertwo;
cout<<"Please enter the first number to be multiplied: ";
cin>> thisisanumber;
cin.ignore();
cout<<"Please enter the number to multiply the above number by: ";
cin>> thisisnumbertwo;
cin.ignore();
cout<<"The numbers you entered, multiplied together are: "<< thisisanumber * thisisnumbertwo<<"\n";
cout<< thisisanumber <<" times " <<thisisnumbertwo<<" is "<< thisisanumber * thisisnumbertwo<<"\n\nPlease press enter twice to perform another calculation.\n";
cin.get();
}
else if ( number == 2 ) {//DIVIDE
float thisisanumber;
float thisisnumbertwo;
cout<<"Please enter the first number to be divided: ";
cin>> thisisanumber;
cin.ignore();
cout<<"Please enter the number for the above number to be divided by: ";
cin>> thisisnumbertwo;
cin.ignore();
cout<<"The numbers you entered, divided are: "<< thisisanumber / thisisnumbertwo<<"\n";
cout<< thisisanumber <<" divided by " <<thisisnumbertwo<<" is "<< thisisanumber / thisisnumbertwo<<"\n\nPlease press enter twice to perform another calculation.\n";
cin.get();
}
else if ( number == 3 ) {//ADD
float thisisanumber;
float thisisnumbertwo;
cout<<"Please enter the first number to be added: ";
cin>> thisisanumber;
cin.ignore();
cout<<"Please enter the number to add to the above number: ";
cin>> thisisnumbertwo;
cin.ignore();
cout<<"The numbers you entered, added together are: "<< thisisanumber + thisisnumbertwo<<"\n";
cout<< thisisanumber <<" plus " <<thisisnumbertwo<<" is "<< thisisanumber + thisisnumbertwo<<"\n\nPlease press enter twice to perform another calculation.\n";
cin.get();
}
else if ( number == 4 ) {//SUBTRACT
float thisisanumber;
float thisisnumbertwo;
cout<<"Please enter the starting number: ";
cin>> thisisanumber;
cin.ignore();
cout<<"Please enter the number to be subtracted from the above number: ";
cin>> thisisnumbertwo;
cin.ignore();
cout<<"The first number you entered, minus the second number are: "<< thisisanumber - thisisnumbertwo<<"\n";
cout<< thisisanumber <<" minus " <<thisisnumbertwo<<" is "<< thisisanumber - thisisnumbertwo<<"\n\nPlease press enter twice to perform another calculation.\n";
cin.get();
}
else {//ERROR
cout<<"Please enter either 1, 2, 3 or 4. Press enter to continue.\n";
}
cin.get();
}
}
hk_mp5kpdw, where do you put the block of code?
I have reieved a reccomendation from hk_mp5kpdw to put a block of code into my program. I have fixed all of the other issues brought up. Thank You Everybody!!! :)
This is the confusing piece of code:
Quote:
Originally Posted by hk_mp5kpdw
To make this easy, it might make sense to create a function to wrap all this code:
Code:
#include <limits> //I know that this is at the top.
...
bool GotAFloat(char* msg,float& f) //But where does this go?
{
cout << msg;
if( !(cin >> f) )
{
// Clear error flags
cin.clear();
// Pass over any bad data in the stream
cin.ignore(numeric_limits<streamsize>::max(),'\n');
return false;
}
return true
}
And then instead of calling the stream extractor directly in your code whenever you want to get a float, you would instead call the function:
Code:
//Instead of following
cout<<"Please enter the first number to be multiplied: ";
cin>> thisisanumber;
cin.ignore();
// Do this instead
while( !GotAFloat("Please enter the first number to be multiplied:",thisisanumber) );
Code:
for ( float x = 0; x < 1; x + 0 ) {
An easier way to do that is:
You should probably also have an option 5 to exit the program as well that would call
break to exit the loop and the program.