Run-Time Error variable not defined?

Y

yogi_bear_79

I shorted the following code for the purpose of this post. I get a
run-time error that
value is not defined. I don't understand what I have done wrong.
Through varios testing methods I've noticed it will throw the error on
both variables subChoice and value. Thanks for yuor time.



void mainMenu(int choice)
{
int subChoice, value;

switch ( choice )
{
case 1:
............
cout << "\n\n Please select a choice from the menu: ";
cin >> subChoice;
cout << "\n\n Please enter a number to convert: ";
cin >> value;
break;
case 2:
..........
}

subMenu(subChoice, value);
}
 
C

Christian Hackl

yogi_bear_79 said:
I shorted the following code for the purpose of this post. I get a
run-time error that
value is not defined.

"to get a run-time error" is a very broad description. Do you mean that
a std::runtime_error exception is thrown?
void mainMenu(int choice)
{
int subChoice, value;

These variables will have more or less random values until you assign
something to them. It's usually better to initialise local variables
like this:

int subChoice = 0;
int value = 0;
switch ( choice )
{
case 1:
............
cout << "\n\n Please select a choice from the menu: ";
cin >> subChoice;
cout << "\n\n Please enter a number to convert: ";
cin >> value;
break;
case 2:
..........
}

If the value of "choice" is not matched by any of the cases (or if there
is no assignment in at least one of the case blocks), then "subChoice"
and "value" continue to have random values.
subMenu(subChoice, value);

I'd guess that what actually causes the error happens inside of this
function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top