Case doesnt work

Mar 19, 2017 at 3:14am
hi guys , just wondering why doesnt case 4 run in the program. Can you please help me by pointing at the problem and what is the correct structure.
case 3:cout << "please insert a number" << endl;
cin >> n;
cout << endl;
if (n <= 1)
{
cout << "please input a number that is greater than 1" << endl;
cin >> n;
}
isprime( n);
break;
case 4 : cout << "please insert a number " << endl;
cin >> a;
cout << "please insert a second number" << endl;
cin >> b;


system("pause");
return 0;
}

void hello()
{
cout << "hello World" << endl;
}

void printBetween(int a, int b)
{
cout << endl;
if (a <= b)
{

for (int i = a; i <= b; i = i + 1)
{
cout << i << endl;
}
}
else if (b <= a)
{

for (int i = b; i <= a; i = i + 1)
{

cout << i << endl;
}

}
}

bool isprime(int n)
{
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)

}

cout << endl;
if (isprime(n))
{
cout << n << " is a prime number" << endl;
}
else
{
cout << n << " is not a prime number" << endl;
}

}

Mar 19, 2017 at 3:29am
I don't see anything wrong with case 4. Please use code tags! and please post the rest of your code so we can test it.
Mar 19, 2017 at 4:12am
Yes, please do post the entire switch statement at the very least.

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
61
62
63
64
65
66
67
case 3:cout << "please insert a number" << endl;
cin >> n;
cout << endl;
if (n <= 1)
{
cout << "please input a number that is greater than 1" << endl;
cin >> n;
}
isprime( n);
break;
case 4 : cout << "please insert a number " << endl;
cin >> a;
cout << "please insert a second number" << endl;
cin >> b;


system("pause");
return 0;
}

void hello()
{
cout << "hello World" << endl;
}

void printBetween(int a, int b)
{
cout << endl;
if (a <= b)
{

for (int i = a; i <= b; i = i + 1)
{
cout << i << endl;
}
}
else if (b <= a)
{

for (int i = b; i <= a; i = i + 1)
{

cout << i << endl;
}

}
}

bool isprime(int n)
{
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)

}

cout << endl;
if (isprime(n))
{
cout << n << " is a prime number" << endl;
}
else
{
cout << n << " is not a prime number" << endl;
}

}


indenting your code will make it easier to read but it looks like your switch statement has a return in it. It should not return anything unless it is a function and every case should return something including default.


Here is a structure you can follow which may help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Int main()
{
    
    cout << "How many players? No more than 4 and no less than 2! << endl;
    int numPlayers;
    cin >> numPlayers;

switch(numPlayers)
    {
        case 2: // basically means if numPlayer = 1
            //Whatever you want to happen.
            cout << "There are 2 players." << endl;
            break;
        case 3: //if numPlayers = 3
            cout << "There are 3 players." << endl; 
            break;
        case 4: //if numPlayers = 4
            cout << "There are 4 players." << endl;
            break;
        default: //If numPlayers doesnt match any of the possible cases. 
            std::cout << " There can only be 2-4 players! " << std::endl;
    }
} 

Last edited on Mar 19, 2017 at 4:21am
Mar 19, 2017 at 2:42pm
Hello Angel1,

Case 4 does work, but you do not do anything except input two numbers, pause and then exit the program. I think you might want to call the "printBetween" function and change the "return 0" to "break". Notice what kingkush has said.

Hope that helps,

Andy

Edit: after seeing the menu the function to call in case 4 would be "leastCommonDenominator" which you do not have.
Last edited on Mar 19, 2017 at 11:41pm
Mar 19, 2017 at 10:39pm
sorry for the late reply and for the confusion. Here is the entire code.


//A menu of functions that the user is able to choose from
#include <iostream>
using namespace std;

void hello();
void printBetween(int, int);


int main()
{
int choice, a, b, n;
bool isprime(int n);
int leastCommonDenominator(int a, int b);

cout << " Menu Of Functions" << endl;

cout << "1. Void hello" << endl;
//
cout << "2. Void printBetween" << endl;
//
cout << "3. Bool isPrime" << endl;
//
cout << "4. Int leastCommonDenominator " << endl;
//
cout << "5. Void squaredOpposite " << endl;
//
cout << " Enter your choice ( only menu number is acceptable): " << endl;
cin >> choice;
// User's choice of operator
switch (choice)
{
case 1:
hello();
break;
case 2:
cout << "please insert a number" << endl;
cin >> a;
cout << "please insert second number" << endl;
cin >> b;
printBetween(a, b);
break;
case 3:cout << "please insert a number" << endl;
cin >> n;
cout << endl;
if (n <= 1)
{
cout << "please input a number that is greater than 1" << endl;
cin >> n;
}
isprime( n);
break;
case 4 : cout << "please insert a number " << endl;
cin >> a;
cout << "please insert a second number" << endl;
cin >> b;


system("pause");
return 0;
}

void hello()
{
cout << "hello World" << endl;
}

void printBetween(int a, int b)
{
cout << endl;
if (a <= b)
{

for (int i = a; i <= b; i = i + 1)
{
cout << i << endl;
}
}
else if (b <= a)
{

for (int i = b; i <= a; i = i + 1)
{

cout << i << endl;
}

}
}

bool isprime(int n)
{
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)

}

cout << endl;
if (isprime(n))
{
cout << n << " is a prime number" << endl;
}
else
{
cout << n << " is not a prime number" << endl;
}

}


