Thread: My first program

  1. #1
    Registered User
    Join Date
    Feb 2003

    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:

    #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;
    		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;
    		srand((unsigned int) seconds);
    		int right;
    			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
    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
    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
    You should change your header files to the standard ones.
    #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


    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, 01: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, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM