0

How fo you code a question but if A is selected it must say something else as when B is selected?

Questions

1st Jan 2017, 5:39 PM
erik
13 Respuestas
0
Is the user inputting a response or could you elaborate on what you need to do?
5th Jan 2017, 9:40 PM
Ethan
0
no its just like a normal question I ask like do you think you are pretty (Yes or No) if Yes is selected then there must stand (You think to much of yourself) if No is selected there must stand (Thats probably true) but not getting it rite I got it rite to tell you something but not getting it rite with different outcomes.Im still new to this.
6th Jan 2017, 2:47 PM
erik
0
Oh ok, depending on how many responses are possible, there are a few ways to do this. The first is after you pose the question use in "input getter" like cin and set the response as a variable (cin>>userInput). Then compare the userInput variable to your list of responses using if, else if, or else statements. Don't forget to program how the program should work if none of the userInput match your responses (use your else statement). Another way, especially if you have a lot of options for input is what is called a switch statement. It works like the if statement, just easier for more options. Use cin>>userInput again and then switch (userInput ) { case 1: do this case 2: do that default: does if no cases match } If you are going to use a switch, take some time to make sure you research how it works, it can be a little weird with the case statements (the above is just an example for explaining purposes, there are some good examples online)
6th Jan 2017, 3:02 PM
Ethan
0
does that help?
6th Jan 2017, 3:02 PM
Ethan
0
ok I still dont quite understand should I just ask the question and then use the switch statement case 1:as the yes and case 2: as the no.Could you maybe code everything for me of what I described to you maybe please.
6th Jan 2017, 3:44 PM
erik
0
sorry, I won't write the whole code, I'll just give some snippets. We only really learn through coding. if(input == "yes") { do whatever } else if("no") { do something else } else { error statement }
6th Jan 2017, 4:27 PM
Ethan
0
so this is what I tried but too much errors #include <iostream> using namespace std; int main() { cout << "Do you think you are pretty ?"; if(input == "yes") //it says that input was not declared in the scope// { You think to much of yourself //"You" was not declared// } else if("no") { You are probably ugly } else { Please say Yes or No //"Please" was not declared in this scope// } return 0; }
6th Jan 2017, 8:46 PM
erik
0
ok, this is good stuff, with minor errors. the input is a variable, so you should initialize input and then after your cout statement, put cin>>input. This will prompt the user to input some response. Once the user types the answer and hits enter, then that will be stored in the input variable. In your if statement, the "you think too much of yourself " and the "You are probably ugly" statement should have quotes around them (making them a string) and put a cout<< in front of them. The same goes for the statement in your else braces. Remember cin>> is console input (the user provides input to the program) and cout<< is console output (which will display statements to the screen). You may run into something where the program will close right after it should display a statement, so you may need a system ("pause"); or some other statement that puts the program into standby. Lastly, are you familiar with while loops, if so you can use one to keep running until a valid input is given. If not, don't worry about it. I hope this helps
6th Jan 2017, 8:58 PM
Ethan
0
ok than you but it still say that "input" and "userinput" was not declared in this scope otherwise its still fine
7th Jan 2017, 10:02 AM
erik
0
ok, you just need to put String input; at the beginning of your function and the same with userInput. This is called variable initialization, it tells the computer what type of variable userInput and input will be
7th Jan 2017, 12:26 PM
Ethan
0
its still not working #include <iostream> using namespace std; int main() { String input; String userinput; cin >> userinput; cout << "Do you think you are pretty ?"; if(output == "yes") { cout << "You think to much of yourself"; } else if("no") { cout << "Yip you are ugly"; } else { cout << "Please awnser Yes or No"; } return 0; }
7th Jan 2017, 3:46 PM
erik
0
try putting #include <string> at the top of your program and the string type is lower-case. An easier way to do what you're doing would to have the user either give a 'y' or an 'n' and compare those char in your if statements.
7th Jan 2017, 4:07 PM
Ethan
0
Thank you I finnally figured it out!
7th Jan 2017, 4:38 PM
erik