Help with a code Please! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help with a code Please!

***The loop keeps going and I don't know how to get it to stop. I'm trying to get it to ask a question: How are you feeling today on a scale of 1-10?, then the user inputs the data. If it's over 7 then the program ends with a message. If it's less than 7, then it asks a question. You input a response. Then the program restarts.*** How would I do this? I'm still very new to C++. #include <iostream> using namespace std; int main() { int feeling; int response; while(feeling <= 7) { cout <<"How are you feeling today on a scale of 1-10?\n"; cin >> feeling; if (feeling >= 7) { cout << "Way to go! You're having a great day!\n"; } else { cout << "What can you do to make your day better?\n"; cin >> response; cout << "That's a good a idea.\n"; } } return 0; }

28th Jun 2018, 7:19 PM
Christina Camacho
Christina Camacho - avatar
6 Answers
+ 4
The code appears to work. You must enter all of your expected input prior to the start of your code because it runs on a remote server. For example: 3 5 4 6 8 for 3 feeling (3, 4, 8) and 2 response inputs (5 & 6).
28th Jun 2018, 7:53 PM
John Wells
John Wells - avatar
+ 4
response is an integer. You must change to string. That requires some skipping to the next input line plus getting multiple words for your string isn't done with cin. Use: getline(cin, response); Do it twice, first finishes the integer line and second gets your response.
28th Jun 2018, 9:14 PM
John Wells
John Wells - avatar
+ 4
Also, add: #include <string>
28th Jun 2018, 9:16 PM
John Wells
John Wells - avatar
+ 3
Updated your code earlier. Figured I would let you decide to try coding yourself or peek and see my changes. https://code.sololearn.com/cU5VdT0e4vvE
29th Jun 2018, 2:23 AM
John Wells
John Wells - avatar
+ 1
I want it to have a back and forth for example: How are you feeling today on a scale of 1-10? 5 What can you do to make your day better? IDK That's a good a idea. How are you feeling today on a scale of 1-10? 2 What can you do to make your day better? Eat ice cream That's a good a idea. How are you feeling today on a scale of 1-10? 7 Way to go! You're having a great day! /*program ends*/
28th Jun 2018, 9:03 PM
Christina Camacho
Christina Camacho - avatar
+ 1
oh. I havent gotten that far in the lessons. Ill look into it. thanks!
28th Jun 2018, 11:02 PM
Christina Camacho
Christina Camacho - avatar