# Simple game from Ivan's C++ video

Ivan asked us to build a guessing game based on the knowledge that we have. I worked hard, searching to find a solution. All roads may lead to Rome, but my solution did not produce the exact result as Ivan’s did. In fact, it didn’t even do what I wanted it to do.
I have two questions:

1. I spent too much time trying to hide the number that player one put into the system. Is that possible at my beginner level? If so, is there a tutorial that you can link?
2. As you will see, I chose a “do while” loop, and wanted five guesses. It iterated the false answers twice, then ended the loop. I tried moving the curlies around, but this didn’t work. Any help??

Here’s the code:
// Created by Donnie Starkey on 9/10/20.
//

#include
#include <stdlib.h>
int main(int argc, const char * argv[])

{

int number {};

std::cout <<“give me a number”<< ‘\n’;
std::cin>> number;

int guess{};
do {
std::cout<<“guess the number!”;
std::cin>> guess;
if (number==guess)
{
std::cout<< “good job!!!”;
}
else if (number>guess)
{
std::cout<<"your guess is too low " <<’\n’;
}
else if (number<guess)
{
std::cout<< “your guess is too high!!” <<’\n’;
}

} while (guess <5);
guess++;

}

Here’s the code:

Remember you can use the “Preformatted Text” Button to encapsulate any kind of code you want to show.

function formatText(){

let words = “I’m a preformatted Text box, Please use me wisely!”

}

Carlos Z.

1 Like

got it, sorry here it is again:

type or#include <iostream>
#include <stdlib.h>
int main(int argc, const char * argv[])

{

int number {};

std::cout <<"give me a number"<< '\n';
std::cin>> number;

int guess{};
do {
std::cout<<"guess the number!";
std::cin>> guess;
if (number==guess)
{
std::cout<< "good job!!!";
}
else if (number>guess)
{
std::cout<<"your guess is too low " <<'\n';
}
else if (number<guess)
{
std::cout<< "your guess is too high!!" <<'\n';
}

} while (guess <5);
guess++;

}

It is possible but not at a beginner level, you might need to use some complex functions on c++.

I change some few things on your code to make it work, you got an error on your do while structure, remember this is the basic structure that you should always keep in mind.

int varA = 0;
do{
...
...
varA++;
}while(varA <= 5);

Here is your code fix it. Try to understand what i did there, if any question let me know

#include <iostream>
#include <stdlib.h>
int main(int argc, const char * argv[])
/*
I have two questions:

I spent too much time trying to hide the number that player one put into the system. Is that possible at my beginner level? If so, is there a tutorial that you can link?
As you will see, I chose a “do while” loop, and wanted five guesses. It iterated the false answers twice, then ended the loop. I tried moving the curlies around, but this didn’t work. Any help??
*/
{

int number {};
//declaration of tries variable
int guess = 0;
std::cout <<"give me a number"<< '\n';
std::cin>> number;

do {
std::cout<<"guess the number!";
std::cin>> guess;
if (number==guess)
{
std::cout<< "good job!!!"<<'\n';
//break the loop, if answer is correct
break;
}
else if (number>guess)
{
std::cout<<"your guess is too low " <<'\n';
}
else if (number<guess)
{
std::cout<< "your guess is too high!!" <<'\n';
}
//each try, +1
guess++;
} while (guess <= 5); //guess equal or greater to 5 tries
std::cout<< "thanks for playing" <<'\n';

}

Carlos Z.

1 Like

I got it! Thanks for your help!!!

#include <iostream>
#include <stdlib.h>

int main(int argc,const char * argv[])
{
int number = 0;
int guess =0;
int varA =0;
std::cout<<"give me a number"<<'\n';
std::cin>> number;
do {
std::cout<<"guess the number!";
std::cin >> guess;
if (number==guess)
{
std::cout<<"Good Job!"<<'\n';
break;
}
else if (number>guess)
{
}
else if (number<guess)
{
std::cout <<"your guess is too high!"<<'\n';
}
varA++;
}
while (varA <=5);
std::cout<<"thanks for playing"<<'\n';
}

1 Like

Hello I just finished the little game and I have a problem

#include <iostream>

using namespace std;

int main()
{
/*

The Rules:

1) Player 1 selects a random number
2) Player 2 needs to guess the number
3) The game will tell Player 2 whether the guess is too low or too high
4) the game will count the number of attempts player 2 made
*/

int Player[1];
int attempt = 0;
cout<<"Player 1 select a random number: "<<endl;
cin>>Player[0];
system("CLS");
while(true){
attempt++;
cout<<"Player 2 guess the number: ";
cin>>Player[1];
cout<<attempt<<endl;
if(Player[0]< Player[1]){
cout<<"your guess is above the number of Player1"<<endl;
}
else if(Player[0]> Player[1]){
cout<<"your guess is below the number of Player1"<<endl;
}
else if(Player[0]== Player[1]){
cout<<attempt;
break;

}
}
cout<<"congrats you found the number in "<<attempt<<" try"<<endl;
return 0;
}

With this code I obtain at the end, if I enter as the player 1 the number 12, my number of try will be 12. I don’t understand why. Is it a problem of memory or something like that?

You initialized the Player array length with size 1, accessing the next element in the array will override the next variable in the stack, which is attempt that you initialized right after the array

Thanks for your answer. I didn’t understand all of it. When you say that when I try to access the next element in the array it will override the next variable, I don’t find the moment where I try to access the element 2 of the array. If I edit Player[2] I understand that it will replace attempt by Player[2] but I don’t try to edit Player[2] or I don’t see where.

Player 2 has index 1 (Player[1]). But you have to initialize the array with two elements (int Player[2]). Might be a bit confusing at first because you visually do the same thing, but in fact you are doing two different things here:

One is you access the second element in the array Player
and second is you initialize the Player array to hold 2 elements.

1 Like

Hi!!! I also did this homework … if anyone can use this code, I will leave it here … its also in github I supose, Its the first time for me using Xcode, but I guess that thing is connected to github from the biggining … tihs can certainly be improved to handle some cases:

1.using entering double or float or strings

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main() {
// insert code here...
//player 1 selects a random number
//player 2 guess number
//game will tell if guess is too high, too low or correct
//the game needs to keep track of how many attempts player 2 has made
srand((unsigned)time(0));
int player1roof{};
cout<<"tell me an int number, which will be up to the number than I can choose:"<<endl;
cin>>player1roof;
int player1 = (int)((rand()%player1roof)+1);
cout<<"ok, now I will chose a number between 1 and "<<player1roof<<endl;
int guesses{};
int player2{};
while(player1!=player2){
cout<<"Time to guess!!. You have guessed "<<guesses<<" times so far. Go ahead:"<<endl;
cin>>player2;
if(player1==player2) {
guesses++;
break;
}
else if(player1<player2){
cout<<"Try a little lower next time"<<endl;
++guesses;
} else if(player1>player2) {
cout<<"Try a litte higher next time"<<endl;
++guesses;
}
}
cout<<"Congratulations!!! You have finally guessed. You took "<<guesses<<" times to guess"<<endl;
return 0;
}

Hey, its possible that xcode has git integration, but you still have to create and configure the repo first to be able to push to gitlab.

1 Like