I've writen a simple calculator code it is pasted below. What do I need to change in order to make it loop using a "while" statement until, the user inputs the letter ("Q" upper or lower), casuing it to exit the program instead of a function such as(/,*,-,+).. please help...

#include <iostream>
using namespace std;
int main()
{
int x,y;
char z;
char q;
cout<<"please input an expression (two numbers and a function) to be solved, when u wish to quit simply type q instead of a function into an equation"<<endl;
cin>>x>>z>>y;

switch(z)
{
case '+':
    cout<<"The sum is "<<x+y<<endl;
    break;
case '-':
    cout<<"The difference is "<<x-y<<endl;
    break;
case '*':
    cout <<"The product is " <<x*y<<endl;
    break;
case '/':
    if (y==0)
    {
        cout<<"Can't divide by 0 "<<endl;
    }
    else 
        cout<<"The quotent is " <<x/y<<endl;


cout<<endl;
    return 0;
}
}

By now if no one has told you how to use code tags, I will:

[code]

// code here

[/code]

#include <iostream>
using namespace std;
int main()
{
int x,y;
char z;
char q;
cout<<"please input an expression (two numbers and a function) to be solved, when u wish to quit simply type q instead of a function into an equation"<<endl;
cin>>x>>z>>y;

switch(z)
{
case '+':
cout<<"The sum is "<<x+y<<endl;
break;
case '-':
cout<<"The difference is "<<x-y<<endl;
break;
case '*':
cout <<"The product is " <<x*y<<endl;
break;
case '/':
if (y==0)
{
cout<<"Can't divide by 0 "<<endl;
}
else
cout<<"The quotent is " <<x/y<<endl;


cout<<endl;
return 0;
}
}

>> What do I need to change in order to make it loop using a "while" statement until, the user inputs the letter ("Q" upper or lower)


You need a program redesign. Since the first thing the user enters can be 'Q' or 'q', this isn't going to work:

cin>>x>>z>>y;

Because you have x as an int and a 'q' can't be read into an integer variable with the >> operator.

So you need to read something into a variable that can handle an integer or a 'Q'. The variable can't be an integer because that can't handle a 'Q', and it can't be a char because that can't handle 456,987, so that leaves a string, which means you'll need to read into a string, test whether the user wants to quit, and if not, convert the string to an integer and read in the other variables.

So the key thing to remember, again, is don't expect to be able to read directly into the variables. You'll need to read into some other variable, then test and convert and act accordingly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.