switch inside while loop

...........................................
...........................................
...........................................
while(op != 'q')

{
switch (op)
{
case 1:
case 2:
case 3:
case 4:
default: ..........................
.......................................
....................................

i have a problem , the while loop condition to stop is to enter 'q'
and inside it switch needs case 1, case 2, case 3 and case 4

so the variable should accept the integers of the cases and the 'q' to stop

how can i do that?
I assume op is of type char
Hence the test should be for a char, such as 'q' or '2' etc.

1
2
3
4
5
6
7
8
while (op != 'q')
{
    switch (op)
    {
        case '1':
        case '2':
    }
}
when i did this , once i'm entering 1 ,2 etc it gives me a result as if it's 'default' and when i enter 'q' i got an infinite loop
I hope the following example helps you somewhat:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main( void ) {
    char op = 0;   
    while ( op != 'q' ){
        printf("give option:");
        op = getchar();
        switch (op) {
            case '1': { printf("case 1\n"); break; }
            case '2': { printf("case 2\n"); break; }
            case '3': { printf("case 3\n"); break; }
            case '4': { printf("case 4\n"); break; }
            default: { break; }
        }
    }
    
    printf("Program finished...\n");
    return 0;
}


when i enter 'q' i got an infinite loop

First of all, your example loops as long as the provided input is NOT 'q'. Guessing from your snippet you only provide input once before you start the loop after which this never changes. So the input will never change from what you first provided. Hence the infinite loop.

when i did this , once i'm entering 1 ,2 etc it gives me a result as if it's 'default'

It will always go to the default depending on your input. In my example it WILL go to the case but since the input is followed by a '\n' (return when you press enter to submit input) it will go to the default case.

I hope this helps and there are various ways to surpas the problem I just mentioned :)
Last edited on
it stills doesn't work :( , here is my program , i don't know where the mistake is .

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
#include <iostream>
#include <cmath>

using namespace std;

int minu();
void primeChecker();
void fact(int);
char nextCh (char);
int abs (int);

int main()
{
	char op, ch;
	int n;
	double x;

	op = minu();
	while(op != 'q')
	{
		op = getchar();
		switch (op)
		{
		case '1':	primeChecker();
					break;
		case '2':	cout << "Enter an integer: ";
					cin >> n;
					fact(n);
					break;
		case '3':	cout << "Enter a character: ";
					cin >> ch;
					cout << "Next character is: " << nextCh(ch);
					break;
		case '4':	cout << "Enter a real number: ";
					cin >> x;
					cout << "Absolute value is: " << abs(x);
					break;
		default:	cout << "Wrong input. Try again...";
					break;
		}
		op = minu();
	}
	cout << "Thanks for using my program. Goodbye!!\n\n";

	return 0;
}


int minu()
{
	int n;

		cout <<endl<< "1. Prime Checker" << endl << "2. Factorial" << endl << "3. Next Character" << endl << "4. Absolute Value" ;
	    cout << endl << "Enter your choice, or 'q' to quit: ";
        cin >> n;
		return n;
}
void primeChecker()
{
	int n,i,pr;
	cout << "Enter an integer: ";
	cin >> n;

	pr=1;

	for (i=2;i<n;i++)
	{
		if(n%i==0)
		{
			pr=-1;
		}
	}
	if(pr==1)
	{
		cout << "This is a prime number" << endl;
	}
	else
	{
		cout << "This is NOT a prime number." << endl;
	}

}


void fact(int n)
{
	int i, f=1;
	for(i=1;i<=n;i++)
	f=f*i;
	cout << n << "!" << " = " << f << endl ;
}

char nextCh(char ch)
{
	
	ch=ch+1;
	return ch;
}

int abs(int x)
{
	return fabs(x);
}
There are a few problems here.
The main one is that function int minu() asks the user for an integer, and returns an integer, therefore it isn't possible to enter an alphabetic character such as 'q'. If you do so, the statement cin >> n; at line 55 will fail. This sets the 'fail' flag for the cin stream, meaning that any subsequent operation using cin will fail, unless the error flags are cleared.

Another problem is the op = getchar(); statement at line 21. If function mine() has been successful, there will still be a newline character remaining in the input buffer. Thus, after the getchar(). op will contain '\n' and thus the switch-case will follow the "default", as '\n' was not one of the options.

I'd recommend that the function minu() should use type char instead of type int.
And I'd recommend removing the getchar() completely.




yes it works!

Thank you so much :D
Topic archived. No new replies allowed.