interchanging lines

Write your question here.
well.. i dont know if i put the right tittle so anyways
im new to c++ prog. and i kind of want to know something
here is my code a basic one.
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
#include <iostream>
#include <string>
 
using namespace std;
 
int main ()
{
   string q1;
   cout << "What is your name?"<<endl;
   getline (cin,q1);
   string q2;
   cout << "How old are you?"<<endl;
   getline (cin,q2);
   string q3;
   cout << "What is your contact number?"<<endl;
   getline (cin,q3);
   string q4;
   cout << "Do you believe GOD?"<<endl;
   getline (cin,q4);
   // cout << "Why do you believe in GOD?"<<endl;
   // cout << "Why dont you believe in GOD?"<<endl;
   
   
   cout << "So You are: " <<q1<<endl;
   cout << "You are:" <<q2<<endl;
   cout << "Your contact number is: " <<q3<<endl;
   cout << "and your answer is:" <<q4<<endl;

   system("PAUSE");
   return 0;
}


with the code i provided... i am wondering if someone answers my question specifically q4, depending on his/her answer of yes and no? how can i change the next question according to what he answer on q4? in which the question i want that will vary according to the answer on q4 is the one in comment..

will that be possible?

how?

and im new to programing so please....

SPEAK ENGLISH :)
With std::string you can just use the comparison operator ==
1
2
3
4
5
6
7
8
9
10
if(q4 == "yes")
{
...
}
else if(q4 == "no")
{
...
}
else
   cout << "What does that mean?" << endl;

This assumes the answer is fully lowercase.
ok so... really im new.. i dont have idea with that comparison or what so ever..

how do i use this if and else if? sorry with the question XD
ok i got it working but i do wonder another thing

in line 27 the answer that will display vary on his/her answer on q4 right? now i was wondering? how will i be able to add his reason that he stated on the comparison operator?

like if he says yes line 27 will display yes and his answer on line 20 and if he says no on line 27 it will also display his answer on line 21?
Last edited on
Basically if the expression inside the parentheses is true, the group of instructions after the 'if' is executed.
'else if' can be put after an 'if' to check another condition if the ones preceeding it were false. You can use as many 'else if' as you want.
'else' will execute the instruction of its group if all of the preceeding checks were false (you can have only one 'else').

This tutorial is probably better than my exlanation though.
http://www.cplusplus.com/doc/tutorial/control/

EDIT
Use another string.
Last edited on
uhm...

what do you mean to use another string? eh...

what i am expecting to popout is that the answer he provided on line 20 or 21 will be included in line 27..

but the problem is if i add <<q4<<q6<<endl;

and he says yes the q6 will be blank right?

where in q6 is supposed to be answered by persons who answers no
in q4? XD do you get my point? ahaha

anyways im off to school i'll just check back l8r :)
Last edited on
@xeimaster

I think this is what you were referring to. If the answer is "Yes" or "No", then you are asked "Why?" or "Why not?", and that is added to your 'Yes" or "No" answer, along with a comma. Otherwise, the q4 string becomes "Subject not quite sure!!".

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
// Interchanging Lines.cpp : main project file.

#include <iostream>
#include <string>

using namespace std;

int main ()
{
	string q1;
	bool answer=false;
	cout << "What is your name?"<<endl;
	getline (cin,q1);
	string q2;
	cout << "How old are you?"<<endl;
	getline (cin,q2);
	string q3;
	cout << "What is your contact number?"<<endl;
	getline (cin,q3);
	string q4;
	cout << "Do you believe GOD? (Yes/No)"<<endl;
	getline (cin,q4);
	if(q4=="Yes" || q4=="No")
	{
		string q5;
		if (q4=="Yes")
		{
			
			answer=true; // Show we received an answer
			cout << "Why do you believe in GOD?"<<endl;
			getline(cin,q5);
		}
		if (q4=="No")
		{
			answer=true; // Or show we received an answer, here
			cout << "Why dont you believe in GOD?"<<endl;
			getline(cin,q5);
		}
		q4=q4+", "+q5;
	}
	if (!answer) // If answer still false
		q4 = "Subject not quite sure!!";

	cout << "So You are: " <<q1<<endl;
	cout << "You are: " <<q2<<endl;
	cout << "Your contact number is: " <<q3<<endl;
	cout << "and your answer is: " <<q4<<endl; // Show the current q4 string

	system("PAUSE");
	return 0;
}
Although I think it's best not to use a string for q4 in this case, because one shouldn't have to cater for any mis-typing of an answer as in "yEs". Even though whitenite1's code handles an invalid answer on line 42, it is much easier all round IMO to just use a char.

So just make q4 a char, use the toupper function, then compare to 'Y' or 'N'

Hope all is well
ok so i got it how it works thnx @whitenite1

i really wasn't expecting that boolean and @ideasman i dun know how to use and what is your point XD sorry i am really new like i just started learning c++

so i was xpecting stringline only XD

and @ideasman
can you give me a sample syntax using mine using the char and that toupper function? :)
Last edited on
Topic archived. No new replies allowed.