Just starting out!

This is a discussion on Just starting out! within the C++ Programming forums, part of the General Programming Boards category; hi there, I have started to go through the tourtorials so far i have got up to the case section ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    1

    Just starting out!

    hi there, I have started to go through the tourtorials so far i have got up to the case section and decided to try and make a simple form of calculator i suspose you could call it. To see if i could still remember the stuff from the earlier tutorials. so far i have written the coding below but when i try to compile i get this error

    " line 26 error: expected initializer before "int"

    Code:
    #include <iostream>
    
    using namespace std;
    
    int input;
    int mult ( int x, int y);
    
    
    int main()
    {
        int x;
        int y;
    
        int main();
    {
    
        cout<<"enter two numbers (sperated by a space): ";
        cin>> x >> y;
        cin.ignore ();
        cout<<"the answer is "<< mult ( x, y ) <<"\n";
        cin.get();
    }
    
    int mult ( int x, int y )
    
    int input;
    
      cout<<"1. multiply\n";
      cout<<"2. devide\n";
      cout<<"3. subtract\n";
      cout<<"4. add\n";
      cout<<"Selection: ";
      cin>>input;
      switch ( input ) {
      case 1:
        {
    return x * y;
    }
        break;
      case 2:
        {
    return x / y;
    }
        break;
      case 3:
        {
    return x - y;
    }
        break;
      case 4:
        {
    return x + y;
    }
        break;
      default:
        cout<<"Error, bad input, quitting\n";
        break;
      }
    }
    the code is a combonation of the tutorial and what i made my self. as far as i know i could be a millons miles off the actual code i need. any help would be greatly accepted so that i know how to avoid the error in the future as well.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >int main()
    >{
    > int x;
    > int y;
    >
    > int main();
    >{

    You've got main() defined twice.

    >int mult ( int x, int y )
    You left out the opening brace for this function.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And there's no need to declare variable input as a global on the line which follows using namespace std;

  4. #4
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    Get rid of
    Code:
     int main(); {
    i think that
    Code:
    int mult ( int x, int y )
    is also wrong...
    I think you need
    Code:
    int mult ( int x, int y ){ 'code }

  5. #5
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38
    I think you also need to define mult function
    Code:
    int mult ( int x, int y ){ 'code to do what ever function will do }
    because you got prototype, but it is not correctly defined anywhere

  6. #6
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    I'm just going to fix formatting issues here and let you deal with the code as needs be. Compare to your original code so you don't make these same mistakes again.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int input;
    int mult ( int x, int y);
    
    int main()
    {
        int x;
        int y;
    // You had an extra declaration of main here. Too much copy and paste, I think.
        cout<<"enter two numbers (seperated by a space): ";
        cin>> x >> y;
        cin.ignore ();
        cout<<"the answer is "<< mult ( x, y ) <<"\n";
        cin.get();
    }
    
    int mult ( int x, int y )
    {
    int input;
    
      cout<<"1. multiply\n";
      cout<<"2. devide\n";
      cout<<"3. subtract\n";
      cout<<"4. add\n";
      cout<<"Selection: ";
      cin>>input;
      switch ( input ) { // The brackets in every one of your case statements...
      case 1:            // ... didn't do anything (good or bad) in this case...
        return x * y;    // ... so I removed them. In switch statements it's the
        break;           // ... break statement that's important.
      case 2:
        return x / y;
        break;
      case 3:
        return x - y;
        break;
      case 4:
        return x + y;
        break;
      default:
        cout<<"Error, bad input, quitting\n";
      }  // of course you do need brackets around the whole case statement.
    }
    There are still alot of issues with the code, but this should get you to compiling it.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting programs
    By Molokai in forum C Programming
    Replies: 1
    Last Post: 04-16-2009, 10:10 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  4. question about reading inputs starting with 0
    By jibbles in forum C Programming
    Replies: 8
    Last Post: 08-09-2004, 03:27 AM
  5. 12 year old starting...
    By Xterria in forum Game Programming
    Replies: 2
    Last Post: 09-24-2001, 07:33 PM

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