Archived

This topic is now archived and is closed to further replies.

C++: weird error on my code

This topic is 5040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I got this weird error in my program. Please help me with this because I''ve been working on this for the whole day and I don''t have a clue what is wrong with my code. #include <stdio.h> #include <conio.h> #include <math.h> class calculator { public: static void addition(); static void subtraction(); static void multiplication(); static void division(); static void squareroot(); { addition(); subtraction(); multiplication(); division(); squareroot(); squareroot(); } private: double a; double b; }; main(void) { calculator compute; int a; do { printf("choose an operation"); printf("\n1. addition"); printf("\n2. subtraction"); printf("\n3. multiplication"); printf("\n4. division"); printf("\n5. squareroot"); } while (a!=1 || a!=2 || a!=3 || a!=4 || a !=5); if (a==1) { compute.addition(); } else if (a==2) { compute.subtraction(); } else if (a==3) { compute.multiplication(); } else if (a==4) { compute. division(); } else if (a==5) { compute.squareroot(); } else { printf("invalid selection"); } calculator::addition(); { printf("\nEnter first number: "); scanf("%d",&a); printf("Enter second number: "); scanf("%d",&b); a=a+b; printf("The answer is: %d",a); printf("\nPress any key: \n"); getche(); return(0); } calculator::subtraction(); { printf("\nEnter first number: "); scanf("%d",&a); printf("\nEnter second number: "); scanf("%d",&b); a=a-b; printf("The answer is: %d",a); getche(); main(); } calculator::multiplication(); { printf("\nEnter first number: "); scanf("%d",&a); printf("Enter second number: "); scanf("%d",&b); a=a*b; printf("\nThe answer is: %i",a); printf("Press any key: "); getche(); main(); } calculator::division(); { printf("\nEnter first number: "); scanf("%d",&a); printf("\nEnter second number: "); scanf("%d",&b); a= a/b; printf("The answer is: %d",a); getche(); main(); } calculator::squareroot(); { double answer; printf("enter a number: "); scanf("%d",&answer); answer=sqrt(answer); printf("the squareroot is %d",answer); getche(); main(); } } errors: Calculator2.cpp C:\Cpp\Calculator2.cpp(13) : error C2059: syntax error : ''{'' C:\Cpp\Calculator2.cpp(13) : error C2334: unexpected token(s) preceding ''{''; skipping apparent function body C:\Cpp\Calculator2.cpp(50) : error C2065: ''b'' : undeclared identifier Error executing cl.exe. Flamers are worst than Newbies

Share this post


Link to post
Share on other sites
Put your code in source tags, it makes it easier to read (edit my post to see what they look like).


static void squareroot();
{
addition();
subtraction();
multiplication();
division();
squareroot();
squareroot();
}


The braces and in fact the existence of that block of code is illegal where you have placed it (within the class definition).

EDIT:
Okay, I thought I had less time to answer you than I actually do, so I'm gonna mention a few more things:

1) Your main input loop will run forever, since within it you never actually get any input from the user. The loop guard itself is incorrect because if a is, say, 2 (which is valid input) the condition (a != 1) will be true and thus the loop guard will be true and you will loop when you don't intend to.

2) It is generally regarded as "bad" to make your functions perform direct I/O. For example, rather than making addition() prompt the user for the two numbers, pass them as parameters. This way you divorce the user interface from the actual functionality, which makes your code more reusable.

3) Along the same lines, you can just as easily do your addition/subtraction/whatever on local variables within the functions, rather than using class members (a and b).

4) A switch statement is better suited to than an if/else waterfall for dealing with user input in the way you are. It makes your intent a little clearer.

5) All of your math functions are declared as returning nothing (void), yet you return (0) from addition(). And for the rest, you call main() recursively. That's kind of sloppy.


Most of the above, except for point 1, are not completly fatal errors, but they're definately ugly usage of the language.


[edited by - jpetrie on February 19, 2004 12:05:31 AM]

[edited by - jpetrie on February 19, 2004 12:09:34 AM]

Share this post


