PDA

View Full Version : C++ problem



minaadel1994
07-25-2008, 11:35 PM
hi , i have got a problem with this calculator , why when i cant calculate twice.... or more , it automaticly closes help plz heres the code:

//here it starts
#include <iostream>
using namespace std;
int subs (int x, int y);
int add (int x, int y);
int divi (int x, int y);
int mult (int x, int y);
int main()
{
int x;
int y;
cout<<"please input a number then press space and the other number to calculate: \n";
cin>>x>>y;
cin.ignore();
cout<<"equals (substracted) : "<< subs (x,y)<<"\n";
cout<<"equals (added) : "<< add (x,y)<<"\n";
cout<<"equals: (divied) : "<< divi (x,y)<<"\n";
cout<<"equals:(multiplyed) : "<< mult (x,y)<<"\n";
cin.get();
}
int subs (int x, int y)
{
return x - y;
return 0;
}
int add (int x, int y)
{
return x + y;
return 0;
}
int divi (int x, int y)
{
return x / y;
return 0;
}
int mult (int x, int y)
{
return x * y;
return 0;
}
// ends here
now wots wrong?? or wot should i add to prevent it from automaticly closing itself

Moonbat
07-26-2008, 10:19 AM
You need to use a loop, such as a while() loop to keep the programming going.

Also, why are you ending all your math functions with

return 0;
If I recall correctly, that terminates the program doesn't it? If you want your program to keep going I would take off those return 0; statements. Even if those aren't a problem, I still don't understand why you are putting them there.

minaadel1994
07-27-2008, 06:19 PM
its nothing about cin.ignore(); or return 0; , its about looping , i forgot to loop around the code its suppose to be do { the code } while (*); that makes it loop and it worked peeps :) ive learned that from a professional programer but thx for helping , by the way cin.ignore(); ignores any error when u loop cuz if it doesn't the program starts redoing the code inf times unless u close it :) thx it should be :
#include <iostream>
using namespace std;
int subs (int x, int y);
int add (int x, int y);
int divi (int x, int y);
int mult (int x, int y);
int main()
{
int x;
int y;
do { //the other fix
cout<<"please input a number then press space and the other number to calculate: \n";
cin>>x>>y;
cin.ignore();
cout<<"equals (substracted) : "<< subs (x,y)<<"\n";
cout<<"equals (added) : "<< add (x,y)<<"\n";
cout<<"equals: (divied) : "<< divi (x,y)<<"\n";
cout<<"equalsmultiplyed) : "<< mult (x,y)<<"\n";
} while (*); //the fix
}
int subs (int x, int y)
{
return x - y;
return 0;
}
int add (int x, int y)
{
return x + y;
return 0;
}
int divi (int x, int y)
{
return x / y;
return 0;
}
int mult (int x, int y)
{
return x * y;
return 0;
}

Moonbat
07-27-2008, 09:41 PM
Please use code tags when posting code, thanks :)