Cin and point to function chosen - c++

I am not understanding how to make it so when i input my selection in testSelection for it to point to the function test. How do i go about doing this? Shouldn't it just go there?
#include <iostream>
using namespace std;
int test (int testSelection);
int main()
{
int testSelection;
cout << "Welcome to the pizza place!" << endl;
cout << "Choose 1 for pizza or 2 for drinks: ";
cin >> testSelection;
return 0;
}
int test (int testSelection)
{
if (testSelection== 1)
{
cout << "select your Pizza" << endl;
}
if (testSelection== 2)
{
cout << "Please select your drink" << endl;
}
else
cout << "test";
return 0;
}

You need to call the function...
cin >> testSelection;
test(testSelection);
Basically, you've written a function definition int test(int testSelection) {...code...} but, it's just dormant code until you invoke it by calling it.

I'm not sure what you are asking exactly. testSelection is an int, not a function which returns an int (which test is). Please elaborate on what you are trying to accomplish here if I'm off track, you never even call test. As far as I can tell, what you actually want is:
int test (int testSelection);
int main()
{
int testSelection;
cout << "Welcome to the pizza place!" << endl;
cout << "Choose 1 for pizza or 2 for drinks: ";
cin >> testSelection;
// you actually have to call the function...
test(testSelection);
return 0;
}
I didn't add any input validation (you should check that the cin actually grabbed a valid integer).

Related

C++ not waiting for an input

so I have been learning C++ and was working on a monkey see monkey do program and i managed to get the first input working but the second input it just skips straight over it, i have no clue why and any help would be apreciated.
#include <iostream>
#include <string>
using namespace std;
char monkey_1;
char monkey_2;
int msmd()
{
cout << "monkey one:"; cout << endl;
cin >> monkey_1;
system("cls");
cout << "monkey two:"; cout << endl;
cin.clear();
cin >> monkey_2;
cout << "waiting"; cout << endl;
if (monkey_1 == monkey_2)
{
cout << "both monkeys are happy."; cout << endl;
}
else
{
cout << "the monkeys are upest."; cout << endl;
}
return 0;
}
void main()
{
msmd();
}
Do you intent to only get a single character from the input as the monkeys are of type char? If not, change them to string, otherwise it will only assign a single character per cin.
If you want to input a sentence, cin also splits on spaces, so if you enter "something else", the first cin will assign something to monkey_1, and the second cin will automatically assign else to monkey_2.
To get around this you can use getLine(cin,monkey_x).
There are two point needs to be modified.
char monkey_1 and mokeny_2 should be declared as string for get more than 1 character.
void main need to be changed as int main.

How do I make an if statement that repeats itself if the requirements are still not met?

Here's my code.
#include <iostream>
using namespace std;
int main()
{
int i = 0;
int players;
int silenceAmount;
int bleedAmount;
int bleedDays;
int playersAlive;
int playersDead;
int playersAllowed = 6;
cout << "How many players are playing (can only be " << playersAllowed << ")? ";
cin >> players;
if (players > playersAllowed) (
cout << "There an only be " << playersAllowed << " players. Please select another number. ");
cin >> players;
cout << "There are " << players << " players.\n\n";
return 0;
}
this works for only one time and I want it to work until it gets a number less than or equal to 6.
Repeating if statements are generally done as a while loop. In your code, when you have
if (players > playersAllowed)
You should simply change it do
while (players > playersAllowed)
Also, while I'm at it, the syntax you have for your if statement is not correct for what you are trying to do. You should replace ( and ) by { and } respectively. Also, the ending bracket is in the incorrect place.
In the end, your loop would be something like this:
while (players > playersAllowed) {
cout << "There can only be " << playersAllowed << " players. Please select another number: ";
cin >> players;
}
Note that this doesn't take into account someone entering jfksdjfs for a number.

How do I execute previously executed lines of code in C++

