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.
#include <iostream>
#include <string>
usingnamespace 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..
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?
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').
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!!".
// Interchanging Lines.cpp : main project file.
#include <iostream>
#include <string>
usingnamespace 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'
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? :)