Functions in C++ - The 13 Sone game - c++

I am trying to write a program that requires input validation through functions. The idea behind it is much like the 21 stones only it is with 13 and the computer always wins. The game starts with 13 stones and the computer will always choose 1 on the first turn creating a multiple of 4 scenario. This means if the user takes 3 computer takes 1, user takes 2 computer takes 2 and so on until no stones remain. My problem is I am having a hard time getting my head around functions and how data is called from the parameters within so any help with this would be greatly appreciated!
This is what I have sofar.
#include <iostream>
using namespace std;
//function prototypes
bool validPick(int numStones);
int computerPick(int stones_in_pile, int player2taken);
int playerPick(int stones_in_pile);
int main()
{
int stones_left = 13, P1Taken, P2Taken;
cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
<< "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
<< " the pile on your turn.\nThe player that removes the last stone "
<< "or stones from the pile wins the game.\nGood Luck... You will need"
<< " it! I NEVER LOOSE!!"
<< endl << endl;
computerPick(stones_left, P2Taken);
playerPick(P1Taken);
validPick(stones_left);
//game logic here -- This is far from done.
stones_left -= P1Taken;
stones_left -= P2Taken;
return 0;
}
/******************************************************************************\
* Validate the picked number 1-3 are only valid numbers to choose from. *
\******************************************************************************/
bool validPick(int numStones)
{
if((numStones < 1) || (numStones >3))
cout << "Invalid Selection. 1-3 is all you can have!";
else
return numStones;
}
/******************************************************************************\
* Computer's function calls. Should start with 1. We always want the computer *
* to win the game. *
\******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
if(player2taken == 0)
stones_in_pile -= 1;
else
{
if(player2taken == 1)
stones_in_pile -= 3;
else
if(player2taken == 2)
stones_in_pile -= 2;
else
stones_in_pile -=1;
}
return stones_in_pile;
}
/******************************************************************************\
* Player's Pick function call goes here. The player goes second *
\******************************************************************************/
int playerPick(int stones_in_pile)
{
cout << "Please choose the ammount of stones. 1-3 only! : ";
cin >> stones_in_pile;
return stones_in_pile;
}

Despite the fact that you should better read a beginners book than trying to understand C++ by asking such questions, I will try to explain what is wrong in your code by an example:
bool validPick(int numStones) {
if((numStones < 1) || (numStones >3))
cout << "Invalid Selection. 1-3 is all you can have!";
else
return numStones;
}
This function is declared to return a bool value. However, if the condition in the if-clause turns out to be true, the function does not return anything, that is a mistake. Second, numStones is an int, so when you return it as a bool it will get converted (from int to bool) which is probably not what you want. Honestly, I didnt even try to understand the logic of your program but a valid version of this function could look like this:
bool validPick(int numStones) {
if((numStones < 1) || (numStones >3)) {
cout << "Invalid Selection. 1-3 is all you can have!";
return false;
}
return true;
}

There are many philosophies with functions that produce values and how those values are passed back.
Functions can pass values back to the caller by either modifying the parameter or by returning a value.
The playerPick function can either modify the passed value (by passing by reference):
void playerPick(int& stones_in_pile)
{
cout << "Please choose the ammount of stones. 1-3 only! : ";
cin >> stones_in_pile;
}
Or by returning a value:
int playerPick(void)
{
// Local variable to temporarily hold a value.
int stones_in_pile = - 1;
cout << "Please choose the ammount of stones. 1-3 only! : ";
cin >> stones_in_pile;
return stones_in_pile;
}
Note that the latter version uses a local, temporary, variable and the compiler will return a copy of the value upon end of the function.
I'm using void in the parameter for emphasis here.

Related

Why is my while loop function only returning '1'?

Precursor, I just started my first coding class, so forgive me if my mistake(s) is/are painfully obvious. All I need to do right now is use a programmer defined function to ask for an integer and read out an error message until the correct input is entered, then read out the correct input.
#include <iostream>
using namespace std;
bool evenint(int num1_par);//even integer declaration
int main ()
{
int num1, correctnum1;//even integer, function call variable
cout << "Enter an even integer between 2 and 12. \n";
cin >> num1;
correctnum1 = evenint(num1); //function call
cout << "You chose '" << correctnum1 << "'" <<endl;
return 0;
}
/*
Function: evenint
Parameter/Return: An even integer between 2 and 12 inclusively
Description: This function ensures the input matches parameters before
returning its value
*/
bool evenint(int num1_par)
{
if (!(num1_par>=2 && num1_par<=12 && num1_par % 2 ==0))// integer must be
between 2 and 12 inclusively
{
while (!(num1_par>=2 && num1_par<=12 && num1_par % 2 ==0))
{
cout << "Your number is invalid, please try again. \n";
cin >> num1_par;
}
return (num1_par);
}
else
{
return (num1_par);
}
}
I've tried switching my if-else/while loop to just a do-while/everything else I can think of but what I have now is the closest I've gotten. When I enter an invalid integer I get the correct response, but when I enter a valid integer it prints, "You chose '1'" no matter what valid integer I input. I've tried everything I know but now I'm stumped. Any help at all would be great!
Your function returns a bool which is either zero or one. Change it to int and your code will work.

