double split(double e, double f)
{
return e/f;
}
int main()
{
begin:
clrscr();
double z;
cout<<"Please select the operation you would like to perform. ";
cout<<"For multiplying press 1, ";
cout<<"for adding press 2, for substracting press 3, ";
cout<<"for deviding press 4 and then hit enter. Your choice is: ";
cin>>z;
if (z==1)
{
clrscr();
double mult(double x, double y);
double x, y;
cout<<"Please enter two numbers to be multiplied, using a space between them: ";
cin>>x>>y;
cout<<"The product of your two numbers is "<<mult(x, y)<<". ";
}
else if (z==2)
{
clrscr();
double plus(double a, double b);
double a, b;
cout<<"Please enter two numbers to be added, using a space between them: ";
cin>>a>>b;
cout<<"The sum of your two numbers is "<<plus(a, b)<<". ";
}
else if (z==3)
{
clrscr();
double minus(double c, double d);
double c, d;
cout<<"Please enter two numbers to be substracted, using a space between them: ";
cin>>c>>d;
cout<<"The difference is "<<minus(c, d)<<". ";
}
else if (z==4)
{
clrscr();
double split(double e, double f);
double e, f;
cout<<"Please enter two numbers to be divided, using a space between them: ";
cin>>e>>f;
cout<<"The result of your two numbers is "<<split(e, f)<<". ";
}
else
{
clrscr();
goto begin;
}
char h, y, n;
cout<<"Would you like to perform ";
cout<<"another calculation? Y/N ";
cin>>h;
if (h=='y')
{
goto begin;
}
else if (h=='n')
{
goto end;
}
end:
system ("PAUSE");
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Firstly, don't use goto. use functions or loops.
Redesigning your program with functions will make it easier to fix this sort of problem.
Hint: use switch(z) instead of the if..else..if
To stop your program crashing from invalid input try this:
if(cin.bad()) // invalid data like 'a' or 'q'
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Firstly, don't use goto. use functions or loops.
Redesigning your program with functions will make it easier to fix this sort of problem.
Hint: use switch(z) instead of the if..else..if
To stop your program crashing from invalid input try this:
if(cin.bad()) // invalid data like 'a' or 'q'
{
cin.clear(); // clear the stream
}
hope this helps :)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2002-03-23
is cin.clear(); similar to cin.ignore(80,'\n');?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi! How can I make the following program not to crash when a letter (ex. a or g) is pressed by user? (it's in C++)
#include <iostream.h>
#include <stdlib.h>
#include <conio.c>
double mult(double x, double y)
{
return x*y;
}
double plus(double a, double b)
{
return a+b;
}
double minus(double c, double d)
{
return c-d;
}
double split(double e, double f)
{
return e/f;
}
int main()
{
begin:
clrscr();
double z;
cout<<"Please select the operation you would like to perform. ";
cout<<"For multiplying press 1, ";
cout<<"for adding press 2, for substracting press 3, ";
cout<<"for deviding press 4 and then hit enter. Your choice is: ";
cin>>z;
if (z==1)
{
clrscr();
double mult(double x, double y);
double x, y;
cout<<"Please enter two numbers to be multiplied, using a space between them: ";
cin>>x>>y;
cout<<"The product of your two numbers is "<<mult(x, y)<<". ";
}
else if (z==2)
{
clrscr();
double plus(double a, double b);
double a, b;
cout<<"Please enter two numbers to be added, using a space between them: ";
cin>>a>>b;
cout<<"The sum of your two numbers is "<<plus(a, b)<<". ";
}
else if (z==3)
{
clrscr();
double minus(double c, double d);
double c, d;
cout<<"Please enter two numbers to be substracted, using a space between them: ";
cin>>c>>d;
cout<<"The difference is "<<minus(c, d)<<". ";
}
else if (z==4)
{
clrscr();
double split(double e, double f);
double e, f;
cout<<"Please enter two numbers to be divided, using a space between them: ";
cin>>e>>f;
cout<<"The result of your two numbers is "<<split(e, f)<<". ";
}
else
{
clrscr();
goto begin;
}
char h, y, n;
cout<<"Would you like to perform ";
cout<<"another calculation? Y/N ";
cin>>h;
if (h=='y')
{
goto begin;
}
else if (h=='n')
{
goto end;
}
end:
system ("PAUSE");
}
Firstly, don't use goto. use functions or loops.
Redesigning your program with functions will make it easier to fix this sort of problem.
Hint: use switch(z) instead of the if..else..if
To stop your program crashing from invalid input try this:
if(cin.bad()) // invalid data like 'a' or 'q'
Firstly, don't use goto. use functions or loops.
Redesigning your program with functions will make it easier to fix this sort of problem.
Hint: use switch(z) instead of the if..else..if
To stop your program crashing from invalid input try this:
if(cin.bad()) // invalid data like 'a' or 'q'
{
cin.clear(); // clear the stream
}
hope this helps :)
is cin.clear(); similar to cin.ignore(80,'\n');?