Announcement

Collapse
No announcement yet.

need some help with c++

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • need some help with c++

    Hi guys, i was wondering if anyone could help me with a c++ project..
    I wrote this code in dev c++ but for some reason it wouldnt compile..it says the syntax in the "switch/case" is wrong but i cant find any errors <_<
    i am a newb and i just started learning c++ a few days ago..
    heres my code:

    #include <iostream>

    using namespace std;


    int input;

    int main()
    {
    cout<<"Please make a selection from the following: \n";
    cout<<"To add press 1\n";
    cout<<"To multiply press 2\n";
    cout<<"To subtract press 3\n";
    cin>> input;

    switch (input)
    {
    case 1:
    add(int a, int b);
    break;

    case 2:
    mult(int c, int d);
    break;

    case 3:
    sub(int e, int f);
    break;

    default:
    cout<<"Wrong input, Quitting.\n";
    break;
    }
    }

    int add(int a, int b)
    {
    cout<<"Please enter two numbers\n";
    cin>> a >> b;
    cout<<"The total is: " << add(a,b) <<"\n";
    return a + b;
    }

    int mult(int c, int d)
    {
    cout<<"Please enter two numbers to multiply\n";
    cin>> c >> d;
    cout<<"The product is: " << mult (c,d) <<"\n";
    return c * d;
    }

    int sub(int e, int f)
    {
    cout<<"Please enter two numbers to subtract\n";
    cin>> e >> f;
    cout<<"The total is: " << sub(e,f) <<"\n";
    return e - f;
    }




    any help would be much appreciated.
    thx
    1: TNTkilla> wow i cant see them
    1: PRiMORDiAL> u laggot
    1: funny_porkchop> move your dick out the way :P
    1: PRiMORDiAL> loooool

  • #2
    Your <[<wrong/input.delete)) is obviously in the wrong place
    Originally posted by Facetious
    edit: (Money just PMed me his address so I can go to Houston and fight him)

    Comment


    • #3
      Originally posted by funny_porkchop
      default:
      cout<<"Wrong input, Quitting.\n";
      break;
      }
      }
      Remove one of the '}'s

      Comment


      • #4
        Originally posted by Nockm
        Remove one of the '}'s
        nope still dont work
        1: TNTkilla> wow i cant see them
        1: PRiMORDiAL> u laggot
        1: funny_porkchop> move your dick out the way :P
        1: PRiMORDiAL> loooool

        Comment


        • #5
          Originally posted by ZeUs!!
          Your <[<wrong/input.delete)) is obviously in the wrong place
          1: TNTkilla> wow i cant see them
          1: PRiMORDiAL> u laggot
          1: funny_porkchop> move your dick out the way :P
          1: PRiMORDiAL> loooool

          Comment


          • #6
            I haven't coded for a while in c++ but I can see two mistakes in main. I'm not quite sure about that switch semicolon but i think it is necessary.

            Originally posted by funny_porkchop
            Hi guys, i was wondering if anyone could help me with a c++ project..
            I wrote this code in dev c++ but for some reason it wouldnt compile..it says the syntax in the "switch/case" is wrong but i cant find any errors <_<
            i am a newb and i just started learning c++ a few days ago..
            heres my code:

            #include <iostream>

            using namespace std;

            declarations for your functions go here

            int input;

            int main()
            {
            cout<<"Please make a selection from the following: \n";
            cout<<"To add press 1\n";
            cout<<"To multiply press 2\n";
            cout<<"To subtract press 3\n";
            cin>> input;

            switch (input)
            {
            case 1:
            add(int a, int b);
            break;

            case 2:
            mult(int c, int d);
            break;

            case 3:
            sub(int e, int f);
            break;

            default:
            cout<<"Wrong input, Quitting.\n";
            break;
            }; //I'm pretty sure switch expects a semicolon at the end

            return 0; //you specified you are going to return a int and never did so
            }

            thx
            Last edited by Force of Nature; 01-15-2005, 08:20 AM.
            Jav Guide: Jav Guide

            Too bad you have to be a pallie to see it

            Comment


            • #7
              Originally posted by funny_porkchop
              nope still dont work
              Yeah mb. What you need is to declare int a,b,c,d,e,f; and get the main() function to return an int.

              The semicolon as described above isn't needed.

              Comment


              • #8
                Hows your sister?
                I don't know how to put this but I'm kind of a big deal...

                Comment


                • #9
                  Originally posted by Force of Nature
                  I haven't coded for a while in c++ but I can see two mistakes in main. I'm not quite sure about that switch semicolon but i think it is necessary.
                  ah i should have declared the functions earlier before main()
                  thx a lot force of nature and everyone else
                  1: TNTkilla> wow i cant see them
                  1: PRiMORDiAL> u laggot
                  1: funny_porkchop> move your dick out the way :P
                  1: PRiMORDiAL> loooool

                  Comment


                  • #10
                    Code:
                    #include <iostream>
                    using namespace std;
                    int input;
                    void add();
                    void mult();
                    void sub();
                    int main()
                    {
                    	cout<<"Please make a selection from the following: \n";
                    	cout<<"To add press 1\n";
                    	cout<<"To multiply press 2\n";
                    	cout<<"To subtract press 3\n";
                    	cin>> input;
                    	switch (input)
                    	{
                    		case 1:
                    			add();
                    			break;
                    		case 2:
                    			mult();
                    			break;
                    		case 3:
                    			sub();
                    			break;
                    		default:
                    			cout<<"Wrong input, Quitting.\n";
                    			break;
                    	}
                    	return 0;
                    }
                    void add()
                    {
                    	int a,b;
                    	cout<<"Please enter two numbers\n";
                    	cin>> a >> b;
                    	cout<<"The total is: " << a+b <<"\n";
                    }
                    void mult()
                    {
                    	int c,d;
                    	cout<<"Please enter two numbers to multiply\n";
                    	cin>> c >> d;
                    	cout<<"The product is: " << c*d <<"\n";
                    }
                    void sub()
                    {
                    	int e,f;
                    	cout<<"Please enter two numbers to subtract\n";
                    	cin>> e >> f;
                    	cout<<"The total is: " << e-f <<"\n";
                    }
                    You forgot to declare your functions before main.
                    [Edit: I also took the liberty of making them void since the results were not used outside of the functions themselves, and you didn't have to pass anything in or make all variable names seperate in individual functions, those variables only exist in that function and you can use the same names again in different ones. If you planned on using the results, just switch them to ints and put back the return statements.]
                    Last edited by Richard Creager; 01-15-2005, 10:07 AM.
                    sage

                    Comment


                    • #11
                      Also, making variables that are only used locally global is a bad habit. (in this case, int input)

                      Comment

                      Working...
                      X