need solution for my script

i think my script is pretty okay ( i know its dutch)
you need to guess the number between 0 and 20 and you have 4 try's if you guessed it it wil return 0; but i want to give the option to start over again. i already implemented it in the script if you did not guess the number you will be given the option. but i cant find any solution for it after the sentence
if (a == b) {cout <<"goed geraden"; return 0;}
thanks for your help.


#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <sstream>
#pragma once
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
srand(time(NULL));
int a;
string c;
int b = rand()%21;
do{

cout <<"vul getal in tussen 0 en 20: ";
cin >>a;
if (a>=0&&a<=20){
if (a == b) {cout <<"goed geraden"; return 0;}
else if (a >= b) {cout <<"het is lager\n";}
else cout <<"het is hoger\n";}
else cout <<"onder de 20!!\n";
cout <<"nog 3 levens over\n"<<"vul getal in tussen 0 en 20: ";
cin >>a;
if (a>=0&&a<=20){
if (a == b) {cout <<"goed geraden"; return 0;}
else if (a >= b) {cout <<"het is lager\n";}
else cout <<"het is hoger\n";}
else cout <<"onder de 20!!\n";
cout <<"nog 2 levens over\n"<<"vul getal in tussen 0 en 20: ";
cin >>a;
if (a>=0&&a<=20){
if (a == b) {cout <<"goed geraden"; return 0;}
else if (a >= b) {cout <<"het is lager\n";}
else cout <<"het is hoger\n";}
else cout <<"onder de 20!!\n";
cout <<"nog 1 leven over\n"<<"vul getal in tussen 0 en 20: ";
cin >>a;
if (a>=0&&a<=20){
if (a == b) {cout <<"goed geraden"; return 0;}
}
else cout <<"onder de 20!!\n";
cout <<"je bent af ";
cout <<"het getal was: "<<b;
cout <<"\n nog een potje? ja of nee: ";
cin >>c;

}while (c=="ja");


}
You have
1
2
3
4
int main() {
  // code
  // code you want to repeat
}

Move that something to a separate function:
1
2
3
4
5
6
7
8
void guessinggame() {
  // code you want to repeat
}

int main() {
  // code
  guessinggame();
}

That makes the main() look simpler and you can focus there on the "start over and do again". The content of guessinggame() is not related to repeating it.
Topic archived. No new replies allowed.