0

Display variable

How to display variable ? but it's not like that simple one. I mean if we use selection. for example if I use loop and I have selection in the loop, I want to do like if user's statement is true, it will display 'hello' else, it will display 'world' . But, this is loop, so if the user's statement is true and also false, how to display both answer ? or only one of them ?

1st Dec 2018, 5:10 PM
Syed Bob
7 Respostas
+ 4
"if the user's statement is true and also false, how to display both answer ? " This is a contradiction and is not possible. It's like saying I want to have the light on and off at the same time. You can do it in the code, though, but it no longer serves its purpose. if (off) turn it on. else turn it off if (off) turn it on turn it off
1st Dec 2018, 5:35 PM
Babak
Babak - avatar
+ 3
One list is enough for reporting. so x[10], y[10] --> x[20] ~~~~~~ cout<<" enter (1) for Bath OR (2) for Brush OR (3) for both "; As simple as that! ~~~~~~~~ if (code == '1') { strcpy(x,"Bath"); } else if (code ==' 2') { strcpy(x,"Brush"); } else if (code == '3') { strcpy(x,"Bath and Brush"); } ~~~~~ cout << "Service type is: " << x;
1st Dec 2018, 8:31 PM
Babak
Babak - avatar
+ 2
"what if there are a lot of option that user can choose ? more than 10 option ?" There should be a prompt same as before to inform the user about the choices s/he has. cout<<" 1) Bath 2) Brush 3) Toilet 4) Tub 5) Sauna X) Exit"; and it requires you to store the choices in a choice list like so const int number_of_choices = 5; bool choices[number_of_choices + 1]; // {0, choice1, choice2, choice3, choice4, choice5} Next, an efficient way to handle a list of messages would be an array of strings like so string msgList[] = { "Bath", "Brush", "Toilet", "Tub", "Sauna" }; Then, restructuring the code like this #include <iostream> #include <string> using namespace std; int main() { const int number_of_choices = 5; char choice; int choiceToInt; // hold integer equivalent of the choice to access array's elements bool choiceList[number_of_choices + 1] = { false }; // {0, choice1, choice2, choice3, choice4, choice5} string msgList[] = { "", "Bath", "Brush", "Toilet", "Tub", "Sauna" }; cout << "1) Bath \n2) Brush \n3) Toilet \n4) Tub \n5) Sauna \nX) Exit\n"; do { cin >> choice; if (choice == 'x' || choice == 'X') break; else if (choice >= 1 && choice <= 5) choiceToInt = choice - '0'; // choice - '0' --> char to int if (choiceList[choiceToInt] == false) // if the item hasn't already been selected ++choiceList[choiceToInt]; } while (true); cout << "Service type : "; for (unsigned i = 1; i < (number_of_choices + 1); ++i) { if (choiceList[i]) cout << msgList[i] << " "; } } Sample output: 1) Bath 2) Brush 3) Toilet 4) Tub 5) Sauna X) Exit 1 5 x Service type : Bath Sauna
2nd Dec 2018, 6:40 AM
Babak
Babak - avatar
+ 1
wow, thank youu so much. you're very helpful. another question, what if there are a lot of option that user can choose ? more than 10 option ?
2nd Dec 2018, 1:34 AM
Syed Bob
0
let say the code is :- char code, x[10], y[10]; char respond ='y' while (respond=='y') { cout<<" enter (1) for Bath and (2) for Brush "; cin>>code; if (code=='1') { strcpy(x,"Bath"); } else if (code=='2') { strcpy(y,"Brush"); } cout<<" Do you want to continue ? Press 'n' to exit "; } how can I display which service that the user choose ? for example cout<<x<<y; if I do this, it will display ' 0 ' for the one that user don't choose right ? how can I display the service type ( bath or brush ) ? user can only pick bath and also can only pick brush and also can pick both.
1st Dec 2018, 8:18 PM
Syed Bob
0
I want the output to display like this :- Service type : Brush or Service type : Bath or Service type : Bath and Brush
1st Dec 2018, 8:20 PM
Syed Bob
0
thank you so much sir ! You're really good. and also thank you for your time, have a nice day sir ! :)
2nd Dec 2018, 6:44 AM
Syed Bob