954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

guessing game program problem

i have been fooiling around with my program for class trying to get it to work, but now i get this error message:
error C2064: term does not evaluate to a function taking 1 arguments
it is referring to this part of the program:
num = (rand() + time(0)) % 1000;
could anyone explain what i did wrong and how i can fix it. thank you
here is the program. i have been having problems doing this correctly so i hope this works

<code>#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;ctime&gt;

using namespace std;

int main ()
{
	int num;
	int guess;
	bool done;
	int noOfGuesses=0;
	int ncount;
	int sum=0;
	int noofgamesplayed;
	int avgNoOfGuesses;
	int time;

	 sum += noOfGuesses;
	 avgNoOfGuesses=sum/noofgamesplayed;

	num = (rand() + time(0)) % 1000;
	done = false;
	while ((noOfGuesses &lt; 10) &amp;&amp; (!done))
	
	{
		cout &lt;&lt; &quot;Enter an integer greater&quot;
			&lt;&lt; &quot; than or equal to 0 and &quot;
			&lt;&lt; &quot;less than 1000: &quot;;
		cin &gt;&gt; guess;
		cout &lt;&lt; endl;
		noOfGuesses++;
		if (guess == num)
		{
			cout &lt;&lt; &quot;you guessed the correct &quot;
				&lt;&lt; &quot;number.&quot; &lt;&lt; endl;
			done = true;
		}
		else
			if (guess &lt; num)
				cout &lt;&lt; &quot;Your guess is lower &quot;
				&lt;&lt; &quot;than the number. \n&quot;
				&lt;&lt; &quot;Guess again!&quot; &lt;&lt; endl;
			else
				cout &lt;&lt; &quot;Your guess is higher &quot;
				&lt;&lt; &quot;than the number.\n&quot;
				&lt;&lt; &quot;guess again!&quot; &lt;&lt; endl;
			cout &lt;&lt;&quot;Total gueses equal &quot; &lt;&lt; noOfGuesses &lt;&lt; endl;


		
	}
	return 0;
	}</code>
dblbac
Light Poster
40 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

It looks like you have a couple of things mixed up. There was just a great thread on using random numbers. before you use rand() you will want to seed the generator with a random variable first. That is done by using

srand((unsigned)time(0))

Use that before you use rand(); and then just use

rand() % 1000;


here is the link to the Thread on random numbers: http://www.daniweb.com/forums/thread1769.html

joshua.tilson
Light Poster
47 posts since Nov 2007
Reputation Points: 13
Solved Threads: 0
 

i put it in but i don't know if i did it correctly. now i have a an error that says:
error C2064: term does not evaluate to a function taking 1 arguments. it is talking about this line:
srand((unsigned)time(0))
what did i do wrong? please let me know if you can

dblbac
Light Poster
40 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

delete int time then use srand((unsigned)time(0))

hgedek
Newbie Poster
10 posts since Nov 2007
Reputation Points: 11
Solved Threads: 1
 

thanks, i fixed that, now i have a debug error which says that the variable 'noofgamesplayed' is being used without being defined. how can i fix that. i am so confused
thanks for your help

dblbac
Light Poster
40 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

> int noofgamesplayed; noofgamesplayed++;
Is using it without it being defined. cout << "You played " << noofgamesplayed << " games";
Is also using it without it being defined. int noofgamesplayed = 0;
Now we're cooking!.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

thank you for your help. i really appreciate it

dblbac
Light Poster
40 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

i have been working on this program for the last month and it is driving me nuts. i have been listening to all the advice you guys have been giving me and somehow i keep getting a debug error. could someone pleeeeaaasssseee look over what i have and tell me how or what i have to fix. the program should ask (at the end of the guesses or when they get the answer) if they want to play again and react accordingly. it should also display some summary information including number of games played, number of correct guesses and average number of guesses it took to get the right answer. i really need this project to work. it is for half my grade. any help would be greatly appreciated. here is what i have.

<code>#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;ctime&gt;

using namespace std;