Retrieve all data via void function

Been trying to find a way through this. I am new to C++ and creating a simple program to get the user data, validate and cout to the screen. What i'm trying to do is to have the one function use pointers to get the users input and display back to them. This may have been answered before but I haven't had much luck finding it.
So far i have the below code
#include <iostream>
using namespace std;
void userData(int&);
int main(){
int a = 0;
int * kmpointer;
int * dayspointer;
userData();
cout << "You ran " << userData(kmpointer) << endl;
cout << "in " << userData(dayspointer) << "days!!" <<endl;
}
void userData(int& i){
cout << "Enter how Many Km's you ran:";
while (true)
{
cin >> kmpointer;
if ((cin) && (kmpointer >= 0) && (inputYear <= 100))
break;
cin.clear();
cin.ignore( 100, '\n' );
cout << "That can't be right!\n";
cout << "Enter how Many Km's you ran:";
}
cout << "How many days in a row did you run?";
while (true)
{
cin >> dayspointer;
if ((cin) && (dayspointer >= 1) && (dayspointer <= 100))
break;
cin.clear();
cin.ignore( 1000, '\n' );
cout << "Thats way to much!";
cout << "How many days in a row did you run? ";
}
}
IMO, you should start with some reading about C++. You are missing some basic concepts and trying too complex exercises for your level.
1
function is not declared/defined.
2
userData is declared accepting a parameter, but used without.
3
The problem you face is related probably with what we call scope: A variable is only existing and visible within its scope (usually enclosed by { and }.
In your case, kmpointer and dayspointerare only visible within the main function and thus, you cannot use them in userData.
To solve that, I suggest you to pass those variables as parameters for userData.
4
Pointers, references, values: They are different. You are saving the user input as a pointer address, which is indeed problematic.
General
In general, your code is full of mistakes. Try a Hello world! and continue from there steps by steps.
Focussing on the specific question you asked (though as observed you have other problems in your code), don't use pointers, use references.
Before we get to that this
cout << "You ran " << userData(kmpointer) << endl;
won't compile, since as you know userData is a void function, so applying << to it makes no sense. It's void so there's nothing to stream.
You said you wanted to pass parameters into the function and let them be changed so do that. Then display the variables afterwards. (Not the "result" of a void function call).
Correctly getting the user input is a separate question which has been answered before.
#include <iostream>
using namespace std;
void userData(int& i, int& j, int& k);
int main() {
int a = 0;
int kmpointer;
int dayspointer;
//Here we call our function, ONCE
userData(a, kmpointer, dayspointer);
//Here we display what values we now have
//after calling the function, ONCE
cout << "You ran " << kmpointer << endl;
cout << "in " << dayspointer << " days!!" << endl;
}
//simplified to demonstrate changes to the reference parameters
void userData(int& i, int& j, int& k) {
//Here we have three parameters which we refer to as i, j and k
// They may have different names ousdie in the calling code
// but this function (scope) neither knows nor cares
j = 42;
k = 101;
}

Simple query regarding operators

Hello I'm writing a program which runs a lot of functions that run based on the user inputted menu choice which I will not be including. My question is why is the following code not responding to differences in user input. For example if I enter menu choice 1 or 4 it doesn't matter and will revert to menu choice 1. I know it has something to do with my = or == operators but neither has produced the correct result so im not sure what to do. Please help!
int main() //Handles the if statements concerning the menu of the program
{
int r_identifier[42]; //Variable Declaration
int year_entry[42];
double gpa_entry[42];
string student_name[42];
int index = 0;
int menuchoice; //Variable Declaration
do
{
print_menu(); //Calls function to print menu
get_selection(menuchoice); //Calls function to get the menu selection
if (menuchoice = 1) //Calls the function to input a new user
{
input_new_student(student_name, r_identifier, gpa_entry, index, year_entry);
cout << "\nThe student with R#" << r_identifier[index] << " was created. " << endl;
index++;
}
else if (menuchoice = 2) //Prints all
{
print_all ();
}
else if (menuchoice = 3) //Prints statistics about all students in a particular year
{
int year_view;
print_by_year(student_name, r_identifier, gpa_entry, index, year_entry);
}
else if (menuchoice = 4) //Prints statistics about all entered users
{
print_statistics();
}
else if (menuchoice = 5) //Quits the program
{
cout << "Have a good summer! ";
cout << endl;
}
} while (menuchoice != 5);
return 0;
}
1.'=' is for assignment
Ex: int a=5 assigns 5 to the variable named a.
In ur case...u should change all ur '=' to '=='.
'==' is for comparison.
Ex: if(a==5)cout<<a; will print a only if a equals 5...
2 The variable menuchoice doesnt take a value ...u should not take it as a parameter of the function getselection... Instead u can make it return the choice something like this menuchoice=getselection()
3 Include an else part... it give some more meaning to the whole program instead of do while...Keep it as simple as possible :)