Again, case 4 doesnt want to run, please can you show me the correct structre while using my code. I am not good when people explain to me the logic
Mar 19, 2017 at 11:14pm
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//A menu of functions that the user is able to choose from
#include <iostream>
using namespace std;

void hello();
void printBetween(int, int);


int main()
{ 
int choice, a, b, n;
bool isprime(int n);
int leastCommonDenominator(int a, int b);

cout << " Menu Of Functions" << endl;

cout << "1. Void hello" << endl;
// 
cout << "2. Void printBetween" << endl;
// 
cout << "3. Bool isPrime" << endl;
// 
cout << "4. Int leastCommonDenominator " << endl;
// 
cout << "5. Void squaredOpposite " << endl;
// 
cout << " Enter your choice ( only menu number is acceptable): " << endl;
cin >> choice;
// User's choice of operator
switch (choice)
{
case 1:
hello();
break;
case 2:
cout << "please insert a number" << endl;
cin >> a;
cout << "please insert second number" << endl;
cin >> b;
printBetween(a, b);
break;
case 3:cout << "please insert a number" << endl;
cin >> n;
cout << endl;
if (n <= 1)
{
cout << "please input a number that is greater than 1" << endl;
cin >> n;
}
isprime( n);
break;
case 4 : cout << "please insert a number " << endl;
cin >> a;
cout << "please insert a second number" << endl;
cin >> b;


system("pause");
return 0;
}

void hello()
{
cout << "hello World" << endl;
}

void printBetween(int a, int b)
{
cout << endl;
if (a <= b)
{

for (int i = a; i <= b; i = i + 1)
{
cout << i << endl;
}
}
else if (b <= a)
{

for (int i = b; i <= a; i = i + 1)
{

cout << i << endl;
}

}
}

bool isprime(int n)
{
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)

}

cout << endl;
if (isprime(n))
{
cout << n << " is a prime number" << endl;
}
else
{
cout << n << " is not a prime number" << endl;
}

}
Mar 19, 2017 at 11:26pm
If line 13: int leastCommonDenominator(int a, int b); if a function declaration it should be outside of main with the other declarations. I made some small changes but...

FOR THE LOVE OF GOD INDENT YOUR CODE HBLJKNJLHBGKHBLJKN:JH

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
using namespace std;

void hello();
void printBetween(int, int);

int leastCommonDenominator(int a, int b);

int main()
{ 
    int choice, a, b, n;
    bool isprime(int n);

    cout << " Menu Of Functions" << endl;

    cout << "1. Void hello" << endl;
    // 
    cout << "2. Void printBetween" << endl;
    // 
    cout << "3. Bool isPrime" << endl;
    // 
    cout << "4. Int leastCommonDenominator " << endl;
    // 
    cout << "5. Void squaredOpposite " << endl;
    // 
    cout << " Enter your choice ( only menu number is acceptable): " << endl;
    cin >> choice;
    // User's choice of operator
    switch (choice)
    {
        case 1:
            hello();
            break;
        case 2:
            cout << "please insert a number" << endl;
            cin >> a;
            cout << "please insert second number" << endl;
            cin >> b;
            printBetween(a, b);
            break;
        case 3:
            cout << "please insert a number" << endl;
            cin >> n;
            cout << endl;
            if (n <= 1)
            {
                cout << "please input a number that is greater than 1" << endl;
                cin >> n;
            }
            isprime( n);
            break;
         case 4 : 
             cout << "please insert a number " << endl;
             cin >> a;
             cout << "please insert a second number" << endl;
             cin >> b;
             break;
    }
//system("pause");
//return 0;
}

void hello()
{
cout << "hello World" << endl;
}

void printBetween(int a, int b)
{
cout << endl;
if (a <= b)
{

for (int i = a; i <= b; i = i + 1)
{
cout << i << endl;
}
}
else if (b <= a)
{

for (int i = b; i <= a; i = i + 1)
{

cout << i << endl;
}

}
}

bool isprime(int n)
{
    for (int i = 2; i <= n / 2; i++)
    {
        if (n % i == 0)
        {
            cout << endl;
        }
        if (isprime(n)) //might neeed "else if" statement instead
        {
            cout << n << " is a prime number" << endl; 
        }
        else
        {
            cout << n << " is not a prime number" << endl;
        }
    }

}
Last edited on Mar 20, 2017 at 12:20am
Mar 19, 2017 at 11:38pm
Hello Angel1,

Again, case 4 does work see message http://www.cplusplus.com/forum/beginner/211360/#msg990140 Your program does not.

You are missing a closing "}" on main and the isprime function has an improperly placed semicolon on the first if statement. And is prime is a recursive function with no way out, so it will over run the stack at some point.

Hope that helps,

Andy
Mar 19, 2017 at 11:42pm
nvm i found problem
Mar 25, 2017 at 1:46pm
What was the problem?
Mar 25, 2017 at 11:21pm
i closed my application for my code, then it started to work when i reopen . i think it was a bug in the system.
Mar 26, 2017 at 2:42pm
Im doing almost the exact same problem how did you do case 5? this is the step i'm missing
: void squaredOpposite(double &n)
Sets the contents of the argument to be the squared value of n, with the opposite sign compared to n
E.G. input of 3 would result in output of -9
n is being passed as a reference variable, meaning changes made to it in the function will be reflected in the argument that was passed into the function call
-Asks the user if they would like to return to the menu
Topic archived. No new replies allowed.