How do you make this end

I need it so that when you enter q or Q that it ends the program.How do I do that? please add it to my code.Thanks!
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{

srand(time(NULL));


bool run = true;

while (true)
{
cout << "A addition" << endl;
cout << "S subtraction" << endl;
cout << "Q quit" << endl;
cout << "Please enter your choice: ";

char choice;
cin >> choice;



switch(choice)
case 'A':
case 'a':
{
int num1 = rand() % 100 + 1;
cout << "How much is " << num1;

int num2 = rand() % 100 + 1;
cout << " + " << num2 << "? ";

int answer;
cin >> answer;
cin.ignore(100, 10);

int sum = num1 + num2;



if (answer == sum)
cout << "Congratulations, you got the right answer!" << endl;

while(answer != sum)
{cout << "How much is " << num1;
cout << " + " << num2 << "? ";
cin >> answer;
if (answer == sum)
cout << "Congratulations, you got the right answer!" << endl;
}

cout << endl;
}


switch(choice)
case 'S':
case 's':
{

int num1 = rand() % 100 + 1;
cout << "How much is " << num1;

int num2 = rand() % 100 + 1;
cout << " - " << num2 << "? ";


int answer;
cin >> answer;
cin.ignore(100, 10);

int sum = num1 - num2;

if (answer == sum)
cout << "Congratulations, you got the right answer!" << endl;


while(answer != sum)
{cout << "How much is " << num1;
cout << " + " << num2 << "? ";
cin >> answer;
if (answer == sum)
cout << "Congratulations, you got the right answer!" << endl;
}

char choice;

switch(choice)
case 'Q':
case 'q':
break;
}
}

return 0;
}
you pop everything in a loop that has a variable set to true until when prompted pressing q changes that variable to false, then we use code tags next time we post code because not doing that is naughty
Please use the <> code tags when posting code.

At the end, you have char choice;, but never ask the user for input to initialize it.

Then in the case statements for the switch, if the user had entered 'Q' or 'q', you don't have a way to exit?

Maybe use a bool, instead of while( true ).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool running = true;

while( running )
{
    // code...

    switch( choice )
    {
        case 'Q':
        case 'q':
            running = false;
            break;
    }
}


Hope this helps. (:
I still dont get it,can you make an example out of my code
I'll make some explicit suggestions when you edit your post to use code tags.
[code]
code here
[/code]

Topic archived. No new replies allowed.