Solved

Program problems

Posted on 1998-04-26
5
209 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
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.

751 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