# Tutorial 2 Code Problem (Simple but I'm new to it all)

This is a discussion on Tutorial 2 Code Problem (Simple but I'm new to it all) within the C++ Programming forums, part of the General Programming Boards category; Tl;dr: Why doesn't this work? So I've begun working through the tutorials on the website to start learning to code ...

1. ## Tutorial 2 Code Problem (Simple but I'm new to it all)

Tl;dr: Why doesn't this work?

So I've begun working through the tutorials on the website to start learning to code as a hobby. For the second tutorial on If statements and booleon algebra I wrote a program which tests the user's adding and subtracting skills and congratulates them, or not, appropriately. I'm copying and pasting all the code here, which I haven't commented on because I'm assuming it is so basic you guys will be able to figure it out easily. The problem arises when I run it and get both the answers wrong (purposefully obviously), instead of saying what it should say "Damn you suck at maths", it says "I guess you're ok at maths". I'm not sure if this is a problem with the syntax of the first "else if" or the second. Not really a huge problem as the program doesn't bother me but I'd like to know if there is a key bit of syntax that I haven't understood.
Code:
```#include <iostream>

using namespace std;

int main()
{
int firstnumber;
int secondnumber;
int thirdnumber;
int fourthnumber;
int fifthnumber;
int sixthnumber;
cout << "This program will check if your adding and subtracting is correct" <<endl;
cout << "Type your first number: ";
cin >> firstnumber;
cin.ignore();
cout << "Type your second number: ";
cin >> secondnumber;
cin.ignore();
cout << "Type what you think they add up to: ";
cin >> thirdnumber;
cin.ignore();
if (firstnumber+secondnumber==thirdnumber){
cout<< "That's the first one right, good job!" << endl;
}
else {
cout<< "Oops got that one wrong :/" <<endl;

}
}
else {
}

cout << "Enter your first number: ";
cin >> fourthnumber;
cin.ignore();
cout << "Now enter the number you are subtracting from it: ";
cin >> fifthnumber;
cin.ignore();
cout << "Now enter what you think the answer is: ";
cin >> sixthnumber;
cin.ignore();

cout << "Well done! Correct again" <<endl;
}
}
else if (!fourthnumber-fifthnumber==sixthnumber && answer1) {
}
else if (!fourthnumber-fifthnumber==sixthnumber && !answer1) {
cout << "Oops, wrong again" <<endl;
}

cout << "Yay! You are great at Maths";
}
cout << "I guess you're OK at Maths";
}
cout << "Damn you suck at Maths!";
}
cin.get();

return 0;
}```

2. Simply a shot in the dark, but a statement like this looks like it could be problematic:

I'd add parenthesis to ensure you're getting exactly what you're asking for

3. most likely the problem is where your using !
line 60: is doing what you think its doing.
line 64: the "not' ! operator isn't doing what you think it is.
try this line
Code:
`else if (fourthnumber-fifthnumber != sixthnumber && answer1)`
same for line 68

Line 80 looks like the correct usage aswell.

4. Oh ok wildcard_seven thanks that solved it, I guess it thought I meant just the fourth number isn't true not the whole equation.
Thanks for your help as well nor!
Cheers guys

5. Originally Posted by wildcard_seven
Simply a shot in the dark, but a statement like this looks like it could be problematic:

I'd add parenthesis to ensure you're getting exactly what you're asking for

In fact, it is problematic because ! binds harder than -, so the statement becomes

Aside from that, some improvements to your program might be arrays, boolean variables (bool) and revised logic.
For example, this:

Code:
```    if (fourthnumber-fifthnumber==sixthnumber && answer1) {
cout << "Well done! Correct again" <<endl;
}
}```
The "fourthnumber-fifthnumber==sixthnumber" part is common to both if statements, so you can break it out:

Code:
```if (fourthnumber - fifthnumber == sixthnumber)
{
{
std::cout << "Well done! Correct again" << std::endl;
}
else /* Nitpicking: this should be !answer1, be we know if it isn't true, then it must be false */ //else if (fourthnumber-fifthnumber==sixthnumber && !answer1){
{
}
}```
Well, keep it up!

6. Oh ok thanks elysia, that makes loads more sense actually. I'm just confused as to what std:: before cout does?
Also, to prevent starting another thread, would you mind just telling me where I've gone wrong here as well?
I'm actually writing something with more functions but i've shown just the error that I'm getting here to make it easier:
Code:
```#include <iostream>

using namespace std;

int mult( int a, int b );

int main()
{
int a;
int b;
cout<< "This program multiply your two numbers together" <<endl;
cin>> a >> b;
cin.ignore();
cout<< "The two numbers multiplied together are" << mult() <<endl;
}
int mult()
{
return a*b
}```
The compiler says that the prototype of mult has an error in it but I can't for the life of me figure out what it is, the program is practically identical to the one in the tutorial...
Thanks again

7. You function prototype, function call, function implementation must all match as to the number an type of parameters. Your prototype is different than the other two. However I would say the prototype is correct and the function call and implementation should have two parameters.

Jim

8. Originally Posted by TheMomento
Oh ok thanks elysia, that makes loads more sense actually. I'm just confused as to what std:: before cout does?
This tells the compiler "this identifier lies in the namespace std, so look there."
You have to explicitly do this, or the compiler can't find what it's looking for. There is also an additional way, and that is to use "using namespace std;".
This tells the compiler, "take the contents of the std namespace and dump it into the global namespace."
And everything that is in the global namespace does not need a prefix (although you can type ::cout as well, for example).

Also, to prevent starting another thread, would you mind just telling me where I've gone wrong here as well?
I'm actually writing something with more functions but i've shown just the error that I'm getting here to make it easier:
The problem is with scopes. Inside the function mult, a and b does not exist. They only exist inside main.
The best solution to this (not the only) is to pass this information to mult, such as this:

Code:
```int mult(int num1, int num2)
{
return num1 * num2;
}

// In main
mult(a, b); // Pass along the numbers a and b.```

9. I've changed it to this based on what you said but it still says there is an error :/
Code:
```#include <iostream>

using namespace std;

int mult( int a, int b );

int main()
{
int a;
int b;
cout<< "This program multiply your two numbers together" <<endl;
cin>> a >> b;
cin.ignore();
cout<< "The two numbers multiplied together are" << mult() <<endl;
}
int mult(int a, int b)
{
return a*b;
}```

10. You don't pass along the appropriate parameters (arguments) to mult:
Code:
`cout<< "The two numbers multiplied together are" << mult(a, b) <<endl;`

11. Sorry didn't mean to reply to you, was actually replying to person above you but didn't post fast enough. I see what you are saying, so int num1 and int num2 could be anything but they are equal to a and b respectively? If I were to define the integers outside of a function would they become usable across functions?

12. Originally Posted by TheMomento
I see what you are saying, so int num1 and int num2 could be anything but they are equal to a and b respectively?
num1 and num2 are placeholders, only. You say you want the function to take two numbers to multiply, so we must pass along two numbers to multiply.
We simply name those numbers that the function takes as num1 and num2.

When we do
mult(a, b);
we say, "take the values in a and b and pass those numbers along to mult."
So in essence, num1 will get the number stored in a and num2, b, but num1 and num2 are not in any way related to a and b.

If I were to define the integers outside of a function would they become usable across functions?
Yes, they would, but then they would become global variables, and they are discouraged for a reason (google global variables are bad).

13. Ok I see, this is the easiest way of doing it, I've told it to pass a,b on as a,b in the function just for ease of use.
Thanks for all of this XD I really feel I'm beginning to get to grips with it now, it's a great feeling!