I've started to learn how to code in C++ on my spare time, using different sites and apps that someone who has also learned C++ online provided me with. By now, I know the most basic commands. I've tried an exercise given by a program, and I'm given the information that someone is going on a vacation, and needs to know how much baggage he can bring with him. The limit to how many baggages he can carry is 45, and I have to display a different output if the baggages are below, above or the same as the limit (45 baggages). I have done some coding, and I ended up with this:
#include <iostream>
using namespace std;
int main()
{
const int limit = 45;
int bag;
cout << "Please type your number here: ";
cin >> bag;
string yn;
int keep = 0;
if (limit < bag)
{
cout << "You passed the limit." << endl;
};
if (limit == bag)
{
cout << "Just enough." << endl;
};
if (limit > bag)
{
cout << "You got space." << endl;
};
++keep;
while(keep > 0)
{
int keep = 0;
cout << "Do you want to try another number?" << endl;
cin >> yn;
cout << endl;
if(yn == "yes")
{
int bag = 0;
cout << "Please type your number here: ";
cin >> bag;
if (limit < bag)
{
cout << "You passed the limit." << endl;
};
if (limit == bag)
{
cout << "Just enough." << endl;
};
if (limit > bag)
{
cout << "You got space." << endl;
};
}
else
{
return 0;
}
}
}
I have developed it more than needed -as you can see-, out of my own interest in the problem. I have copied and pasted the 3 IF commands as seen above, and I believe that there is an easier way, with less code, do solve this. What I have thought of is if I could go back and execute some line of code again, either from a line and below (e.g. from line 45 and below), or specific lines of code (e.g. from line 45 to line 60).
I would appreciate it if you thought of another way to solve this problem and posted your code below.
Thank you for your reply.
We all started writing our first C++ program at some time, so allow me to give you some additional feedback:
First of all, avoid writing using namespace std;
Secondly, naming - what is bag, limit, keep and yn? Wouldn't it be much easier to read and understand if they were called bagSize, maximumPermittedBagSize, inputFromUser (you don't really need the variable keep, see below)?
Finally, here is a (roughly) refactored version your program, with duplication removed and comments added.
#include <iostream>
int main()
{
const int maximumPermittedBagSize = 45;
// Loops forever, the user exits by typing anything except 'yes' laster
while(true)
{
std::cout << "Please type your number here: " << std::endl;
//Declare (and initialize!) variables just before you need them
int bagSize = 0;
std::cin >> bagSize;
if (bagSize > maximumPermittedBagSize)
{
std::cout << "You passed the limit." << std::endl;
}
else if (bagSize == maximumPermittedBagSize )
{
std::cout << "Just enough." << std::endl;
}
else
{
std::cout << "You got space." << std::endl;
}
std::cout << "Do you want to try another number?" << std::endl;
std::string inputFromUser = "";
std::cin >> inputFromUser;
std::cout << std::endl;
//Leave the loop if the user does not answer yes
if(inputFromUser != "yes")
{
return 0;
}
}
}
You can simply run a while loop and do like this:
#include <iostream>
using namespace std;
int main()
{
const int limit = 45;
int bag;
string yn = "yes";
while(yn == "yes")
{
cout << "Please type your number here: ";
cin >> bag;
if (limit < bag)
{
cout << "You passed the limit." << endl;
}
else if (limit == bag)
{
cout << "Just enough." << endl;
}
else if (limit > bag)
{
cout << "You got space." << endl;
}
cout << "Do you want to try another number?" << endl;
cin >> yn;
cout << endl;
}
}

How to loop the program's asking for new set of inputs in c++?