int main ()
{
	int num;
	int guess;
	bool done;
	int noOfGuesses=0;
	int ncount;
	int sum=0;
	int noofgamesplayed;
	int avgNoOfGuesses; 
	noofgamesplayed++;

	

 sum += noOfGuesses;
	 avgNoOfGuesses=sum/noofgamesplayed;
	srand((unsigned)time(0))
	;num = (rand() % 1000);
	done = false;
	while ((noOfGuesses &lt; 10) &amp;&amp; (!done))
	
	{
		cout &lt;&lt; &quot;Enter an integer greater&quot;
			&lt;&lt; &quot; than or equal to 0 and &quot;
			&lt;&lt; &quot;less than 1000: &quot;;
		cin &gt;&gt; guess;
		cout &lt;&lt; endl;
		noOfGuesses++;
		if (guess == num)
		{
			cout &lt;&lt; &quot;you guessed the correct &quot;
				&lt;&lt; &quot;number.&quot; &lt;&lt; endl;
			done = true;
		}
		else
			if (guess &lt; num)
				cout &lt;&lt; &quot;Your guess is lower &quot;
				&lt;&lt; &quot;than the number. \n&quot;
				&lt;&lt; &quot;Guess again!&quot; &lt;&lt; endl;
			else
				cout &lt;&lt; &quot;Your guess is higher &quot;
				&lt;&lt; &quot;than the number.\n&quot;
				&lt;&lt; &quot;guess again!&quot; &lt;&lt; endl;
			cout &lt;&lt;&quot;Total gueses equal &quot; &lt;&lt; noOfGuesses &lt;&lt; endl;
			cout &lt;&lt; &quot;you played &quot; &lt;&lt; noofgamesplayed &lt;&lt; &quot; games&quot;;


		
	}
	return 0;
	}</code>
dblbac
Light Poster
40 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

where are you facing the problem...???

tracethepath
Junior Poster in Training
54 posts since Jul 2007
Reputation Points: 8
Solved Threads: 4
 

when i go to debug it gives me a warning:
run time check #3: the variable 'noofgamesplayed' is being used without being defined. i have been getting this message constantly. also when i build the solution, i get 2 warnings:
warning C4101: 'ncount' : unreferenced local variable
and
warning C4700: uninitialized local variable 'noofgamesplayed' used
I am at my wits end. any help is appreciated

dblbac
Light Poster
40 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Think about using some functions.

int main ( ) {
  playSomeGames();
  return 0;
}

void playSomeGames( ) {
  int numGamesPlayed = 0;
  do {
    playAGame();
    numGamesPlayed++;
    // prompt for "play again" and read answer
  } while ( again );
  cout << "You played " << numGamesPlayed << " games";
}

void playOneGame ( ) {
  int numGuesses = 0;
  // generate a target value
  do {
    // prompt for guess etc
  } while ( numGuesses < 10 );
  cout << "You took " << numGuesses << " to guess";
}

Each function is short (<20 lines say), and performs a very specific task. Indeed, main could just call playOneGame() to begin with, so you can test that function, then switching over to playSomeGames() is a one-line change in main.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

don't think he's reached functions yet. Your errors are syntax. Look at something like this:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>
using namespace std;


int main()
{
	srand ( static_cast<unsigned int> ( time ( 0 ) ) );

  int randNum = rand() % 100 + 1;
  int num = 0;
  int GuessLow=0;    
  int GuessHigh = 0;
  int guess=0;
  string uname;
  
  cout<<"Please enter your name: ";
  getline ( cin, uname );
  cout<< uname <<'\n';
  cout<<"Welcome to the Random Number Guessing Game!!\n";

  
    {
        do
        {
            // Loops until user finds the number
            
            cout <<uname<<" "<<"please enter a number between (#1 - 100): ";
            cin >> num; // User's input of guessed number
			guess++;
			            
            if (num < randNum) // If num is < than the random number, inform player to try again.

               cout <<uname<<" "<<"I'm sorry-your number was too low-please try again!";GuessLow++;
			   
			 else
            
            if (num > randNum) // If num is > than the random number, inform player to try again.
               cout<<uname<<" "<<"I'm sorry-your number was too high-please try again!";GuessHigh++;
			
				
			
            else
            
            break;
            
        } while (num != randNum);
		
		
		
    
    
        cout<<uname<<endl;
        cout<<" "<< "Excellent Job!! You guessed correctly!!"<< endl;//Informs if the player is successfull
        cout << "Guessed too high: " << GuessHigh;//Display the user's score at the end of the program
		cout << "Guessed too low: " << GuessLow;//Display the user's score at the end of the program
		cout<<  "Total number of guesses: "<<guess;//Display the # of guesses by user
        } 


	return 0;
}

This ought to work...there may be a few errors in it...you ought to modify it o fit the requirements of your assignments. Hope this helps.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

Hold up...........why are all these people double posting.....or is there a bug on my PC?...thought i just answered the question.....

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

>"run time check #3: the variable 'noofgamesplayed' is being used without being defined"

somebody gave you the answer to that already.
In general It's a good Idea to set things equal to zero when you initialize them.
meaning

int x=0;
is better than
int x;

If you don't set it equal to zero at the beginning it'll be set to a large negative number.

maxmaxwell
Newbie Poster
16 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You