Why wont it work?

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void DisplayMenu();
void Addition();
void Subtraction();

int main()
{
int choice;

DisplayMenu();
cout << "\tEnter your choice: ";
cin >> choice;
switch (choice)
{
case 1: Addition(); break;
case 2: Subtraction(); break;
}
cout << "Have a nice day\n";

system("pause");
return 0;
}

void DisplayMenu()
{
cout << "-------------MENU--------------" << endl;
cout << "1. Practice Addition" << endl;
cout << "2. Practice Subtraction" << endl;
}
void Addition()
{
int choice=0,x,y,ans,r=0,w=0;
char cont;

if (choice == 1)
{
srand(time(0));
for (int j = 1; j <= 2; ++j)
{
x = rand() % 100;
y = rand() % 100;
cout << "\t" << x << " + " << y << " =?";
cin >> ans;
if (x + y == ans)
{
cout << "\t Correct\n"; r++;
do
{
cout << "\tContinue(y/n)? ";
cin >> cont;
} while (ans == 'y' || ans == 'Y');
}

else
{
cout << "\t Wrong\n"; w++;
do
{
cout << "\tContinue(y/n)? ";
cin >> cont;
} while (ans == 'y' || ans == 'Y');

}
}
}
}
void Subtraction()
{
int choice=0, x, y, ans, r = 0, w = 0;
char cont;

if (choice == 2)
{
srand(time(0));
for (int j = 1; j <= 2; ++j)
{
x = rand() % 100;
y = rand() % 100;
cout << "\t" << x << " - " << y << " =?";
cin >> ans;
if (x - y == ans)
{
cout << "\t Correct\n"; r++;
do
{
cout << "\tContinue(y/n)? ";
cin >> cont;
} while (ans == 'y' || ans == 'Y');
}

else
{
cout << "\t Wrong\n"; w++;
do
{
cout << "\tContinue(y/n)? ";
cin >> cont;
} while (ans == 'y' || ans == 'Y');

}
}
}
}


This program is running absolutely fine, unfortunately whenever I react to the program(pressing 1 or 2 when it asks for a choice) it automatically states "have a nice day" and ends the program. There are absolutely no errors in the actual program, not syntax or semantic, it will just not respond to the choice that I enter, any idea whats happening and how I can fix it??
Where are you actually saving the user's input? I can't find it. Also, please use the [ code ] and [ /code ] tags so it formats it in a more understandable format.
in the beginning
DisplayMenu();
cout << "\tEnter your choice: ";
cin >> choice;
switch (choice)
{

its the cin >> choice
Take a look at your Addition() function:
34
35
36
37
38
39
40
41
void Addition()
{
    int choice=0,x,y,ans,r=0,w=0;
    char cont;

    if (choice == 1)
    {
        // ... 

You initialize choice to 0 and then you immediately check if it's equal to 1.
Of course it's not going to be equal to 1 (because you just set it to 0), so that entire block never runs.

The important thing(s) to realize here is:
1. Your choice variable does not "carry over" from main(). The choice variable in Addition() is completely different and separate from the one in main().
2. Because of your switch statement in main() (lines 17-21), the only way Addition() will ever get called is if the user enters 1, so there's no need to check for choice == 1 anymore in Addition() because that's the only way Addition() could have gotten called in the first place.

That being said, the fix is to remove the if (choice == 1) check in line 39 of Addition() (and you have the same problem with your Subtraction() function as well).

By the way, you also have a problem with your loops. No matter what I enter when it says "Continue(y/n)? ", it always runs exactly twice.
One way to fix that would be to change
for (int j = 1; j <= 2; ++j) (line 42)
to
while (cont == 'y') (But also initialize cont to 'y' on line 36 -- or, better yet, make it into a do-while loop)
and then change your if (x + y == ans) check to
48
49
50
51
52
53
54
55
56
57
if (x + y == ans)
{
    cout << "\t Correct\n"; r++;
}
else
{
    cout << "\t Wrong\n"; w++;
}
cout << "\tContinue(y/n)? ";
cin >> cont;


200!
I'm a bit confused with how you corrected my program with the changing of the continuing aspect, is there a way for you to rewrite it to fit the code and actually make the continue option work? I know its a lot to ask but I tried your method and was unable to fix it properly.
Topic archived. No new replies allowed.