Need help to stop program terminating without users consent

The following code is supposed to do as follows:
create list specified by the user
ask user to input number
3.a) if number is on the list , display number * 2, go back to step 2
3.b) if number isn't on the list, terminate program
HOWEVER step 3.a) will also terminate the program, which is defeating the purpose of the while loop.
here is the code :
#include <iostream>
#include <array>
using namespace std;
int main()
{
cout << "First we will make a list" << endl;
array <int, 5>list;
int x, number;
bool isinlist = true;
cout << "Enter list of 5 numbers." << endl;
for (x = 0; x <= 4; x++)
{
cin >> list[x];
}
while (isinlist == true)
{
cout << "now enter a number on the list to double" << endl;
cin >> number;
for (x = 0; x <= 4; x++)
{
if (number == list[x])
{
cout << "The number is in the list. Double " << number << " is " << number * 2 << endl;
}
else
isinlist = false;
}
}
return 0;
}
Please can someone help me to resolve this ?
I would suggest that you encapsulate the functionality of step 3 into a separate function. You could define a function as follows, and then call it at an appropriate location in the main function.
void CheckVector(vector<int> yourlist)
{
.... // Take user input for number to search for
.... // The logic of searching for number.
if (number exists)
{
// cout twice the number
// return CheckVector(yourlist)
}
else
return;
}
The same functionality can be implemented with a goto statement, avoiding the need for a function. However, using goto is considered bad practice and I won't recommend it.
Your issue is that you set isinlist to false as soon as one single value in the list is not equal to the user input.
You should set isinlist to false ay the beginning of your while loop and change it to true if you find a match.
Stepping your code with a debugger should help you understand the issue. I encourage you to try it.

Incremented int is resetting at the end of the function

This is the function in question. The variable in question is count1. Prior to return count1; the function appears to reset count1 to either 1 or 2. The result of the final cout line is n lines where n=number of tries including the correct answer. Each line outputs a number that is 1 higher than the line below until count1 = either 1 or 2. I haven't been able to establish a pattern as to which it will finally output.
The questions themselves are simply placeholders.
What on Earth is going on?
Note: I am a very new programmer, and I am aware that there are likely more efficient ways to do what I am doing that I have not learned. I'm open to suggestions, but my understanding of those suggestions will likely be hampered by my unfamiliarity with C++
int q1(int count1) //q1() is always fed a value of 1.
{
using namespace std;
if (count1 <= 3) //User gets 3 tries to answer for full credit.
{ //count1 is returned in order to determine user score.
cout << "What is 2+2 \n"; //problem appears to occur between here and the end of this if statement.
double a1;
cin >> a1;
if (a1 == 4)
{
cout << "Yup. You know what 2+2 is. \n\n";
}
else
{
wrong(); //wrong() is a single line void function using std::cout and nothing else.
q1(++count1);
}
}
else
{
cout << "You have used all three tries. Next question. \n\n";
++count1; //count1 is incremented for an if statement in int main()
}
cout << count1 << "\n"; //This line is strictly for debugging
return count1;
}
Output of the final cout line looks along the lines of this:
5
4
3
2
Without \n
5432
EDIT:
There was an answer below that is deleted for some reason that appeared to resolve my problem.
The answer stated I should replace q1(++count1) with count1 = q1(++count1);
In my mind this shouldn't work, but in practice it seems to work. Why?
When using recursion, the first time your function runs count1 is 1 (as you said). If the user answers right, then your function will return 1, because the value of count1 never changes.
If the user answers wrong, then count1 increases by 1 and gives it's value to a new function (of the same type). Keep in mind that you pass the value of count1, that means the new function (the second q1()) will get the number 2 but will have a new variable count1. They may have the same name, but they are different variables.
There are two ways to solve your problem:
Either by using pointers, this way you pass the address of count1, and each function changes the same variable. (This is the hardest way and not the most efficient) or
Instead of making recursive calls, you can make a while like so:
int q1(int count1)
{
using namespace std;
while (count1 <= 3) //Run as long as user has chances
{
cout << "What is 2+2 \n";
double a1;
cin >> a1;
if (a1 == 4)
{
cout << "Yup. You know what 2+2 is. \n\n";
//Using `break` you stop the running `while` so the next
//step is for the function to return
break;
}
else
{
wrong();
//By incrementing `count1` the next time the `while` runs
//if user ran out of tries it will not enter the loop, so
//it will return `count1` which would most likely be 4
count1++;
}
}
//Here the function is about to return, so you check if user won or lost
if (count1 == 4)
cout << "You have used all three tries. Next question. \n\n";
//Debug
cout << count1 << "\n";
//Return
return count1;
}