I am attempting to make a number guessing game but I need to use functions I figured a good spot for this would be the greater and less than problems that I need to include to make the user know what they have, I am horrible with functions and dont know how i would write them, this is what i have for code (yes i got a most of it from asking other people on other websites)

#include<iostream>
#include<cstdlib>
#include<ctime>
#include <string> //added strings
using namespace std;

int main()
{
srand(time(0));
int guess;
char playAgain='y'; //new variable

while (playAgain !='n') 
/*added a while loop so that the whole program 
will loop while the new variable isn't 'n'*/
{
int number=rand()%1000+1;;
cout<<"Im thinking of a number between 1-1000. Take a guess: ";
cin>>guess;

    while(guess!=number)
    {
        if(guess>number)
        {
            cout<<"Too high,Guess again: ";
            cin>>guess;
        }
        else if(guess<number)
        {
            cout<<"Too low,Guess again: ";
            cin>>guess;
        }
    }
        if(guess=number)
        {
            cout<<"Congrats!! You got it.";
        }

//do while loop so that the play again question loops for right answer
    do { 
        cout << "Would you like to play again? y/n ";
        cin >> playAgain;
        } while (playAgain !='y' && playAgain !='n');

}
return 0;
}

I will be continuing to try and figure this out (the assignment is due in 24 hours ugh) hopefully someone will be able to help me

Recommended Answers

All 4 Replies

the code looks fine to me, what exactly is the problem?

edit: noone will do the functions for you if you don't at least try to...
we are here to help people, and by giving them the full code we don't help them at all, they need to understand first

HII,
I HAVE WRITTEN A SIMPLE CODE WITH PARAMETERIZED FUNCTION AS PER UR REQUIREMENT...
I HAVE USED UR CODE ITSELF.. BUT WRITTEN IN FUNCTIONS FORMAT
PLEASE DO UNDERSTAND THE CODE. DONT JUST BLINDLY RUN IT..

#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

void numberGame(int);/*FUNCTION DECLARATION MENTIONING THE RETURN-TYPE AND TYPE OF PARAMETER(S)*/

void main()
{
clrscr();


int number=rand()%1000+1;
numberGame(number);// FUNCTION CALL

}


//FUNCTION DEFINITION//
void numberGame(int number)  
{
do{
     int guess;
     char ans;
     cout<<"IAM THINKING OF A NUMBER BETWEEN 1-1000, TAKE A GUESS";

cin>>guess;

      while(number!=guess)
        {


           if(guess<number)
             cout<<"too low guess again";
              cin>>guess;
             if(guess>number)
              {

                  cout<<"too high,guess again";
                  cin>>guess;
             }

        }
                   if(guess==number)
                   {
                   cout<<"you got it ";

                   }



cout<<"wish to continue..?";
cin>>ans;
}while(ans=='y' || ans=='Y');
}
    getch();

GOOD LUCK..!!

honestly its just the function that im not understanding. already told my teacher i dont really get it. i need a function and an fstream in here. not sure where/how to add.

please read it hope u understand ... if any query ask me again..
why functions..:
every programming language contains a mandatory main()..
as the length of the program increases its difficult to handle the code for the programmer only in main() thats when functions were introduced..

it modularizes ur code and makes it easier for the programmer to decode it..
the basic syntax required is :
return-type function-name (parameters)
parameters are not compulsory...
FUNCTIONS :
there are 3 steps in using a function
1. function prototype also called as function declaration
2. function calling
3. function definition..

Function prototype:
it informs the compiler that there is some function used ahead in the program..
its concept is same as a variable declaration
eg. return-type example(parameters);

return-type specifies that WHAT the function will return
parameters list defines what kind of parameters are used..

Function calling :
syntax
function-name(parameters);
the above syntax will take the program flow to function definition
no need to mention the return-type here..
the parameters will be passed to it.

Function definition :
here's its the actual code of what a function should do is written..
return-type function-name(parameters)


a practical example:
suppose u want to order a pizza on phone ...

u get the phone directory mentioning the pizza shop's phone number....(thats function declaration )

u make call to him.. (thats function calling )
on the phone u mention which pizza u wish to have..(thats the parameters u pass)

how the pizza is to be made that's pizza chef's work/... thats function definition
definition is what the function is gonna perform..
and finally the return-type is ur pizza u get at home.....

hope u understand it buddy..
or else press F8 to understand the flow of program...
that will help u too

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.