Advertisement

Noob Here, need help with "Guess My Number"

Started by June 29, 2005 11:45 AM
29 comments, last by Washu 10 years, 3 months ago
Quote:
Original post by chris_bell
1. I need to have the user input an integer number to be guessed

I don't think that's necessary. Wouldn't it make more sense to let the computer guess and each try you say if the number is higher or lower? If two humans played the game together it would seem odd to me if the first step is telling the other one the number he has to guess...
Quote:
Original post by Fred304
Quote:
Original post by chris_bell
1. I need to have the user input an integer number to be guessed

I don't think that's necessary. Wouldn't it make more sense to let the computer guess and each try you say if the number is higher or lower? If two humans played the game together it would seem odd to me if the first step is telling the other one the number he has to guess...


Quoted for not actually missing the point.

The game goes like this:

- Output welcome message, and ask user to *think of* a number.- Decide on an initial guess.- Repeat:  - Increment count of guesses.  - Output the current guess and ask the user to indicate if it is high, low or correct.  - If guess was correct, we're done and can break out of this loop.  - Otherwise, modify the current guess (and loop again).- Output the count of guesses and terminate.
Advertisement
Quote:
Original post by CJWR
i don't know if this is what is causing your error or not, but you really should have a return 0; at the very end of the int main() function.

vc++ 6 is just giving me a warning about it, but other compilers might not allow it.


VC++ 6 is non conforming in this respect the standard has an exception for main stating that you don't need to return a value from it, if you omit the return statement 0 is implicitly assumed and your program is still conforming.

main is the only exception though so all other functions declared to return values must do so.

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Quote:
Original post by chris_bell
Can anyone figure out what's wrong with this program? Closes immediately after user enters "theNumber".
I tried using system("pause") where i have "cout << "\n\nPress Enter to see the Computer's guess...";" but the program would just get to that new pause point and close afterward (not much further than the previous closing @ entering 'theNumber').
-----------------------------------------

// Guess My Number 2.0
// Have computer guess your number

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

using namespace std;

int main()
{
int theNumber;
int tries;

cout << "\tWelcome to Guess My Number 2.0!\n\n";

cout << "Please enter a number to be guessed: ";
cin >> theNumber;

do
{
int comGuess = rand() % 100 + 1; //Problem 2
cout << "\n\nPress Enter to see the Computer's guess...";
getch();
cout << "The Computer's Guess: " << comGuess << ".\n";
++tries;

if (comGuess > theNumber) {
cout << "Too high!\n\n"; } //Not a problem, but its bad style

if (comGuess < theNumber) {
cout << "Too low!\n\n"; } //Same

} while (comGuess != theNumber); //Problem 1

cout << "\nThat's it! The Computer got it in " << tries << " guesses!\n";
getch();

}


Also, note that after the computer gets it it quits.

Also note: I'm not that skilled at C++ yet i picked up all the errors in ~10 seconds.

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Quote:
Original post by Nice Coder
Also, note that after the computer gets it it quits.

Also note: I'm not that skilled at C++ yet i picked up all the errors in ~10 seconds.

From,
Nice coder


is there actually any c++ in that? i only see c statements. but i don't know c lol.
Charles Reed, CEO of CJWR Software LLC
Quote:
Original post by CJWR
Quote:
Original post by Nice Coder
Also, note that after the computer gets it it quits.

Also note: I'm not that skilled at C++ yet i picked up all the errors in ~10 seconds.

From,
Nice coder


is there actually any c++ in that? i only see c statements. but i don't know c lol.


Cout is C++, not C. (The C version would be printf())

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Advertisement
heres my version:

#include <iostream>using namespace std;int guess;int numTries = 0;bool correct = false;int input;int GuessMin = 0, GuessMax = 100;int GetRandomNum(int min, int max){    return min + int((max-min) * rand()/(RAND_MAX+1.0));}int main(){	srand(0); //could pass in current time here		cout << "====Guess YOUR number====" << endl << endl;	cout << "pick a number between 0 and 100, press any key when ready.." << endl;	cin.get();	do	{		guess = GetRandomNum(GuessMin,GuessMax);		cout << "is it " << guess << "??" << endl;		cout << "enter 1 if too low, 2 if too high, 3 if correct" << endl;		cin >> input;		if (input == 1)		{			GuessMin = guess + 1;		}		else if (input == 2)		{			GuessMax = guess - 1;		}		else if (input == 3)		{			correct = true;		}		else		{			cout << "I don't understand " << input << endl;			cout << "guessing again" << endl;		}		numTries++;	} while(!correct);	cout << "yes! I got it! phew..... took me " << numTries << " guesses!" << endl;    	cin.get();		return 0;}
-fuchiefck----------------------------------------------------------"Inside the world that you as a programmer or developer create, you are God" - Zerbst & Duvel
This should do it.
#include <iostream>using namespace std;int main(int ,char **){	double n;	cout << "Give me your number: " << flush;	cin >> n;	if (!cin) { cerr << "You cheat!\n"; return 1;};	for(;;)	{		cout << "Was your number " << n << "? " << flush;		string s;		cin >> s;		if (s=="y" || s=="yes") { cout << "I'm psychic!"; break; };		if (s=="n" || s=="no") { cout << "Liar!\n"; break; };		cout << "Answer yes or no. ";	}}

Quote:
Original post by Nice Coder

Also note: I'm not that skilled at C++ yet i picked up all the errors in ~10 seconds.


twit.

I did probably the most ghetto/lazy way of writing this program that anyone has ever done.

However if this were actual homework the wording of the question yeilds my answer "bool" no matter how bad it is!!

// ConsoleApplication6.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace System;
using namespace std;
int main(){
int guess;
int tries = 0;
cout << "put a number for me to guess" << endl;
cin >> guess;
for (int i = 0; i != guess; ++i){
if (guess < i){
cout << "computer guesses " << i << " to high" << endl;
--i;
--i;
}
if (guess > i){
cout << "computer guesses " << i << " to low" << endl;
}
++tries;
}
cout << "Your number is " << tries << endl;
system("pause");
return 0;
}

This topic is closed to new replies.

Advertisement