C ++ Programing Challenge: Complete the code

Kaustubh Katdare

Kaustubh Katdare

@thebigk Oct 25, 2024
Puzzle Source: <a href="https://www.cprogramming.com/complete/guessing.html" target="_blank" rel="noopener noreferrer">C++ Programming Challenge - Guessing Game Challenge - Cprogramming.com</a>

The following program will act as a guessing game in which the user has eight tries to guess a randomly generated number. The program will tell the user each time whether he guessed high or low:
#include <stdlib.h>
#include <iostream>
  using namespace std;  int main()
{
int number=rand()___; 
int
guess=-1;
int trycount=0;
while(guess__number __ trycount<8)
{

cout__"Please enter a guess: ";
cin__guess;
__(guess_number)

cout<<"Too low"<<endl;
 __(guess_number)

cout<<"Too high"<<endl;
_______________;
_

if(guess==number)
cout<<"You guessed the number";
____

cout<<"Sorry, the number was: "<<_____;
______
0;

Those who want to scratch your brain and improve your C++ programming skills - discuss the problems.

Those who need spoon feeding, get the solution at the puzzle source.

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Aug 26, 2008

    I sit and wonder why we don't have a code cracker in our 18,000+ engineers' squad.
  • bayazidahmed

    bayazidahmed

    @bayazidahmed-qg0JR9 Aug 26, 2008

    The_Big_K
    I sit and wonder why we don't have a code cracker in our 18,000+ engineers' squad.
    Naah! Again too easy.
  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Aug 26, 2008

    Ok, bayazidahmed - how about posting the solution and proving your point? 😉
  • bayazidahmed

    bayazidahmed

    @bayazidahmed-qg0JR9 Aug 26, 2008

    The_Big_K
    Ok, bayazidahmed - how about posting the solution and proving your point? 😉
    Well, If you insist.

    #include <stdlib.h>
    #include <iostream>
    using namespace std; int main()
    {
    int number=rand() * 1; /*this blank could be used to scale the number, if we give it as * 1, the max number could be upto STD_MAX defined in stdlib.h which is usually 32767*/
    int
    guess=-1;
    int trycount=0;
    while(guess != number && trycount<8)
    {

    cout<<"Please enter a guess: ";
    cin>>guess;
    if(guess<number)

    cout<<"Too low"<<endl;
    if(guess>number) // here else if can also come in place of if

    cout<<"Too high"<<endl;
    trycount++;
    }

    if(guess==number)
    cout<<"You guessed the number";
    else

    cout<<"Sorry, the number was: "<<number;
    return
    0;
    }//you missed this bracket
  • friendster7

    friendster7

    @friendster7-oVJr9h Aug 26, 2008

    bayazidahmed
    Well, If you insist.

    #include <stdlib.h>
    #include <iostream>
    using namespace std; int main()
    {
    int number=rand() * 1; /*this blank could be used to scale the number, if we give it as * 1, the max number could be upto STD_MAX defined in stdlib.h which is usually 32767*/
    int //error in this line it must be int guess=-1;
    guess=-1;

    int trycount=0;
    while(guess != number && trycount<8)
    {

    cout<<"Please enter a guess: ";
    cin>>guess;
    if(guess<number)

    cout<<"Too low"<<endl;
    if(guess>number) // here else if can also come in place of if

    cout<<"Too high"<<endl;
    trycount++;
    }

    if(guess==number)
    cout<<"You guessed the number";
    else

    cout<<"Sorry, the number was: "<<number;
    return
    0;
    }//you missed this bracket
    ur code would not give a result.
  • bayazidahmed

    bayazidahmed

    @bayazidahmed-qg0JR9 Aug 26, 2008

    friendster7
    ur code would not give a result.

    Too bad, you should have tried compiling and posted your comments. It works perfectly fine for me.

    The syntax error you have pointed out will not come in any compiler.

    For your information, a single C/C++ statement can be spanned on multiple lines. The statement ends with a ; only.
    So, there is nothing wrong in writing
    int guess = -1 as
    int
    guess=-1
    OR
    int



    guess=-1
    OR
    int
    guess
    =-1
    OR
    int
    guess
    =
    -1
  • niraj.kumar

    niraj.kumar

    @nirajkumar-6nokRG Aug 27, 2008

    calm down ahmed 😉
  • bayazidahmed

    bayazidahmed

    @bayazidahmed-qg0JR9 Aug 27, 2008

    niraj.kumar
    calm down ahmed 😉
    Me? No worries. I was just explaining...