Selection menu with case and break commands

Hi all,

i'm trying to've this program to display a selection menu but i keep having this error "initialization of RESULT is skipped by case label"
Can someone look at this and let me know what i'm doing wrong.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
int num1, num2, choice;


// Set the numeric output.
cout << fixed << showpoint << setprecision(2);

do
{
cout << "\n\t\tMath Test Menu\n\n";
cout << "1. Addition \n";
cout << "2. Subtraction \n";
cout << "3. Multiplication\n";
cout << "4. Division \n";
cout << "5. Quit the Program\n\n";
cout << "Enter your choice: ";
cin >> choice;

// Validate the menu selection.
while (choice < 1 || choice > 5)
{
cout << "Please enter 1, 2, 3 or 4: ";
cin >> choice;
}

// Respond to the user's menu selection.
switch (choice)
{
case 1:

srand((unsigned)time(0));
num1 = (rand()%700)+1;
num2 = (rand()%700)+1;
const int RESULT = num1 + num2;
cout << "\n\n\t num1 ";
cout << "\n\n\t + ";
cout << "\n\n\t num2 ";
cout << "\n\n\t -------";
cin.get();
cout << "\n\n\t RESULT ";
cout << "\n\n\n\tPress Enter to continue...";
break;

case 2:

srand((unsigned)time(0));
num1 = (rand()%700)+1;
const int RESULT = num1 - num2;
num2 = (rand()%700)+1;
cout << "\n\n\t num1 ";
cout << "\n\n\t - ";
cout << "\n\n\t num2 ";
cout << "\n\n\t ------ ";
cin.get();
cout << "\n\n\t RESULT ";
cout << "\n\n\n\tPress Enter to continue...";
break;

case 3:

srand((unsigned)time(0));
int num1;
int num2;
num1 = (rand()%700)+1;
num2 = (rand()%700)+1;
const int RESULT = num1 * num2;
cout << "\n\n\t num1 ";
cout << "\n\t\t * ";
cout << "\n\n\t num2 ";
cout << "\n\n\t ------";
cin.get();
cout << "\n\n\t RESULT ";
cout << "\n\n\n\tPress Enter to continue..."
break;
case 4:
srand((unsigned)time(0));

num1 = (rand()%700)+1;
num2 = (rand()%700)+1;
const int RESULT = num1 / num2;
cout << "\n\n\t num1 ";
cout << "\n\n\t / ";
cout << "\n\n\t num2 ";
cout << "------" << endl;
cin.get();
cout << "\n\n\t RESULT ";
cout << "\n\n\n\tPress Enter to continue...";
break;

case 5:
cout << "Program ending.\n";

break;
}
} while (choice !=5);
return 0;
}
closed account (zwA4jE8b)
try initializing result with your other declarations... ya know int result = 0, why do you have it declared constant in the middle of your program?

also to output it, you need to use the insertion operator


cout << "\n\n\t" << RESULT;
thanks, i made some changes following ur directions and now the program is working. i'm using
cin.get(); to make the program pause before displaying the result but so far it's not working. Do u've any suggestion?




#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
int num1, num2, choice;
int RESULT;



// Set the numeric output.
cout << fixed << showpoint << setprecision(2);

do
{
cout << "\n\t\tMath Test Menu\n\n";
cout << "1. Addition \n";
cout << "2. Subtraction \n";
cout << "3. Multiplication\n";
cout << "4. Division \n";
cout << "5. Quit the Program\n\n";
cout << "Enter your choice: ";
cin >> choice;

// Validate the menu selection.
while (choice < 1 || choice > 5)
{
cout << "Please enter 1, 2, 3 or 4: ";
cin >> choice;
}

// Respond to the user's menu selection.
switch (choice)
{
case 1:

srand((unsigned)time(0));
num1 = (rand()%700)+1;
num2 = (rand()%700)+1;
RESULT = num1 + num2;
cout << setw (6) << num1 << endl;
cout << "+ ";
cout << setw (4) << num2 << endl;
cout << "------" << endl;
cin.get();
cout << setw (6) << RESULT << endl;
cout << "\n\n\n\tPress Enter to continue...";
break;

case 2:

srand((unsigned)time(0));
num1 = (rand()%700)+1;
num2 = (rand()%700)+1;
RESULT = num1 - num2;
cout << setw (6) << num1 << endl;
cout << "- ";
cout << setw (4) << num2 << endl;
cout << "------" << endl;
cin.get();
cout << setw (6) << RESULT << endl;
cout << "\n\n\n\tPress Enter to continue...";
break;

case 3:

srand((unsigned)time(0));
num1 = (rand()%700)+1;
num2 = (rand()%700)+1;
RESULT = num1 * num2;
cout << setw (6) << num1 << endl;
cout << "* ";
cout << setw (4) << num2 << endl;
cout << "------" << endl;
cin.get();
cout << setw (6) << RESULT << endl;
cout << "\n\n\n\tPress Enter to continue...";
break;

case 4:
srand((unsigned)time(0));
num1 = (rand()%700)+1;
num2 = (rand()%700)+1;
RESULT = num1 / num2;
cout << setw (6) << num1 << endl;
cout << "/ ";
cout << setw (4) << num2 << endl;
cout << "------" << endl;
cin.get();
cout << setw (6) << RESULT << endl;
cout << "\n\n\n\tPress Enter to continue...";
break;

case 5:
cout << "Program ending.\n";

break;
}
} while (choice !=5);
return 0;
}
closed account (zwA4jE8b)
to use cin.get(); to pause a program I have noticed there has to be some output before it.

cout << "press any key to exit...";
cin.get();

that works for me. I do not know the details of why it works like that and not with just cin.get(); alone. if you find out let me know.
i try ur suggestion but it's still not working, cin.get () alone is working in the program below, so the program pause before displaying the result and the user has to press enter to see the result.

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;

int main ()
{


srand((unsigned)time(0));
int num1;
int num2;
num1 = (rand()%700)+1;
num2 = (rand()%700)+1;
int const RESULT = num1 + num2;
cout << setw (6) << num1 << endl;
cout << "+ ";
cout << setw (4) << num2 << endl;
cout << "------" << endl;
cin.get();
cout << setw (6) << RESULT << endl;
return 0;
}
Topic archived. No new replies allowed.