Program that inputs an integer and outputs info depending on case choice

I was able to set up the code so that the user chooses from the cases but I am confused with how I go about implementing the code into each case. For the first case I need to find the number of digits in an integer using "getnumdigits". I keep getting the error saying "jump bypasses variable initialization". I am wondering where my mistake can be fixed before I attempt to proceed to the next cases.


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

int main ()
{
//declare a choice from the menu
int choice;

//Display choice menu
do {
cout << "Choose 1 to find the number of digits in an integer." << endl;
cout << "Choose 2 to find the nth digit in an integer." << endl;
cout << "Choose 3 to find the sum of all digits of an integer." << endl;
cout << "Choose 4 to quit this program." << endl;
cin >> choice;

switch (choice)
{
case 1:
int number;
int getNumDigits = 0;
cout << "Enter a positive integer: " << endl;
cin >> number;
while (number < 0) {
cout << "Invalid number entered." << endl;
cout << "Enter a positive integer: " << endl;
cin >> number;
}
while (number > 0)
{
number = number / 10;
getNumDigits++;
}
cout << "Number of digits in " << number << ": " << getNumDigits << endl;
return 0;
break;

case 2:

break;

case 3:

break;

case 4:
cout << "End of program." << endl;
break;

default:
cout << "Not a valid choice." << endl;
}
} while (choice != 4);

return 0;

}
> "jump bypasses variable initialization"
to declare variables inside the `case' you need to define another scope
1
2
3
4
5
case 1:
{
   int number;
   //...
}
put curl brackets around your case 1:
close them before the break
also, why do you want to return 0 at the end of case 1?
sont you want to get another input from user?
I was trying to write my first function inside of a case before moving to next cases. So that when I test/compile I could see if I was on the right track.
Here Is The Correct Code :

If you are gonna use the variables declared inside the braces of case 1 outside those braces DECLARE THEM WITH GLOBAL SCOPE

I suggest reading this for better understanding : https://www.codesdope.com/cpp-scope-of-variables/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cstdlib>
using namespace std;

int main ()
{
//declare a choice from the menu
int choice;
int number;
int getNumDigits = 0;
//Display choice menu
do {
cout << "Choose 1 to find the number of digits in an integer." << endl;
cout << "Choose 2 to find the nth digit in an integer." << endl;
cout << "Choose 3 to find the sum of all digits of an integer." << endl;
cout << "Choose 4 to quit this program." << endl;
cin >> choice;

switch (choice)

{case 1:

cout << "Enter a positive integer: " << endl;
cin >> number;
while (number < 0)

cout << "Invalid number entered." << endl;
cout << "Enter a positive integer: " << endl;
cin >> number;

while (number > 0)

number = number / 10;
getNumDigits++;

cout << "Number of digits in " << number << ": " << getNumDigits << endl;
return 0;
break;

case 2:

break;

case 3:

break;

case 4:
cout << "End of program." << endl;
break;

default:
cout << "Not a valid choice." << endl;

 }}
 while (choice != 4);

return 0;

}

Topic archived. No new replies allowed.