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 <string> //added strings
using namespace std;
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'*/
cout<<"Im thinking of a number between 1-1000. Take a guess: ";
cout<<"Too high,Guess again: ";
cout<<"Too low,Guess again: ";
cout<<"Congrats!! You got it.";
//do while loop so that the play again question loops for right answer
cout << "Would you like to play again? y/n ";
cin >> playAgain;
} while (playAgain !='y' && playAgain !='n');
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
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..
void numberGame(int);/*FUNCTION DECLARATION MENTIONING THE RETURN-TYPE AND TYPE OF PARAMETER(S)*/
numberGame(number);// FUNCTION CALL
void numberGame(int number)
cout<<"IAM THINKING OF A NUMBER BETWEEN 1-1000, TAKE A GUESS";
cout<<"too low guess again";
cout<<"too high,guess again";
cout<<"you got it ";
cout<<"wish to continue..?";
}while(ans=='y' || ans=='Y');
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