Solved

Program problems

Posted on 1998-04-26
5
206 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
5 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 50 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

685 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