Link to post
Share on other sites
quote:
do
{
printf("choose an operation");
printf("\n1. addition");
printf("\n2. subtraction");
printf("\n3. multiplication");
printf("\n4. division");
printf("\n5. squareroot");
} while (a!=1 || a!=2 || a!=3 || a!=4 || a !=5);


You didnt read the keyboard, try this:

{
printf("choose an operation");
printf("\n1. addition");
printf("\n2. subtraction");
printf("\n3. multiplication");
printf("\n4. division");
printf("\n5. squareroot");
scanf("%d",&a);
} while (a!=1 || a!=2 || a!=3 || a!=4 || a !=5);

Share this post


Link to post
Share on other sites
In your functions you don't need a ; at the end of the first line and you should specify the type...you don't need to call main either the function simply returns when it reaches the end...
You want your functions to look something like this..


void calculator::addition()
{
//blah
}


Oh yeah and use "void" instead of "static void"


Now, "a" isn't set to 0 automatically since it is a local variable so you need to give it a starting value so it isn't undefined, just set it to zero. Also for your while loop you probably want something like this instead...

while( a < 1 && a > 5 );


[edited by - JohnyB on February 19, 2004 12:35:34 AM]

Share this post


Link to post
Share on other sites
okay thanks jpetrie I'll try to follow your suggestion.


In your functions you don't need a ; at the end of the first line and you should specify the type...you don't need to call main either the function simply returns when it reaches the end...
You want your functions to look something like this..


void calculator::addition() { //blah }


Oh yeah and use "void" instead of "static void"


Now, "a" isn't set to 0 automatically since it is a local variable so you need to give it a starting value so it isn't undefined, just set it to zero. Also for your while loop you probably want something like this instead...

while( a < 1 && a > 5 );


yeah I tried using void but the compiler suggested to use static void instead. another error I'm having, is within my class which is in line 13.

class calculator
{
public:
static void addition();
static void subtraction();
static void multiplication();
static void division();
static void squareroot();
{
addition();
subtraction();
multiplication();
division();
squareroot();
squareroot();
}
private:
double a;
double b;
};

The Error says "C:\Cpp\Calculator2.cpp(13) : error C2059: syntax error : '{'
C:\Cpp\Calculator2.cpp(13) : error C2334: unexpected token(s)

preceding '{'; skipping apparent function body"
I'm completely clueless in this one.

btw: what is the code for source tag and quote tag?



Flamers are worst than Newbies[b]

[edited by - makoy on February 21, 2004 6:26:52 AM]

Share this post


Link to post
Share on other sites
quote:
yeah I tried using void but the compiler suggested to use static void instead. another error I''m having, is within my class which is in line 13.

class calculator
{
public:
static void addition();
static void subtraction();
static void multiplication();
static void division();
static void squareroot();
{
addition();
subtraction();
multiplication();
division();
squareroot();
squareroot();
}
private:
double a;
double b;
};

Use void. If the compiler suggested "static void" it''s because you''re not calling the functions correctly. Static functions cannot access "a" or "b" so you definitely do not want them.

In the block of code above, you for some reason insert braces and function calls and I don''t know what your intent was, but you should change it to:

class calculator
{
public:
void addition();
void subtraction();
void multiplication();
void division();
void squareroot();

private:
double a;
double b;
};


~CGameProgrammer( );

Screenshots of your games or desktop captures -- Post screenshots of your projects. There''s already 134 screenshot posts.

Share this post


Link to post
Share on other sites
quote:
Original post by JohnBolton
The reason for the errors is the semicolon shown below. It should not be there.

static void squareroot(); <--- remove this semicolon
{
addition();
subtraction();
multiplication();
division();
squareroot();
squareroot();
}



No, the entire thing is a mistake. If that was supposed to be his square root function, then besides not being remotely correct, it would also be an infinite loop.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Post screenshots of your projects. There''s already 134 screenshot posts.

Share this post


Link to post
Share on other sites
Thanks to all of you guys! NobodyNews I guess I''ll follw your code it''s much simplier compare to mine. Truth is I do''nt have any Idea bout static void functions. When I compile my code tha compiler suggest that I should use static instead. For some reasons, I don''t know. I just followed what the compiler said. But still, it did''nt work.

Share this post


Link to post
Share on other sites