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.
// Copyright © 2020 Donnie Starkey. All rights reserved.
//

#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!”

}

prefromatted_text-animated

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 :slight_smile:

#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';
  
}

If you have any more questions, please let us know so we can help you! :slight_smile:

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)
            {
                std::cout<<"your guess is too low"<<'\n';
            }
        else if (number<guess)
        {
            std::cout <<"your guess is too high!"<<'\n';
        }
    varA++;
    }
    while (varA <=5);
        std::cout<<"thanks for playing"<<'\n';
}


1 Like