Help with if statements

I'm kinda new at C++ programing and having trouble with this program.

#include <iostream>
using namespace std;

void FirstThrow(int throwOne);
void SecondThrow(int throwTwo);
void ThirdThrow(int throwThree);
void FindErrors();

int main () {
int throwOne,
throwTwo,
throwThree;
FirstThrow(throwOne);
SecondThrow(throwTwo);
ThirdThrow(throwThree);
FindErrors();
return 0;
}

void FirstThrow(int throwOne) {
cout << "Enter the first score: ";
cin >> throwOne;
return;
}

void SecondThrow(int throwTwo) {
cout << "Enter your second score: ";
cin >> throwTwo;
return;
}

void ThirdThrow(int throwThree) {
cout << "Enter your third score: ";
cin >> throwThree;
return;
}

void FindErrors(){
int throwOne,
throwTwo,
throwThree;
if (throwOne > 10){
cout << "Throw of " << throwOne << " is invalid because you can't roll more than 10 on one throw" << endl;
}if (throwTwo > 10){
cout << "Throw of " << throwTwo << " is invalid because you can't roll more than 10 on one throw" << endl;
}if (throwThree > 10){
cout << "Throw of " << throwThree << " is invalid because you can't roll more than 10 on one throw" << endl;}
return;
}

When I run it, it doesn't matter what I enter be it 1 or 100 it says:
"Throw of 2293504 is invalid because you can't roll more than 10 on one throw"
"Throw of 2293512 is invalid because you can't roll more than 10 on one throw"
"Throw of 2293700 is invalid because you can't roll more than 10 on one throw"
It should print what number I type in only if I type a number more than 10... I'm stuck... can someone help please!?
Your functions do not modify the values you pass to them like you seem to think they do. And the throw variables FindErrors() are completely separate from the ones you've declared elsewhere.
I've [code]wrapped your code[/code] into code tags and fixed the indentation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;

void FirstThrow(int throwOne); //see note 1
void SecondThrow(int throwTwo); //see note 1
void ThirdThrow(int throwThree); //see note 1
void FindErrors();

int main ()
{
    int throwOne, throwTwo, throwThree;
    FirstThrow(throwOne);
    SecondThrow(throwTwo);
    ThirdThrow(throwThree);
    FindErrors();
    return 0;
}

void FirstThrow(int throwOne) //see note 1
{
    cout << "Enter the first score: ";
    cin >> throwOne;
    return; //this is not required
}

void SecondThrow(int throwTwo) //see note 1
{
    cout << "Enter your second score: ";
    cin >> throwTwo;
    return; //again useless
}

void ThirdThrow(int throwThree) //see note 1
{
    cout << "Enter your third score: ";
    cin >> throwThree;
    return; //useless
}

void FindErrors()
{
    int throwOne, throwTwo, throwThree; //see note 2
    if (throwOne > 10)
    {
        cout << "Throw of " << throwOne << " is invalid because you can't roll more than 10 on one throw" << endl;
    }
    if (throwTwo > 10)
    {
        cout << "Throw of " << throwTwo << " is invalid because you can't roll more than 10 on one throw" << endl;
    }
    if (throwThree > 10)
    {
        cout << "Throw of " << throwThree << " is invalid because you can't roll more than 10 on one throw" << endl;
    }
    return; //still useless
}


Note 1: From the way you are using these variables, I am guessing you meant to pass by reference. The way you currently have it, you are using pass by value.

Note 2: These variables are not the same ones from main; instead they are uninitialized values. You should pass the ones from main to your errors function (you may use pass by value here).

EDIT: Zhuge had a huge head start and beat me in the Z order.
Last edited on
Thank you both!
Topic archived. No new replies allowed.