Thread: First program. Need help.

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    Question First program. Need help.

    I know this is one of the simplest programs ever, but I need help because I'm new to C++.

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int num;
    	int inputnum;
    	num = rand()%101;
    	cout<<"Pick a number between 0-100"<<endl;
    	cin>>inputnum;
    	if(inputnum == num)
    	{
    		cout<<"Your right! The number is "<<num<<endl;
    	}
    	while(inputnum > num)
    	{
    		cout<<"Try a little lower"<<endl;
    		cin>>inputnum;
    	}
    	while(inputnum < num)
    	{
    		cout<<"That's too low."<<endl;
    		cin>>inputnum;
    	}
    	return 0;
    }
    1st problem: it picks 1 number then it's the same all the time
    2nd problem: after 3 guesses it closes

    the compiler I'm using is VC++

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    it picks 1 number then it's the same all the time
    You need to seed the rand function.
    See here for details
    Last edited by C_Coder; 02-24-2002 at 05:37 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    Ok thanks, now I have another question since it's now finished and I want to modify it. How would you have a high score stored?

  4. #4
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb Try this...

    Look up fstream. It lets you read/write from/to files.

    [EdIt]

    Also, #include <time.h> and call:


    srand((unsigned) time (NULL));

    to randomize the rand() every time.

    [/EdIt]
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Originally posted by Magriep
    Ok thanks, now I have another question since it's now finished and I want to modify it. How would you have a high score stored?
    Do you mean number of guesses? Keep track of them in a variable like count, after the number is guessed set another variable like highScore = count. On subsequent loops compare count to highScore, reset it if higher.
    You might want to look at the logic flow in your program. I think it'll only work in some situations. Compare all values using if statements in a while loop. It closes after three guesses because you're not doing that. Run through it on paper and see.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    here's my current code:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h> 
    #include <conio.h>
    int rand(void);
    
    int main()
    {
    	int num;
    	int inputnum;
    	srand(time(NULL));
    	num = rand()%101;
    	cout<<"Pick a number between 0-100"<<endl;
    	cin>>inputnum;
    	bool loop = true;	
    	while( loop )	
    	{
    		if(inputnum == num)
    		{
    			cout<<"Your right! The number is "<<num<<endl;
    			getch();
    			return 0;
    		}
    		while(inputnum > num)
    		{
    			cout<<"Try a little lower"<<endl;
    			cin>>inputnum;
    		}
    		while(inputnum < num)
    		{
    			cout<<"That's too low."<<endl;
    			cin>>inputnum;
    		}
    	}
    	return 0;
    }
    Where would I store a high score, and if it's in a file, couldn't someone easily edit it?

  7. #7
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    hide it within a bunch of numbers or somthing clever like that

  8. #8
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    And it's "you're", not "your".

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