?
Solved

Program problems

Posted on 1998-04-26
5
Medium Priority
?
219 Views
Last Modified: 2010-04-10
This is my first programming class.  I'm a beginner.  This is an extremely basic program that I must be making stupid mistakes with and need some help.  This program is supposed to be a "Guess my number" game.  The player should have a maximum of 5 guesses and it should let him know after his guess whether the next guess is higher or lower.  It should also allow the player to play as many games as he wants.  Can you help me figure out what is wrong with this program as I've written it.  Here is all the code.  I've commented out some of the major trouble maker code to figure things out better.  Thanks in advance!

#include<iostream.h>
void main(void)
{

int sym,guess;

cout<<"Thanks for playing Guess My Number\n";
cout<<"You will get 5 total guesses.\n";
cout<<"Enter your first guess.  My number is between 1 and 10\n";
cin>>sym;
guess=1;
while (guess<5)
{
      if(sym>6)

      cout<<"Too high!  The number is lower!\n";
            
      else if (sym<6)

            cout<<"Too low!\n:";

       else if (sym==6)
            cout<<"You've guessed my number!  Would you like to play again, y or n?";

      cout<<"Enter another number\n";      
      cin>>sym;
      guess++;
      
}

}
      //char a;
      //      cin>>a;
      //if (a=='n')
      //      cout<<"End of program!\n";
      //else if (a=='y')
      //continue;
      
      //if(guess==5)
      //      cout<<"That was 5 guesses.  Would you like to try again?\n";
      //      cin>>a;

      //switch (a)
      //{
      //case 'n':cout<<"End of program!\n";break;
      //case 'y':cout<<"Enter another number\n!";continue;
      //}
0
Comment
Question by:hoodm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 100 total points
ID: 1164249
Let me know if this works:
----------------------------------------------------------

#include<iostream.h>

void main(void)
{

    int sym,guess;
    char res;

    do {

        cout<<"Thanks for playing Guess My Number\n";
        cout<<"You will get 5 total guesses.\n";
        cout<<"Enter your first guess.  My number is between 1 and 10\n";
        cin>>sym;
        guess=1;

        while (guess<5)
        {
            if(sym>6)
                cout<<"Too high!  The number is lower!\n";
            else if (sym<6)
                cout <<"Too low!\n:";  
            else if (sym==6) {
                cout << "You've guessed my number!\n";
                break;
            }
            cout<<"Enter another number\n";
            cin>>sym;
            guess++;
        }

        if (guess == 5)
          cout<<"That was 5 guesses. ";
        cout << "Would you like to play again, y or n?";
        cin >> res;
 
  } while (res == 'y');
 
    cout<<"End of program!\n";break;
}
 

0
 
LVL 22

Expert Comment

by:nietod
ID: 1164250
You might want to make the number to be guessed for random.  Otherwise the game will get boring.  (Actually I suspect it will be boring anyway.)
0
 

Author Comment

by:hoodm
ID: 1164251
It is a boring program.  When I learn about random number
generators in the future, maybe I'll ad it.

Thanks for your help.  I had considered using a do loop before,
but thought I could just do it with whiles and ifs.  Also, I thought that I could use continue instead of break.  It did give me an error for the break after the last while loop.  I'm not
sure why...?  W/o the break it works fine otherwise.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1164252
You can just do it with just whiles and ifs.
And you can use continue.  But, like a break, it has to done from inside the loop you're continueing or breaking, not after it.
(And certainly not outside of the entire function)

0
 
LVL 3

Expert Comment

by:q2guo
ID: 1164253
Hi Hoodm, what's your question?  Is the program I posted working?
And which 'break' is giving you the error??
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question