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

#include <iostream>
#include <cstdlib>
#include <ctime>

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 < 10) && (!done))

    {
        cout << "Enter an integer greater"
            << " than or equal to 0 and "
            << "less than 1000: ";
        cin >> guess;
        cout << endl;
        noOfGuesses++;
        if (guess == num)
        {
            cout << "you guessed the correct "
                << "number." << endl;
            done = true;
        }
        else
            if (guess < num)
                cout << "Your guess is lower "
                << "than the number. \n"
                << "Guess again!" << endl;
            else
                cout << "Your guess is higher "
                << "than the number.\n"
                << "guess again!" << endl;
            cout <<"Total gueses equal " << noOfGuesses << endl;



    }
    return 0;
    }

Recommended Answers

All 13 Replies

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

commented: great help +1

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

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

commented: great help +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

> 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!.

commented: the information was great. +1

thank you for your help. i really appreciate it

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.

#include <iostream>
#include <cstdlib>
#include <ctime>

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 < 10) && (!done))

    {
        cout << "Enter an integer greater"
            << " than or equal to 0 and "
            << "less than 1000: ";
        cin >> guess;
        cout << endl;
        noOfGuesses++;
        if (guess == num)
        {
            cout << "you guessed the correct "
                << "number." << endl;
            done = true;
        }
        else
            if (guess < num)
                cout << "Your guess is lower "
                << "than the number. \n"
                << "Guess again!" << endl;
            else
                cout << "Your guess is higher "
                << "than the number.\n"
                << "guess again!" << endl;
            cout <<"Total gueses equal " << noOfGuesses << endl;
            cout << "you played " << noofgamesplayed << " games";



    }
    return 0;
    }

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

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

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.

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.

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

>"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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.