Initalizing???

#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";
return 0;
}

void DisplayMenu()
{
cout << "-------------MENU--------------" << endl;
cout << "1. Practice Addition" << endl;
cout << "2. Practice Subtraction" << endl;
}
void Addition()
{
int choice,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, 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');

}
}
}
}




The only problem that I am having is my program will not run. It explains that
"""error C4700: uninitialized local variable 'choice' used"""
The problems are on lines 37 and 74 which I placed multiple stars next to
please tell me what I am doing wrong and how to initialize it

int choice;
cin >> choice;

Upon compilation, choice does not have any value. Since you're using it in various places; the program needs an initial value on compilation. Try:

int choice=0;

Initialization means giving an intial value to something.

Erhm, also, you seem to be using choice in void addition without it having any value...

int choice
if (choice == 1)

Of course choice will never be equal to 1, because of the scope of choice. You placed it initially in the main. So once we go out of the main; choice is gone. You gotta put choice outside of main, and initialize it only once; or pass it on to other functions. Same shit for void substraction. I'm too tired to change the code, I'll do it tomorrow if no one does it by then.
Last edited on
I tried placing the choice = 0 instead of just choice and it allowed me to run the program, unfortunately as soon as I interacted (pressing 1 or 2) it automatically stated "have a nice day" which was supposed to be at the end of the program :/
Please use code tags
http://www.cplusplus.com/articles/z13hAqkS/

The only problem that I am having is my program will not run. It explains that
"""error C4700: uninitialized local variable 'choice' used"""


this is your problem
1
2
3
4
	int choice, x, y, ans, r = 0, w = 0;
	char cont;

	if (choice == 1)


you are trying to compare choice to 1 but choice doesn't contain a meaningful value, because you didn't give it one.

if (choice == 1)
if your going to do this you would have to pass choice to your function. However, you don't even need this if statement because, this function only executes when choice equals 1. so you know if your in this function that choice equals one.

Last edited on
Topic archived. No new replies allowed.