#include <iostream>
#include <windows.h>
#include <cstdlib>

using namespace std;


		
int main() {
 
	SetConsoleTitle("The Guessing Game!");
	int guess;                     
        int clue;
        srand ( time(NULL) );
	int num(rand() % 25 + 1);
	int total;
    	
	 cout << "Hello Internet\n";
	 Sleep(2000);
	 cout << "Today, we are going to play the Guessing Game!\n";
	 cout << "\n";
	 cout << "You are going to pick a number from 1-25 and if you need help\n" << "Id be happy to give you clues!\n" << "\n";
	 
	 do 
     {
         cout << "Pick a number: "; 
    	 cin >> guess;
    	 cout << endl;
    	 cin.ignore();
         total++;
      
    	    if ( guess < num ) { 
            cout << "Sorry, you guessed wrong!"<<endl;
    		cout << "Would you like a hint?\n" << endl << endl << "<y/n>";
    		char clue;
    		cin >> clue;
    			if ( clue =='y' ) {
    			cout << "You guessed low, sorry! Try again!\n";
    			}
    		}
    
            else if ( guess > num ) {            
            cout << "Sorry, you guessed wrong!"<<endl;
    		cout << "Would you like a hint?\n" << endl << endl<< "<y/n>";
    		char clue;
    		cin >> clue;
    			if ( clue =='y' ) {
    		    cout << "You guessed to high, sorry! Try again!\n";
    			}
    	    }
    
            else {
            cout << "You won the game in" << total << "tries!" << endl;    
            }
     } while ( guess != num );
      cin.get();
     
      return 0;
    }

After the person gets the right number, the total that is displayed at the end of the program is some crazy number like 2068798 etc.. for the amount of tries it took to guess the right number, why is this?


Edit: OHHH lol, i think i see the problem, i didnt set the value of total in the beginning =(

Recommended Answers

All 2 Replies

Thats because total is never initialized to 0. So you start with a random number from somewhere in the CPU's memory then start incrementing it by 1. This is bad! very bad!

Chris

ya lol, i just realized that , thanks for the response =P

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.