Hi,
This is small c++ guess game, I almost finished it, but when i tried compile it gave me an error: "Cannot open include file: 'stdlib': No such file or directory"
is this file part of c++ standard library..? why compiler can't find it?

here is my .cpp source:

Attachments
/* HIGH/LOW GAME */
#include<iostream>
#include<windows.h>
#include<stdlib>
using namespace std;
/*
This program plays a simple game.The computer picks a random number 
from
0 to 100, and the user tries to guess the number.
*/
const int totchan=7;
void main()
{
 int number;        //the computer's random number
 int guess;         //the user's guess
 int chances=0,score=0,chanscor;        //chanscor stores score for 1
successful chance.
 char ans;
 do
 { clrscr();
   chances=score=0;
   cout<<"
				Welcome to the High/Low game.";
   cout<<"
			I will pick a random number from 0 to 100.";
   cout<<"
			    You must try to guess the number.
";
   randomize();
   number=(int)(rand()%100);
   chanscor=100/totchan;        //score for each successful chance
   do
   {
     cout<<"
	What is your guess? (0 to 100) ";
     cin>>guess;
     if((guess<0)||(guess>100))
     {
	cout<<"Sorry, but your guess "<<guess<<"must be from 0 to 100.";
     }
     else if(guess < number)
     {
	cout<<guess<<" is low.Try a higher number.";
     }
     else if(guess > number)
     {
	cout<<guess<<" is high.Try a lower number.";
     }
     else		//if correct number is guessed
     {			//number is correct, and the "do" loop will end below
	cout<<guess<<" is correct. Congratulations!";
	score=chanscor*(totchan-chances);   //score calculated for number of
chances left
	cout<<"
	Your score is "<<score<<endl;
        break;
     }
     chances++;
     if(guess!=number)
	 cout<<"

Now you have "<<totchan-chances<<"chances left."<<endl;
     if(chances==totchan)
	 { cout<<"

Only "<<totchan<<"chances are allowed.Better luck next
time";
	   cout<<"
The actual number was "<<number<<endl;
	   break;
	 }
     }while (guess!=number);
     cout<<"

Thank you for playing High/Low!";
     cout<<"Want to play it again? (y/n)...";
     cin>>ans;
   }while(ans=='y' || ans=='Y');
 }

stdlib.h you forgot the .h :]

Edited 7 Years Ago by FrozenSnake: n/a

Attachments
#include <iostream>
#include <windows.h>
#include <stdlib.h> // or #include<cstdlib>
using namespace std;
const int totchan=7;
int main()
{
    int number;        //the computer's random number
    int guess;         //the user's guess
    int chances=0,score=0,chanscor;        //chanscor stores score for 1 successful chance.
    char ans;
    do
    {
        //clrscr();
        chances=score=0;
        cout<<"Welcome to the High/Low game.";
        cout<<"I will pick a random number from 0 to 100.";
        cout<<"You must try to guess the number.";
        //randomize();
        number=(int)(rand()%100);
        chanscor=100/totchan;        //score for each successful chance
        do
        {
            cout<<"hat is your guess? (0 to 100) ";
            cin>>guess;
            if((guess<0)||(guess>100))
            {
                cout<<"Sorry, but your guess "<<guess<<"must be from 0 to 100.";
            }
            else if(guess < number)
            {
                cout<<guess<<" is low.Try a higher number.";
            }
            else if(guess > number)
            {
                cout<<guess<<" is high.Try a lower number.";
            }
            else		//if correct number is guessed
            {			//number is correct, and the "do" loop will end below
                cout<<guess<<" is correct. Congratulations!";
                score=chanscor*(totchan-chances);   //score calculated for number of chances left
                cout<<"Your score is "<<score<<endl;
                break;
            }
            chances++;
            if(guess!=number)
                cout<<"Now you have "<<totchan-chances<<"chances left."<<endl;
            if(chances==totchan)
            {
                cout<<"Only "<<totchan<<"chances are allowed.Better luck next time";
                cout<<" The actual number was "<<number<<endl;
                break;
            }
        }while (guess!=number);
        cout<<"Thank you for playing High/Low!";
        cout<<"Want to play it again? (y/n)...";
        cin>>ans;
    }while(ans=='y' || ans=='Y');
}

Thanks FrozenSnake, code worked well ..
what is header conio.h, is it important in c++ 2008 express

Hi,
This is small c++ guess game, I almost finished it, but when i tried compile it gave me an error: "Cannot open include file: 'stdlib': No such file or directory"
is this file part of c++ standard library..? why compiler can't find it?

here is my .cpp source:

Ok lets fix the first issue, in order to get stdlib to work add a .h to the end of it (i.e. #include <stdlib.h>)

Secondly your formatting is horrible. Your cout's are all done incorrectly, you have functions that are inproper such as line 51: that is an invalid line of code, as is line 19 and line 22 (well the { is valid, the clrscr() is not (nor is it necessary).

Lastly aside from sentence structures, your last while statement is improper, first change the char ans to string ans (don't forget to add the string header to the top), and then the while statement should be (ans == "y" || ans == "Y"). Also you need to add this line to your if(chances==totchan) call:
guess = number;

Otherwise if you keep guessing the wrong number the game will continue playing until you get the number correct. This way if you still miss it, the game will end correctly and allow you to either exit or try again.

After removing the bad parts and fixing the cout structures I was able to compile and run your program.

Edited 7 Years Ago by RayvenHawk: n/a

This article has been dead for over six months. Start a new discussion instead.