n00b problems

This is a discussion on n00b problems within the C++ Programming forums, part of the General Programming Boards category; Hi, I am a big n00b using dev c++ . I tried to make a sort of calculator using what ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    Question n00b problems

    Hi, I am a big n00b using dev c++. I tried to make a sort of calculator using what I have learnt so far from the tutourials but I get lots of compile errors and have no idea what is going on.


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int problem(int x, int y, int z, short operation);
    
    int main()
    {
        int x;
        int y;
        int z;
        bool q = 0;
        short operation;
        
        cout<<"Enter operation. Then x and y.";
        for(q = 0);
        {
            cout<<"1.* \n 2./ \n 3.- \n 4.+";
            cin>>operation;
            cin>>x>>y;
            problem(x, y, z, operation);
            cout<<"Quit? [0/1]";
            cin>>q;
        }
        cin.get();
        return 0;
    }
    
    int problem(int x, int y, int z, short operation)
    {
    
        switch (operation)
        {
               case 1:
                    x * y = z;
               case 2:
                    x / y = z;
               case 3:
                    x - y = z;
               case 4:
                    x + y = z;
        };
        return z;
    }

    These are the compiler errors.
    Code:
          In function `int main()': 
    16  expected `;' before ')' token 
    17  expected primary-expression before '{' token 
    17  expected `;' before '{' token 
    17  expected primary-expression before '{' token 
    17  expected `)' before '{' token 
          In function `int problem(int, int, int, short int)': 
    34  non-lvalue in assignment 
    36  non-lvalue in assignment 
    38  non-lvalue in assignment 
    40  non-lvalue in assignment
    Can you tell me wat im doing wrong?

  2. #2
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Alright, I corrected your code and I will explain what I did.
    Code:
    #include <iostream>
    #include <fstream>                              //<----why are you using this? Its only for reading
    using namespace std;                            and writing files
    
    int problem(int x, int y, short operation);             //doesn't need int z
    
    int main()
    {
        int x;
        int y;
        int z;
        bool q = true;
        short operation;
        
        cout<<"Enter operation. Then x and y."<<endl;
        while(q)
        {
            cout<<" 1.* \n 2./ \n 3.- \n 4.+"<<endl;
            cin>>operation;
            cin>>x>>y;
            z = problem(x, y, operation);           //if you return something,  you need to make it return
            cout<<"Your answer is "<<z<<endl;       //to something, like z.
            cin.get();                               //<---on my comp, when i pressed enter it went too
            cout<<"Quit? [0/1]";                     //fast so i put this in to slow it down. 
            cin>>q;
        }
        cin.get();
        return 0;
    }
    
    int problem(int x, int y, short operation)      //you don't need z because you can identify it in
    {                                               //the function then return it.
        int z;
        switch (operation)
        {
               case 1:
                    z = x * y;                      //all your equations must have z = first 
                    break;                          //you need break or it'll mess up and the 
               case 2:                              //answer will always be addition
                    z = x / y;
                    break;
               case 3:
                    z = x - y;
                    break;
               case 4:
                    z = x + y;
                    break;
                    
        };
        return z;
    }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    2
    Thanks, it worked heaps well. Cya

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  4. Another n00b with pointer problems!
    By jorisvm in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2003, 08:10 AM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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