error in 41st line tell me whats the mistake i did everything right

Discussion in 'C++' started by wajihahmed29@gmail.com, Jul 28, 2013.

  1. Guest

    Error 41: Declaration syntax error in function main()


    #include <iostream.h>
    #include <conio.h>

    float sum(float,float);
    float sub(float,float);
    float mult(float,float);
    float div(float,float);
    float mod(float,float);

    void main()
    {
    clrscr();
    int a,b;
    char calculator;
    cout <<"Press (P) for sum, Press (S) for sub,Press (M) for mult,Press (D) for div,Press (N) for mod";
    cin >>calculator;
    switch(calculator)
    {
    case 'P':
    cout <<"Sum (a,b)";
    cin >>a >>b;

    case 'S':
    cout <<"Sub (a,b)";
    cin >>a >>b;

    case 'M':
    cout <<"Mult (a,b)";
    cin >>a >>b;

    case 'D':
    cout <<"Div (a,b)";
    cin >>a >>b;

    case 'N':
    cout <<"Mod (a,b)";
    cin >>a >>b;
    }
    float sum(float a,float b)
    {
    return a+b;
    }
    float sub(float a,float b)
    {
    return a-b;
    }
    float mult(float a,float b)
    {
    return a*b;
    }
    float div(float a,float b)
    {
    return a/b;
    }
    float mod(float a,float b)
    {
    return a%b;
    }
    getch();
    }
    , Jul 28, 2013
    #1
    1. Advertising

  2. Ian Collins Guest

    Re: error in 41st line tell me whats the mistake i did everythingright

    wrote:
    > Error 41: Declaration syntax error in function main()
    >
    >
    > #include <iostream.h>


    Don't use this old header, use <iostream>

    > #include <conio.h>
    >
    > float sum(float,float);
    > float sub(float,float);
    > float mult(float,float);
    > float div(float,float);
    > float mod(float,float);
    >
    > void main()


    That should at lest generate an warning.

    > {
    > clrscr();
    > int a,b;
    > char calculator;
    > cout <<"Press (P) for sum, Press (S) for sub,Press (M) for mult,Press (D) for div,Press (N) for mod";
    > cin >>calculator;
    > switch(calculator)
    > {
    > case 'P':
    > cout <<"Sum (a,b)";
    > cin >>a >>b;
    >
    > case 'S':
    > cout <<"Sub (a,b)";
    > cin >>a >>b;
    >
    > case 'M':
    > cout <<"Mult (a,b)";
    > cin >>a >>b;
    >
    > case 'D':
    > cout <<"Div (a,b)";
    > cin >>a >>b;
    >
    > case 'N':
    > cout <<"Mod (a,b)";
    > cin >>a >>b;
    > }
    > float sum(float a,float b)


    Missing '}' at the end of main.

    --
    Ian Collins
    Ian Collins, Jul 28, 2013
    #2
    1. Advertising

  3. No, you did not do everything right. But at least you did post to an
    on topic newsgroup.

    On Sun, 28 Jul 2013 02:43:46 -0700 (PDT),
    wrote:

    >Error 41: Declaration syntax error in function main()


    The 41 is the error number, not the line number. You need to post the
    complete text of the error message.

    >
    >
    >#include <iostream.h>


    You should be using the standard header iostream.

    >#include <conio.h>


    This header is not part of the C++ standard. Using it limits the
    number of people who can try to help you.

    > float sum(float,float);


    Horizontal white space to make your code readable is almost free.

    > float sub(float,float);
    > float mult(float,float);
    > float div(float,float);
    > float mod(float,float);
    >
    > void main()


    On a hosted system, main should return an int.

    >{
    > clrscr();
    > int a,b;


    Since all your arithmetic is performed on type float, why are these of
    type int?

    > char calculator;
    > cout <<"Press (P) for sum, Press (S) for sub,Press (M) for mult,Press (D) for div,Press (N) for mod";


    It would be nice if you left a space at the end of the text so the
    user's input did not abut up against the d in mod.

    > cin >>calculator;
    > switch(calculator)
    > {


    If you would learn to indent you code consistently, the error would
    have been obvious.

    > case 'P':
    > cout <<"Sum (a,b)";
    > cin >>a >>b;


    If you are expecting input from the user, shouldn't you prompt him for
    it.

    After receiving the input, you never call the sum function nor do you
    attempt to print any results.

    The absence of a break statement here will cause the code to fall
    through to the next case. Your screen will look something like
    Sum(a,b)xxx yyy
    Sub(a,b)xxx yyy
    Mult(a,b)...

    > case 'S':
    > cout <<"Sub (a,b)";
    > cin >>a >>b;
    >
    > case 'M':
    > cout <<"Mult (a,b)";
    > cin >>a >>b;
    >
    > case 'D':
    > cout <<"Div (a,b)";
    > cin >>a >>b;
    >
    > case 'N':
    > cout <<"Mod (a,b)";
    > cin >>a >>b;
    > }


    This brace closes the switch statement. You are still in function
    main().

    > float sum(float a,float b)


    C++ does not support defining one function within another.

    > {
    > return a+b;
    > }
    > float sub(float a,float b)
    > {
    > return a-b;
    > }
    > float mult(float a,float b)
    > {
    > return a*b;
    > }
    > float div(float a,float b)
    > {
    > return a/b;
    > }
    > float mod(float a,float b)
    > {
    > return a%b;
    > }
    > getch();


    When you input data for a and b, your input process will most likely
    end when you press the Enter key. This will cause a '\n' to be placed
    in the input stream. Your cin statement will recognize this '\n' as
    the character after the last digit in b. It will leave the '\n' in
    the stream buffer. This call the getch will retrieve that '\n' and
    return to your function without waiting for any addition keyboard
    input. It therefore serves no purpose.

    > }


    main should always return an int.

    --
    Remove del for email
    Barry Schwarz, Jul 28, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    355
    Victor Bazarov
    Aug 17, 2006
  2. yogesh
    Replies:
    1
    Views:
    359
    Victor Bazarov
    Mar 14, 2007
  3. Replies:
    4
    Views:
    507
  4. chris
    Replies:
    3
    Views:
    72
    chris
    Oct 5, 2005
  5. Mark Lawrence
    Replies:
    63
    Views:
    160
    Robert Kern
    May 17, 2014
Loading...

Share This Page