Problem with using do while statement in an if statement

May 21, 2012 at 4:38pm
I have written a c++ code for a calculator and it is not outputting the results I want. Here is the outline of my code for a calculator:


#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main(){

cout<<"Welcome to my newly improved calculator program"<<endl;

cout<<"Please enter some number"<<endl;
double x;
int sum =0;
int total=0;
int sub=0;
double prod=1;
double div=1;


do {
cin >>x;
sum=sum+x;


}while(x!=0);
cout<<"Your total sum is "<<sum<<endl;
cout<<"Please enter some numbers"<<sub<<endl;
cout<<"Would you like to perform another math function today? "<<endl;
char la;
cin>>la;
if(la=='q')cout<<"Thanks for using the program! ";

else
cout<<"Continue with the program! ";
do{
cin>>x;
sub=sub-x;

}while(x!=0);
cout<<"Your total difference is "<<sub<<endl;

cout<<"Would you like to perform another math function today? "<<endl;
char ba;
cin>>ba;
if(ba=='q')cout<<"Thanks for using the program! ";

cout<<"Please enter some number"<<endl;
do{
cin>> x;
prod=prod*x;
}while(x!=1);

cout<<"Your total product is "<<prod<<endl;
cout<<"Would you like to perform another math function today? "<<endl;
char ca;
cin>>ca;
if(ba=='q')cout<<"Thanks for using the program! ";






cout<<"Please enter some numbers"<<endl;
do{
cin>>x;
div=x/div;
cout<<"Would you like to perform another math function today? "<<endl;
char da;
cin>>da;
if(da=='q')cout<<"Thanks for using the program! ";


}while(x!=1);
cout<<"The ratio of your numbers are "<<div<<endl;




cout<<"Would you like to perform another mathematical operation today? y/n"<<endl;
char ch;
cin >>ch;
if(ch=='y' || ch=='Y'|| ch=='yes')
cout<<"continue with the program";
else if(ch=='n' || ch=='N' || ch=='no' || ch=='NO')
cout<<"you are done with the improved calculator program"<<endl;



return 0;
}


Here is the output I want:

Please enter some number: 8+8 + 8+....0. The Your total sum is [b]24 . Would you like to perform another math function today y/n? If you chose 'yes' ,continue with the sequence of math operations. If user chooses 'no' the program stops.


Here is my actual output:

Welcome to my newly improved calculator program! Please enter some number? : 8+8+8+0/ Your total sum is [b] 24 Would you like to continue with this program? y.
The problem is , when I choose 'yes' , it doesn't continue to the next mathematical operation which is in the do while loop. When I choose 'yes', I want the program to carry out the next mathematical operation. When I choose 'no', I want the program to stop. I want to be asked each time the program performs a mathematical operation, I want it to ask me if I want to continue the program or do I want to stop the program. How would I go about doing that?
Last edited on May 21, 2012 at 4:39pm
May 21, 2012 at 5:01pm
Remove the statement where you output the phrase "Welcome to my newly improved calculator program!" and you will be closer to the result you want!:)
May 21, 2012 at 7:56pm
Remove the statement where you output the phrase "Welcome to my newly improved calculator program!" and you will be closer to the result you want!:)


I removed the output that you recommended and here is what my code looks like now:

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main(){



cout<<"Please enter some number"<<endl;
double x;
int sum =0;
int total=0;
int sub=0;
double prod=1;
double div=1;


do {
cin >>x;
sum=sum+x;


}while(x!=0);
cout<<"Your total sum is "<<sum<<endl;
cout<<"Please enter some numbers"<<sub<<endl;
cout<<"Would you like to perform another math function today? "<<endl;
char la;
cin>>la;
if(la=='q')cout<<"Thanks for using the program! ";

else
cout<<"Continue with the program! ";
do{
cin>>x;
sub=sub-x;

}while(x!=0);
cout<<"Your total difference is "<<sub<<endl;

cout<<"Would you like to perform another math function today? "<<endl;
char ba;
cin>>ba;
if(ba=='q')cout<<"Thanks for using the program! ";

cout<<"Please enter some number"<<endl;
do{
cin>> x;
prod=prod*x;
}while(x!=1);

cout<<"Your total product is "<<prod<<endl;
cout<<"Would you like to perform another math function today? "<<endl;
char ca;
cin>>ca;
if(ba=='q')cout<<"Thanks for using the program! ";






cout<<"Please enter some numbers"<<endl;
do{
cin>>x;
div=x/div;
cout<<"Would you like to perform another math function today? "<<endl;
char da;
cin>>da;
if(da=='q')cout<<"Thanks for using the program! ";


}while(x!=1);
cout<<"The ratio of your numbers are "<<div<<endl;




cout<<"Would you like to perform another mathematical operation today? y/n"<<endl;
char ch;
cin >>ch;
if(ch=='y' || ch=='Y'|| ch=='yes')
cout<<"continue with the program";
else if(ch=='n' || ch=='N' || ch=='no' || ch=='NO')
cout<<"you are done with the improved calculator program"<<endl;



return 0;
}
That minor change in the code that you recommended did not give me the output that I wanted.
Last edited on May 21, 2012 at 7:57pm
May 21, 2012 at 8:01pm
As far as I know this code already was demonstarted in this forum.
Topic archived. No new replies allowed.