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

W

wajihahmed29

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();
}
 
I

Ian Collins

Error 41: Declaration syntax error in function main()


#include <iostream.h>

Don't use this old header said:
#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.
 
B

Barry Schwarz

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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top