I need help with fixing an error

This is a discussion on I need help with fixing an error within the C++ Programming forums, part of the General Programming Boards category; Hello. I am a beginner programmer and I am writing out a simple program, for a start. But the compiler ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    I need help with fixing an error

    Hello.

    I am a beginner programmer and I am writing out a simple program, for a start. But the compiler (codeblocks) showed an error and so I am here to ask for help =)

    Here's the code -

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int mult ( int a, int b );
    
    
    int main()
    {
        int x;
        int *p;
        int a;
        int b;
    
    
        p = &x;
        cout << "Please input a number: ";
        cin >> x;
        cin.ignore();
    
    
        cout << "\nYou have entered " << *p;
        cin.get();
    
    
        cout << "\nPlease enter two numbers to be added up: ";
        cin >> a >> b;
        cin.ignore();
        cout << "\nThe result is - " << mult ( a, b );
    
    
        cin.get();
    }
    
    
    int mult ( int a, int b )
    {
        return a + b;
    }
    
    
    void Add()
    {
        cout <<"\n Add 100";
    }
    void Subtract()
    {
        cout <<"Subtract by 50";
    }
    void Multiply()
    {
        cout <<"Multiply by 100";
    }
    void Divide()
    {
        cout <<"Divide by 10";
    }
        int input;
    
    
        cout<<"1. Add\n";
        cout<<"2. Subtract\n";
        cout<<"3. Multiply\n";
        cout<<"4. Divide\n";
    
    
        cin>> input;
    
    
        switch (input) {
        case 1:
            Add();
            break;
        case 2:
            Subtract();
            break
        case 3:
            Multiply();
            break
        case 4:
            Divide();
            break
        default:
            cout<<"Input not recognized. Quitting now\n";
            break;
        }
        cin.get();
    }
    And here's the error -

    Line 53, 54 and 55
    expected constructor, destructor or type conversion before << token

    I need help to fix this error. Perhaps anyone has the freetime to teach me a thing or two?

    Reply ASAP please

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    first, don't say things like "ASAP please" when asking for help. you are the only one who thinks this is urgent.

    that being said, your problem isn't on line 53, unless the code you posted is not the code you compiled.

    your first error is on line 58, with 3 more on 77, 80, and 83.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    All your code from line 59 and beyond is outside of any function. You can't have code just lying around outside of a function.

    And "mult" for a function that adds is just plain wrong.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You have a main, then you define some functions, then you have some code outside of any function at all, and stuck a closing brace on the end, meaning there are eight closing braces but only seven opening braces. Do you not see a serious problem there?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Alright, so after a few brain scratching and thinking, I finally fixed it!

    Here it is ^.^ -

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int x;
    int a;
    int b;
    
    
        void Add()
    {
        cout <<"\n Added 100 to " << x << " and " << a + b << "\n";
    }
    void Subtract()
    {
        cout <<"Subtract 50 from " << x << " and " << a + b << "\n";
    }
    void Multiply()
    {
        cout <<"Multiplied 100 with " << x << " and " << a + b << "\n";
    }
    void Divide()
    {
        cout <<"Divided 10 to " << x << " and " << a + b << "\n";
    }
    
    
    int main()
    {
        int *p;
        int *d;
    
    
        p = &x;
        cout << "Please input a number: ";
        cin >> x;
        cin.ignore();
    
    
        cout << "\nYou have entered " << *p << ". Please hit ENTER to continue";
        cin.get();
    
    
        cout << "\nPlease enter two numbers to be added to " << x << " :";
        cin >> a >> b;
        cin.ignore();
        cout << "\nThe result is - " << a + b << ". " << "Please hit ENTER to continue";
    
    
        cin.get();
    
    
        int c;
    
    
        cout<<"1. Add\n";
        cout<<"2. Subtract\n";
        cout<<"3. Multiply\n";
        cout<<"4. Divide\n";
    
    
        cin>> c;
    
    
        switch (c) {
        case 1:
            Add();
            break;
        case 2:
            Subtract();
            break;
        case 3:
            Multiply();
            break;
        case 4:
            Divide();
            break;
        default:
            cout<<"Input not recognized. Quitting now\n";
            break;
    
    
        cin.get();
        }
    
    
        d = &c;
        cout << "Your final result is -\n" << x + a + b + *d;
    
    
    }
    Weeeeeeeeeeeeeeeee~~~! Thanks for the help guys!

    And this is for hk_mp5kpdw -
    And "mult" for a function that adds is just plain wrong.
    If it is alright, may you explain to me why it is "just plain wrong". I'm sorry, I'm still new at this

    (Edit)
    ----

    And, just one more question guys. How do I include the Add, Subtract, Multiply and Divide to x + a + b + *d ?

    What I mean is - If I select multiply, then how do I multiply x + a + b + *d by 100 ?
    Last edited by Conti; 03-01-2012 at 07:32 AM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    Quote Originally Posted by Conti View Post
    If it is alright, may you explain to me why it is "just plain wrong". I'm sorry, I'm still new at this

    because "mult" is obviously an abbreviation of "multiply", which is non-intuitive if it adds numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-04-2010, 07:24 AM
  2. Help fixing a compile time error
    By matt_570 in forum C Programming
    Replies: 2
    Last Post: 09-05-2009, 09:36 PM
  3. Need help fixing error to get correct output.
    By kctrk4 in forum C Programming
    Replies: 12
    Last Post: 04-07-2006, 10:10 PM
  4. How would I got about fixing this?
    By SlyMaelstrom in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2005, 05:34 AM
  5. Fixing paths
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 10-05-2002, 10:32 AM

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