Switch between 3 different programs

I need to use switch, to make a program work, when input a specific character - n,p,k or r, different program for each char, cant figure out, how to initialize switch function to recognize characters, something missing, or a lot missing and all this is really wrong. Besides, where do i initialize variables a and b used by all 3 programs?

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
#include <iostream>
#include <stdlib.h> 
#include <ctime>
#include<math.h>
using namespace std;
int main()

{   int x;
    cout<<"Rekinasanas programma\n";
    cout<<"Ievadiet burtu n, lai izdrukatu autora vardu\n";
    cout<<"Ievadiet burtu k, lai rekinatu skaitlu pakapes\n";
    cout<<"Ievadiet burtu p,lai atrastu pirmskaitlus\n";
    cout<<"ievadiet burtu r, lai speletu ciparu minesanas speli\n";
 
cin>>x;
    switch (x){
       case n: cout<<"Ausma Kalnina\n";
       case k:
            int a, b;
            cout<<"Skaitla kapinashana"<<endl;
            cout<<"ievadi skaitli!"<<endl;
           	cin>>a;
	        cout<<"ievadi pakapi"<<endl;
	        cin>>b;
	        cout<<"Rezultats! "<<pow(a,b)<<endl;
            break;
       case p:
             srand (time(0));
             int a;
             cout<<"Ievadi veselu skaitli"<<endl;
             cin>>a;
             for(int b=1; b<=a; b++)
             {
              bool prime;
              if ((b==2)||(b==3)){prime=true; cout<<b<<" ir pirmskaitlis"<<endl;}
              else if ((b%2==0)||(b%3==0)||(b%5==0)||(b%7==0)) {prime=false;cout<<b<<" nav pirmskaitlis"<<endl;}
              else {prime=true; cout<<b<<" ir pirmskaitlis"<<endl;}
              }break;
       case r:
            int a, b;
             srand (time(0));
             a = rand() % 10 + 1;
             cout << "Uzmini ciparu no 1 to 10!"<<endl; 
             cin >> b;
             do{ 
             if (a<b) {cout <<  "Cipars ir mazaks"<<endl;cin >> b;}
             else if (a>b) {cout << "cipars ir lielaks"<<endl;cin >> b;}
             } while (a!=b);
             cout << "Apsveicu!"<<endl;
             break;
}     
system("pause");
return 0;
}
it doesnt matter what programs do, they all work just as I need, just need help with how to make them function in 1 now.
closed account (o3hC5Di1)
Hi there,

Your problem is that you declare x as an int, but then you want to compare it to characters. So the solution would be to change the following:

1
2
3
4
5
6
7
8
char x;
//...

cin>>x;
    switch (x){
       case 'n': cout<<"Ausma Kalnina\n";  
       case 'k': //note the single quotes
//... 


Hope that helps. Please do let us know if you require any further help

All the best,
NwN
Thanks! it works! also declared int a, b at the beginning and all of it works!
Another thing - If I want for program to do the tasks as many times as I want, do I need to make a do while circle?
Now I can excecute each function just once and then it stops.
closed account (o3hC5Di1)
Hi there,

Glad it worked :)

If I want for program to do the tasks as many times as I want, do I need to make a do while circle?


Yes, entirely correct - wrap the code that needs to be repeated in a do/while loop and you should be fine.

All the best,
NwN
Ok, all this works like this now! just the issue is, i want the programm to continue not to break and every time i input choice letters, do the tasks, but somehow if i put cin>>x; instead of the break in the end of each code, it doesnt react to char letters anymore, it just does all the tasks in a row, for example have to be able to do the tasks in any sequence, like r, k e, p and then for example r again.


#include <iostream>
#include <stdlib.h> 
#include <ctime>
#include<math.h>
using namespace std;
int main()

{ 
char x;
int a,b;
    cout<<"Rekinasanas programma\n";
    cout<<"Ievadiet burtu k, lai izdrukatu autora vardu\n";
    cout<<"Ievadiet burtu p, lai rekinatu skaitlu pakapes\n";
    cout<<"Ievadiet burtu r,lai atrastu pirmskaitlus\n";
    cout<<"ievadiet burtu e, lai speletu ciparu minesanas speli\n";
    
cin.get(x);
    switch (x){
       case 'k': cout<<"Ausma Kalnina\n";break;
       case 'p':
            {cout<<"Skaitla kapinashana"<<endl;
            cout<<"ievadi skaitli!"<<endl;
           	cin>>a;
	        cout<<"ievadi pakapi"<<endl;
	        cin>>b;
	        cout<<"Rezultats! "<<pow(a,b)<<endl;}
            break;
       case 'r':
             srand (time(0));
             cout<<"Ievadi veselu skaitli"<<endl;
             cin>>a;
             for(int b=1; b<=a; b++)
             {
              bool prime;
              if ((b==2)||(b==3)){prime=true; cout<<b<<" ir pirmskaitlis"<<endl;}
              else if ((b%2==0)||(b%3==0)||(b%5==0)||(b%7==0)) {prime=false;cout<<b<<" nav pirmskaitlis"<<endl;}
              else {prime=true; cout<<b<<" ir pirmskaitlis"<<endl;}
              }break;
       case 'e':
             srand (time(0));
             a = rand() % 10 + 1;
             cout << "Uzmini ciparu no 1 to 10!"<<endl; 
             cin >> b;
             do{ 
             if (a<b) {cout <<  "Cipars ir mazaks"<<endl;cin >> b;}
             else if (a>b) {cout << "cipars ir lielaks"<<endl;cin >> b;}
             } while (a!=b);
             cout << "Apsveicu!"<<endl;
             break;
} 
  
system("pause");
return 0;
}

Last edited on
Use separate functions. It makes easier to see the structure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void kapina()
{
  // code
}

void uzmini()
{
  // code
}

...

case 'p':
  kapina();
  break;
case 'e':
  uzmini();
  break;


Your code still lacks that outer loop
1
2
3
while ( cin >> x ) {
  switch ...
}

Thanks a lot! Problem solved, now it works as I need it to :)
Topic archived. No new replies allowed.