3 Replies - 280 Views - Last Post: 05 March 2013 - 09:00 PM Rate Topic: -----

#1 weatherfordcurt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-March 13

Number Guessing Game Errors

Posted 05 March 2013 - 02:00 PM

I'm working on an assignment for my Computer science class that requires me to:

Write a C++ program that plays the game of “guess the number”.
First, your program chooses a number at random (using the rand function ) in the range 1 to 100.
Second, the program then displays the following:

I have a number between 1 and 100
Can you guess my number?
Please type your guess.

Then the player types a guess. The program calls the check function

bool check (int theNumber, int userGuess);
The check function should display one of the following:

Option 1: Excellent! You guessed the number!
Option 2: Too Low. Try Again.
Option 3: Too High. Try Again.

The check function will return true if the user guessed the number and will return false otherwise.
Your program should continue looping as long as the player guess is not correct and tell the player Too High or Too Low to help the player “zero in “ on the correct answer.


Below is the code that I have so far. From I know it is all correct but when I try to build it I get this error message and I do not know how to correct it:

1>------ Build started: Project: Lab 6 Exercise 2, Configuration: Debug Win32 ------
1>Build started 3/5/2013 2:58:21 PM.
1>InitializeBuildStatus:
1> Touching "Debug\Lab 6 Exercise 2.unsuccessfulbuild".
1>ClCompile:
1> exercise 2.cpp
1>c:\users\admin\desktop\cs155\lab 6 exercise 2\lab 6 exercise 2\exercise 2.cpp(20): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>exercise 2.obj : error LNK2019: unresolved external symbol "bool __cdecl check(int,int)" (?check@@YA_NHH@Z) referenced in function _main
1>C:\Users\Admin\Desktop\CS155\Lab 6 Exercise 2\Debug\Lab 6 Exercise 2.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.97
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

/*  ----------------------------------------------------------- 
Curtis Weatherford
Section 1
Lab 6
Exercise 2
-------------------------------------------------------------  */

#include<iostream>
#include<time.h>  //This is included because I am using a rand()
using namespace std;

bool check(int the_Number, int user_guess);

int main()
	
{
	int unknown;
	int guess;
	
	srand ( time(0) ); 
	unknown=rand() % 100 + 1; //generate a random number between 1 and 100
	
	cout<<unknown<<endl;
	cout<<"I have a number between 1 and 100."<<endl;
	cout<<"Can you guess my number?"<<endl;
	cout<<"Please typer your guess :"<<endl;
	
	do
	{
	
		cin>>guess;
		check(unknown, guess);
	
	}while(check(unknown,guess) != true);
	
	return 0;
}

bool validate(int the_Number, int user_guess)
{
	if(user_guess == the_Number)
	{
		cout<<"Excellent! You guessed the number!"<<endl;
		return true;
	}

	else
	{
		if(user_guess > the_Number)
		{
			cout<<"Too High. Try Again"<<endl;
		}

	else if(user_guess < the_Number)
		{
			cout<<"Too Low. Try Again"<<endl;
		}
	return false;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Number Guessing Game Errors

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,883
  • Joined: 25-December 09

Re: Number Guessing Game Errors

Posted 05 March 2013 - 02:12 PM

Where did you implement the function check()? You implemented a function with the name of bool validate() but no function named check().

Jim

This post has been edited by jimblumberg: 05 March 2013 - 02:13 PM

Was This Post Helpful? 1
  • +
  • -

#3 weatherfordcurt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-March 13

Re: Number Guessing Game Errors

Posted 05 March 2013 - 02:20 PM

That was it I changed validate to check and it works. There are still a few problems with the output but I can fix that. Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Number Guessing Game Errors

Posted 05 March 2013 - 09:00 PM

Well, you will notice that you will be getting 2 outputs for a single input value...The reason for that is because you are calling the check/validate function twice.

Instead, you could just comment out line 32 in your program to get a single output value.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1