Ask coding questions

← Back to all posts
Need Help With break
KASON123 (6)

So im trying to break a no Question and i dont know how to create a loop for this iv done research and everything i cant find a good one

#include <iostream>
using namespace std;

int main() {

int dogs = 3;
int cats = 4;
int yes;
int no;

cout << "There are 3 dogs" << endl;
cout << "There is also 4 cats" << endl;
cout << "Do u want to play a game?" << endl;
cin >> no;

if (no) 
I want my break to be right here or somewhere here but dont know how to
cout << "Bye Bye" << endl;
else 
cin >> yes;


cout << "If you add dogs + cats what would you get" << endl;





}
Comments
hotnewtop
MatthewWesolows (9)

1:

You need to use brackets with your if's, it's a must with readability.

2:

use std::string for instead of int for the no and yes varables. Just write #include<string> at the top of your program to include it.

3:

If you want to break when the user enters no, just do
if (no == "no") { return 0; }

4:

to put it in a loop, copy this inside of your main function (don't forget to write #include<string>:

 int dogs = 3;
 int cats = 4;
 std::string yes;
 std::string no;
 int answer = 7;

while(true) {
 cout << "There are 3 dogs" << endl;
 cout << "There is also 4 cats" << endl;
 cout << "Do u want to play a game?" << endl;
 cin >> no;

 if (no)
  {
   cout << "Bye Bye" << endl;
   return;
 }
 else 
  {cin >> yes;}


 cout << "If you add dogs + cats what would you get" << endl;

 cout << dogs + cats;
 cin >> answer;

 if (7)
  {cout << "good job you got it " << endl;}
}

I can't compile and run this at the moment so let me know if any errors show up or if you have any questions. You also seem to not know too much about the language. I think you should look for some free tutorials and learn more. Then come back and try to improve your program.