Solved

Program problems

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

635 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