My first program

This is a discussion on My first program within the C++ Programming forums, part of the General Programming Boards category; Hi. I just started learning C++ last night. I've messed around a bit with just the cout and variable stuff, ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    2

    My first program

    Hi. I just started learning C++ last night. I've messed around a bit with just the cout and variable stuff, and today I've been trying to write a number guessing game. Here's what I have so far:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    void main ()
    {
    	int maxnum;
    	cout<<"\nHey, this is the Number Guessing Game Thing";
    	cout<<"Press control-C to close this program at any time.\n\n";
    
    	char plagain='y';
    	int right=0;
    		do
    		{
    
    		cout << "What number and 1 would you like to guess between?\n";
    		cin >> maxnum;
    
    
    
    		int guess;
    		cout<< "Ok, I'm now thinking of a number between 1 and " << maxnum << ". Try to guess it.\n";
    
    		const int LOW=1;
    		const int HIGH=maxnum;
    		int number;
    
    		time_t seconds;
    		time(&seconds);
    		srand((unsigned int) seconds);
    		number=rand()%(HIGH-LOW+1)+LOW;
    
    		cin>>guess;
    
    
    		int right;
    			do
    			{
    			if(guess==number) {cout<<"You got it.";
    			cout << "\nWould you like to guess again? (y/n)\n"; cin>>plagain;
    			cout<<"\nOk. ";right==1;}
    			else if(guess>number) {cout<<"Too high. Guess again.\n";cin>>guess;right=0;}
    			else if(guess<number) {cout<<"Too low. Guess again.\n";cin>>guess;right=0;}
    
    			}while (right==0);
    
    
    		} while (plagain=='y');
    }
    It works fine except if you guess wrong ever, and then you get it right, when it asks you if you want to play again it says "Ok. You got it. Would you like to play again?" and then no matter what you type or whatever it just keeps saying that. What am I doing wrong?

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Code:
    cout<<"\nOk. ";right==1;}
    
    // should be
    
    cout<<"\nOk. ";right=1;}
    And if you'd like to enjoy your stay here, never used void main() And this is just my opinion, but I think your format could use a bit of cleaning. Two statements on one line??
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    2
    Ok, I did clean it up and used int main()/return 0. Thanks for your help!

  4. #4
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    You should change your header files to the standard ones.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    Congratulations! Now you're conforming(sp?) to the ISO/ANSI C++.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    2

    Post

    You are actually running a infinite loop after you finish first processing of the input!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21