Consider the following code:
#include <iostream>
using namespace std;
int main(){
int a,b;
cout << "Enter two positive numbers:" <<endl;
cin >> a >> b;
if (a<b) cout <<a<<" is less than "<< b<<endl;
else if (a>b) cout <<a<<" is greater than " <<b<<endl;
}
How can I make the program endlessly repeat asking for a new set of numbers as input?
Here's the simplest way of doing what you want (there are other ways). Basically, you just need to 'wrap' the code that you want to repeat in a loop, where the 'test' condition for the loop will always evaluate to true.
Note the comments with "///" I've given:
#include <iostream>
//using namespace std; /// Search this site for "Why using namespace std is bad"
using std::cout;/// Just declare usage of those feature you ACTUALLY use...
using std::cin;
using std::endl;
int main() {
int a, b;
while (true) { /// The test condition will always be "TRUE" so the loop will never end!
cout << "Enter two positive numbers:" << endl;
cin >> a >> b;
if (a < b) cout << a << " is less than " << b << endl;
else if (a > b) cout << a << " is greater than " << b << endl;
// cout /// This line is wrong!
}
}
Feel free to ask for further clarification and/or explanation.
Depends on what exactly do you want your program to do. If you want it to "deny access". For example lets say you have want a number K > 3 always for the program to continue. The all you have to do is use a do- while loop:
do
{
cout << "Enter the value for the sequence: ";
cin >> K;
if ( K <= 3)
{
cout << "Write a bigger number!" << endl;
}
} while(K <= 3);
Otherwise just use a normal loop with the condition suitable for the task.
Suppose your program is to find the Factorial of number and you want it to loop such that it ask for new value from the user
int main()
{
int n;
while (true) {
int factorial = 1;
cin >> n;
if (n==0) {
cout << 0;
}
else {
for (int i=n;i>0;i--) {
factorial = factorial*i;
}
cout << factorial;
}
}
return 0;
}

Currency Conversion Program

I'm working on a currency converter program that converts the old system of British pounds, Shillings, and pence, into their new system, which is a type of Decimal Pound. Where 100 pence equals a pound. Here is the code for the program
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
int calcNum(int pound, int shilling, int pence)
{
pence = pound*240 + shilling*12 + pence;
return pence;
}
int calcNew(int total_pence, double dec_pound)
{
dec_pound = total_pence / 240;
return dec_pound;
}
int main()
{
int pence;
int shilling;
int pound;
const int OLD_POUND = 240;
const int OLD_SHILLING = 12;
double total_pence;
double dec_pound = 0;
double deci_pound;
cout << "Please Enter the Amount of old pounds: ";
cin >> pound;
cout << endl;
if(cin.fail())
{
cout << "That's not a valid number\n";
cout << "This program will terminate on any keypress!";
_getch();
exit(1);
}
cout << "Please Enter the Amount of old shillings: ";
cin >> shilling;
cout << endl;
if(cin.fail())
{
cout << "That's not a valid number\n";
cout << "This program will terminate on any keypress!";
_getch();
exit(1);
}
cout << "Please Enter the Amount of old pence: ";
cin >> pence;
cout << endl;
if(cin.fail())
{
cout << "That's not a valid number\n";
cout << "This program will terminate on any keypress!";
_getch();
exit(1);
}
total_pence = calcNum(pence, shilling, pound);
deci_pound = calcNew(dec_pound, total_pence);
cout << (5, "\n");
cout << "The total amount in decimal pounds is: ";
cout << setprecision(2) << "\x9c" << deci_pound;
_getch();
return 0;
}
When I run this program however, I'm having a bit of a problem. No matter what the number input is, it always says 0 pounds. Just to make sure that the setprecision function at the end wasn't interfering with the code, I had originally set a cout statement with a _getch() after the two functions to show how much deci_pound came out to be calculated to, and once again, it came out as zero. So my issue seems to be somewhere in the functions running the calculations. If someone could help me with this, I would really appreciate it.
Your calcNew(...) function returns an int, make it return a double. Right now it casts to int which involves stripping the decimals.
In your code, dec_pound is set equal to zero, and you're deci_pound = calcNew(dec_pound, total_pence), which divides 0 by 240 = 0.
The order of the parameters when you call both functions is wrong. Your functions are declared and implemented as:
int calcNum(int pound, int shilling, int pence);
int calcNew(int total_pence, double dec_pound);
And then you call them like this:
total_pence = calcNum(pence, shilling, pound);
deci_pound = calcNew(dec_pound, total_pence);