Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Parse error, and I can't fix it. =/ (http://www.go4expert.com/forums/parse-error-i-fix-t12571/)

pikagod 30Jul2008 12:03

Parse error, and I can't fix it. =/
 
alright, I've been working on this code for about 20 min, and I think i know why its giving me the parse error. I wrote a line (b = +) and it works if I change the + to a number, so I'm thinking the + symbol just isn't accepted. I've tried changing it to the char b instead of int b and had no luck, maybe someone can please help me. the code is below.

Code:

#include <stdio.h>

int main()
{
        int z;
        z = 0;
        while (z < 1);
        {
                int a;
                scanf("%d", &a);
                {
                        char b;
                        scanf("%d", &b);
                        if (b = +);
                                {        int c, d;
                                        scanf("%d", &c);
                                        d = a + c;
                                        printf("%d + %d = %d"), a, c, d;
                                        return 0;
                                }
                        if (b = -)
                                {
                                        int c, d;
                                        scanf("%d", &c);
                                        d = a - c;
                                        printf("%d - %d = %d"), a, c, d;
                                        return 0;
                                }
                        if (b = *)
                                {
                                        int c, d;
                                        scanf("%d", &c);
                                        d = a * c;
                                        printf("%d * %d = %d"), a, c, d;
                                        return 0;
                                }
                        if (b = /)
                                {
                                        int c, d;
                                        scanf("%d", &c)
                                        d = a / c;
                                        printf("%d / %d = %d", a, c, d;
                                        return 0;
                                }
                }
               
        return 0;
}

any help or suggestions appreciated, thanks ^.^

xpi0t0s 30Jul2008 15:11

Re: Parse error, and I can't fix it. =/
 
If you're testing for equality use == not =. = assigns the value and evaluates TRUE or FALSE depending on whether or not the assigned value is zero. So if (b=1) will assign 1 to b and evaluate TRUE, as will if (b=23) and if (b=-50000), whereas if (b=0) will assign 0 to b and evaluate FALSE, so if (b=0) printf("foo"); will never print foo.

If you're trying to specify the + character you need to include it in single quotes. + is a symbol that will be interpreted by the compiler as an attempt to add two numbers, and will throw an error in the context you've given. '+' on the other hand means the character, and '+' is a numeric constant that will compare equally happily against char or int.

So I guess what you're looking for is if (b=='+') and so on.

Unless you know the ASCII code of '+' though scanf("%d",&b) won't work if you input just the character +. %d evaluates the input as a number and if it returns anything other than an error it will be the value 0. Given just a plus sign it will have effectively the same problem as the compiler in "if (b=+)". Use %c to scan for a single character.

Also your code has two infinite loop bugs. Actually I'm surprised it runs at all, at least to the point of asking for input. while (z < 1); will loop forever waiting for z to exceed 0, which it never will, so it'll never get to the next line, UNLESS your compiler spots this bug and ignores the incorrect semicolon at the end.

The second infinite loop bug is when you remove the incorrect semicolon; nothing modifies z so your loop will never exit. That might be your intention though, if you're completely happy with just using ctrl-C or kill -9 to kill the program, but a better design would be to input something that indicates whether or not the user wishes to continue, then exit gracefully if they don't. However as each if block has a return 0; in it the program will just exit after one operation regardless of the while loop unless b isn't one of +-*/, so why is the while in there at all?

Silcrome 30Jul2008 18:25

Re: Parse error, and I can't fix it. =/
 
your while loop also doesn't have a corresponding closing bracket that i see....so it shouldnt' compile to run in the first place to find that parsing doesn't work...

xpi0t0s 30Jul2008 18:33

Re: Parse error, and I can't fix it. =/
 
That's true but it would probably fall over on "if (b = +);" long before it found there was a missing }.
(I think the parse error the OP means is a compile error rather than a runtime error.)

pikagod 30Jul2008 18:45

Re: Parse error, and I can't fix it. =/
 
Thank you! I changed the b = all to b == and added ' ' at the characters and it built correctly, still working out a few kinks but making progress :D.

pikagod 30Jul2008 18:51

Re: Parse error, and I can't fix it. =/
 
I didn't think about the program ending after the problem, so I suppose the while loop is completely pointless.....I really hadn't thought about it.

pikagod 30Jul2008 18:55

Re: Parse error, and I can't fix it. =/
 
now the problem I'm having is after I've gotten it built. It takes the numbers in, but does the equation wrong. I'm not sure why and still looking at it atm.

also, if you know a good way to end the program "elegantly" please tell me. The guide I've been using to learn is helpful but not always having the info I need.

and sorry for the three posts in a row.

Silcrome 30Jul2008 19:00

Re: Parse error, and I can't fix it. =/
 
if you want it to end if none of those 4 symbols is there change it to if-else-if statements until the last one and the last else make it change your z to 1....or make it a switch statement with the default case of killing the program...or removed the while loop and have it go through the operator sentence and end with a conditional z being 0 if it worked or 1 if it didn't work so if this go to a larger application later you already have an error check in there to see if it parsed the operator sentence correctly. I don't know if youd call it "elegent" to end a program in the standard way..i consider elegent something spiffy that the majority wouldn't think of, but those are what ive got

-silcrome

xpi0t0s 30Jul2008 19:10

Re: Parse error, and I can't fix it. =/
 
"does the equation wrong" isn't sufficient to determine what is wrong. What exactly isn't it doing? What input did you give it? What output did you get? Have you tried displaying the input after the computer has accepted it to make sure the data is correct?

Silcrome 30Jul2008 19:13

Re: Parse error, and I can't fix it. =/
 
Quote:

Originally Posted by xpi0t0s
"does the equation wrong" isn't sufficient to determine what is wrong. What exactly isn't it doing? What input did you give it? What output did you get? Have you tried displaying the input after the computer has accepted it to make sure the data is correct?

he said he is still looking into it...give him a minute maybe he is learning to fish instead of just asking for some


All times are GMT +5.5. The time now is 14:05.