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?
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include<math.h>
usingnamespace 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;}
elseif ((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;}
elseif (a>b) {cout << "cipars ir lielaks"<<endl;cin >> b;}
} while (a!=b);
cout << "Apsveicu!"<<endl;
break;
}
system("pause");
return 0;
}
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.
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;
}