How to add condition to write something for a "cout" ?

I need some help for my program which I wanna send to my friends. I need to put a condition to write Y/N to send the 2nd cout I put, can you tell me what code do I need?

1
2
3
4
5
6
7
8
9
10
11
12
13
    #include <vector>
    #include <fstream>
    #include <iostream>
    using namespace std;
    char response;
    int main()
    {
    cout << "Wanna hear a joke?" << endl;
    cin >> response;
    switch(response);
    cout << "UrsAWarLord Anti-Mage EleGiggle" << endl;
    }
1
2
3
4
5
6
7
8
9
10
11


switch(response)

case (Y):
cout<<"Whatever you want"<<endl;
break;
case(N):
cout<<"I dont want to";
break;
It doesn't work at all. Do I have to add something to make "y" and "n" available?
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


#include <string>
    #include <fstream>
    #include <iostream>
    using namespace std;
 
	
	int main()
    {
	string str;
	enum decision {Y,N};
	decision whichone;
	cout << "Wanna hear a joke?" << endl;
  
	getline(cin,str);
	if(str =="Y"){
		whichone = Y;
	}
	else if(str=="N"){
		whichone= N;
	}
	//what = response;
    switch(whichone){
    case(Y):
	cout<<"Yes, I want"<<endl;
	break;
	case(N):
	cout<<"No I, dont want"<<endl;
	break;
	}
	
	return 0;
 }



you must do an enum with Y and N...for that you use an string and compare...then load the enum variable and then it's ready to use in a switch case...
With that I finish this, can i add more words for the same task? If I try, somehow, to add more words for case(Y) and case(N) it gets blind. Thanks for everything by the way.
look at your enum enum decision {Y,N}; it contains two elements, Y and N, you could add more or change them too, for instance enum decision{yes,no,ok,tomorrow} then you have 4 case for your switch(decision) case(yes),case(no),case(ok),case(tomorrow)
Last edited on
I mean more words to lead to case(Y), I need for example yes,no,nu,da, it's possible?
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

#include <string>
    #include <fstream>
    #include <iostream>
    using namespace std;
 
	
	int main()
    {
	string str;
	enum decision {yes,no,nu,da};
	decision whichone;
	cout << "Wanna hear a joke?" << endl;
  
	getline(cin,str);
	if(str =="yes"){
		whichone = yes;
	}
	else if(str=="no"){
		whichone= no;
	}
       else if(str =="nu"){
		whichone = nu;
	}
	else if(str=="da"){
		whichone= da;
	}
	//what = response;
    switch(whichone){
    case(yes):
	cout<<"I'm yes"<<endl;
	break;
	case(nu):
	cout<<"I'm no"<<endl;
	break;
         case(nu):
	cout<<"I'm nu"<<endl;
	break;
	case(da):
	cout<<"I'm da"<<endl;
	break;
	

}
	
	return 0;
 }


It's possible you just put as many values as you want in the enum and with the name string that you want...each "word" will be a case for you....
Last edited on
What if I wanna nu = no & da = y task? What command do you use to pause the console when executing program? I dont like the system("Pause").
I have done the code, I still need a pause code, i'm not sure which to use.
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
           #include <string>
        #include <fstream>
        #include <iostream>
        #include <conio.h>
        using namespace std;
        int main()
        {
        string str;
        enum decision  {Y,N,n,y};
        decision whichone;
        cout << "Wanna hear a joke?(Y/N)" << endl;
        getline (cin,str);
        if(str =="Y");
            if(str =="y")
        {
            whichone = Y;
        }
        else if(str =="N");
         else if(str =="n")
        {
            whichone= N;
        }
        switch(whichone){
        case (Y):
        cout << "UrsAWarLord Anti-Mage EleGiggle" << endl;
        break;
        case(N):
        cout << "FeelsBadMan, see ya" << endl;
        break;
            }
             cout << endl;
             cout << "Press any key to continue...";

           getch();
        }
Last edited on
where do you need a pause?? does it have a time, or you want carry on executing after press a button??

and you can do this....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

  if((str =="Y")  or ( str == "y"));
         
        {
            whichone = Y;
        }
        else if((str =="N") or (str=="n"));
       
        {
            whichone= N;
        }



Last edited on
I need a pause to the ending because when i run the .exe it closes immediately. Secondly, can you tell me how to make a delay at a "spamming" cout?
I don't understand what you mean completely but there is a function is sleep_for and you choose the time you want the program sleeps...

http://www.cplusplus.com/reference/thread/this_thread/sleep_for/
Topic archived. No new replies allowed.