Guess! Guess! n Guess!!!

shouvik 0 Tallied Votes 271 Views Share

Well I thought u might be bored with the hangman stuff i posted a few days ago so here is a new gaming programme. well in this one all u have to do is try to guess the number correctly while running the programme. suggestions are welcome. this is not a very complicated programme all can understand this as well have fun with it. ENJOY!!!

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
const int totchan=7;
void main()
{
int number, guess, chances=0, score=0, chanscor;
char ans;
do
{ clrscr();
chances=score=0;
cout<<"n\t\t\t\tWelcome to High or low game.";
cout<<"n\t\t\t\tI will pick up a random number from 1 to 100.";
cout<<"n\t\t\t\t You must try and guess it.\n";
randomize();
number=(int)(rand()%100);
chanscor=100/totchan;
do
{
cout<<"\n What is your guess?(0 to 100)";
cin>>guess;
if((guess<0)||(guess>100))
{
cout<<"Sorry you have not entred a valid number. Your guess must be in between 0 to 100";
}
else if (guess<number)
{
cout<<"is low. Try a higher number";
}
else if (guess>number)
{
cout<<"is high. Try a lower number";
}
else
{
cout<<guess<<"is correct. Congratulations";
score=chanscor*(totchan-chances);
cout<<"\n\tYour score is"<<score<<endl;
}
chances++;
if(guess!=number)
cout<<"\n\nNow you have"<<totchan-chances<<"chances left."<<"\n";
if(chances==totchan)
{
cout<<"\n\nOnly"<<totchan<<"chances are allowed. Better luck next time.";
cout<<"\The actual number was"<<number<<"\n";
break;
}
} while(guess!=number);
cout<<"\n\nThank you for playing High and low!!!";
cout<<"\nWant to play again...???";
cin>>ans;
}while (ans=='Y' || ans=='y');
}

Dani AI

Generated

Nice idea from — and was right: the snippet uses Turbo C++/Borland-specific calls and has a few concrete compile/runtime bugs that will stop it on modern compilers. The main issues: legacy headers (<iostream.h>, <conio.h>) and functions (randomize(), clrscr()); a non-standard void main; wrong escape sequences ("n" instead of "\n", a stray backslash before The actual number was); rand()%100 producing 0–99 while the text says 1–100; input validation that still consumes a chance on bad input; and integer division in the score calculation that truncates results.

Recommended fixes and best practices: use standard headers (e.g. <iostream>, <random>, <limits>), change to int main(), replace rand()/randomize() with std::mt19937 + std::uniform_int_distribution or seed std::srand(time(nullptr)), validate std::cin and avoid incrementing attempts for non-numeric or out-of-range entries, fix string escapes ("\n"), and compute score using floating arithmetic (then round) to avoid truncation. Also remove conio.h/clrscr() for portability (or replace with safe, platform-specific calls).

A compact, modern example that addresses the errors and improves input handling:

#include <iostream>
#include <random>
#include <limits>
#include <cmath>

int main() {
    const int totchan = 7;
    std::random_device rd; std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dist(1,100);
    char ans = 'y';
    while (ans=='y' || ans=='Y') {
        int number = dist(gen), chances = 0, guess = 0;
        bool won = false;
        while (chances < totchan && !won) {
            std::cout << "Guess (1-100): ";
            if (!(std::cin >> guess)) { std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::cout << "Invalid input. Try an integer.\n"; continue;
            }
            if (guess < 1 || guess > 100) { std::cout << "Out of range.\n"; continue; }
            chances++;
            if (guess < number) std::cout << "Too low.\n";
            else if (guess > number) std::cout << "Too high.\n";
            else {
                int remaining = totchan - (chances - 1);
                int score = static_cast<int>(std::lround(100.0 * remaining / totchan));
                std::cout << guess << " is correct. Score: " << score << "\n";
                won = true;
            }
            if (!won) std::cout << "Chances left: " << (totchan - chances) << "\n";
        }
        if (!won) std::cout << "Out of chances. The actual number was " << number << "\n";
        std::cout << "Play again? (y/n): "; std::cin >> ans;
    }
    return 0;
}

Compilation hint: use a modern compiler and warnings, e.g. g++ -std=c++17 -Wall -Wextra guess.cpp -o guess. This resolves the errors pointed out and makes the game portable and robust across current toolchains.

Ghost 0 Posting Whiz

a bunch of errors...

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.