Solved

Program problems

Posted on 1998-04-26
5
193 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi Hoodm, what's your question?  Is the program I posted working?
And which 'break' is giving you the error??
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  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 …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now