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. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    Question 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 answer1;
        int fourthnumber;
        int fifthnumber;
        int sixthnumber;
        int answer2;
        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;
            answer1=1;
        }
        else {
            cout<< "Oops got that one wrong :/" <<endl;
            answer1=0;
    
    
        }
        if (answer1) {
            cout << "Now we will check if your subtracting is as good as your adding" <<endl;
        }
        else {
            cout << "Maybe your subtracting is better than your adding!" <<endl;
        }
    
    
        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();
    
    
        if (fourthnumber-fifthnumber==sixthnumber && answer1) {
            cout << "Well done! Correct again" <<endl;
            answer2=1;
        }
        else if (fourthnumber-fifthnumber==sixthnumber && !answer1){
            cout << "At least your subtracting is better than your adding!" <<endl;
            answer2=1;
        }
        else if (!fourthnumber-fifthnumber==sixthnumber && answer1) {
            cout << "At least your adding is better than your subtracting" <<endl;
            answer2=0;
        }
        else if (!fourthnumber-fifthnumber==sixthnumber && !answer1) {
            cout << "Oops, wrong again" <<endl;
            answer2=0;
        }
    
    
        if (answer1 && answer2){
            cout << "Yay! You are great at Maths";
        }
        else if (answer1 || answer2) {
            cout << "I guess you're OK at Maths";
        }
        else if (!answer1 && !answer2){
            cout << "Damn you suck at Maths!";
        }
        cin.get();
    
    
        return 0;
    }
    Thanks in advance guys!

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    86
    Simply a shot in the dark, but a statement like this looks like it could be problematic:

    "!fourthnumber-fifthnumber==sixthnumber && !answer1"

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

    !(fourthnumber-fifthnumber==sixthnumber) && !answer1

  3. #3
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    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.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    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. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by wildcard_seven View Post
    Simply a shot in the dark, but a statement like this looks like it could be problematic:

    "!fourthnumber-fifthnumber==sixthnumber && !answer1"

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

    !(fourthnumber-fifthnumber==sixthnumber) && !answer1
    In fact, it is problematic because ! binds harder than -, so the statement becomes
    !(fourthnumber)-fifthnumber==sixthnumber && !answer1

    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;
            answer2=1;
        }
        else if (fourthnumber-fifthnumber==sixthnumber && !answer1){
            cout << "At least your subtracting is better than your adding!" <<endl;
            answer2=1;
        }
    The "fourthnumber-fifthnumber==sixthnumber" part is common to both if statements, so you can break it out:

    Code:
    if (fourthnumber - fifthnumber == sixthnumber)
    {
    	if (answer1)
    	{
    		std::cout << "Well done! Correct again" << std::endl;
    		answer2 = 1;
    	}
    	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){
    	{
    	        std::cout << "At least your subtracting is better than your adding!" << std::endl;
    		answer2 = 1;
    	}
    }
    Well, keep it up!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    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;
        cout<< "Please input your two numbers";
        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. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    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. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by TheMomento View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    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;
        cout<< "Please input your two numbers";
        cin>> a >> b;
        cin.ignore();
        cout<< "The two numbers multiplied together are" << mult() <<endl;
        }
    int mult(int a, int b)
    {
        return a*b;
    }

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    You don't pass along the appropriate parameters (arguments) to mult:
    Code:
    cout<< "The two numbers multiplied together are" << mult(a, b) <<endl;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    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. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by TheMomento View Post
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C code problem
    By neo28 in forum C Programming
    Replies: 24
    Last Post: 05-16-2009, 10:48 AM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 08:10 PM
  4. Problem with very simple code
    By pizzapie in forum C++ Programming
    Replies: 21
    Last Post: 10-22-2004, 09:08 AM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 11:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21