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

Discussion in 'C' started by pikagod, Jul 30, 2008.

  1. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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 ^.^
     
    Last edited by a moderator: Jul 30, 2008
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  3. Silcrome

    Silcrome New Member

    Joined:
    Jul 28, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    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...
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.)
     
  5. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  7. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  8. Silcrome

    Silcrome New Member

    Joined:
    Jul 28, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    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
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    "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?
     
  10. Silcrome

    Silcrome New Member

    Joined:
    Jul 28, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    he said he is still looking into it...give him a minute maybe he is learning to fish instead of just asking for some
     
  11. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    alright. Well basically it is trying to add each number, regardless of what is put in. and even when you tell it to add, it isn't using the numbers you put it in. it seems to add them and use that as a, then add the second value you put in again, and set it to what seems a random number. For example I put in 4 + 6 and it spit out an answer of 10 + 6 = 43. thats just the adding, figured I'd look at one at a time.
     
  12. Silcrome

    Silcrome New Member

    Joined:
    Jul 28, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    can you post the changed code?
     
  13. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    #include <stdio.h>

    int main()
    {
    int a;
    scanf("%d", &a);
    {
    char b;
    scanf("%c", &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;
    }
    }
    }


    That's the changed code, I just took out the loop for the moment being. The only thing I can think might be causing the problem is that I declared integer a before the bracket containing c and d. But I don't think that would cause this.
     
  14. Silcrome

    Silcrome New Member

    Joined:
    Jul 28, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    so...i played with your code a bit, first off....after the if statement you dont' want that ";" because it ends the if and will always run the stuff in the brackets. secondly your printf statement printf("%d + %d = %d"), a, c, d; should look more like printf("stuff %<var type>" , variable) so the () wrap the whole thing. and lastly when running your code i found that int a and char b have the same memory address for some reason....when i move char up it runs...so id suggest for future when you try to debug printf everythign you can around teh problem...im attaching the code that works so you can see what i did..i also changed teh b to being %d and giving it the ascii value of 43, this is just because iw as testing the if statement at first and didn't notice the ";" and i didn't go back and change it

    Code:
    char b;
        int a;
        printf("what is a: ");
        scanf("%d", &a);
        printf("you entered a as: %d\n", a);
        printf("a is still %d\n", a);
        printf("address of a: %d\naddress of b: %d\n", &a, &b);
        printf("what is b: ");
        //printf("test: %d\n", a);
        scanf("%d", &b);
        printf("a: %d\n", a);
        printf("the value of b is: %c\n", b);
        printf("a before if statement %d\n", a);
        if (b=='+')
        { 
            printf("a after if statement %d\n", a);
            int c, d;
            printf("what is c: ");
            scanf("%d", &c);
            printf("the value of c is: %d\n", c);
            printf("about to add %d = %d + %d\n", d,a,c);
            d = a + c;
            printf("%d + %d = %d", a, c, d);
            return 0;
        }[\code]
     
  15. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    I made the changes you mentioned and it works fine now. I have it built and compiled. Thank you SO much for the help :)
     
  16. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Swapping variables around to solve an odd memory issue is never the correct approach, find and solve the real problem!

    I'm not clear what the problem is in the above code, I'm using Visual Studio 2005 and it works perfectly. However I'm using scanf("%c",&b) for reading into b, not %d, because b contains the operator. which is a single character (one byte). That's likely the problem.

    Essentially if you do char b; scanf("%d",&b); you have a memory corruption; scanf reads the input and writes it as an integer (4 bytes) to the address where b is stored (where you only allocated one byte). Silcrome, if you swap a and b back and use %c instead of %d, does that solve the problem?
    I checked with Visual Studio doing char b; scanf("%d",&b); and threw a debug error: stack around the variable 'b' was corrupted.

    Ah, yes, just read your post a bit more accurately. "i also changed teh b to being %d and giving it the ascii value of 43" So you're allocating ONE BYTE for storage of a FOUR BYTE integer. Yes, this is definitely a memory corruption.

    So here's my code:
    Code:
    void add_test1()
    {
    	int a;
    	scanf("%d", &a);
    	printf("You entered '%d'\n",a);
    
    	char b;
    	scanf("%c", &b);
    	printf("You entered '%c'\n",b);
    	if (b=='+')
    	{ 
    		int c, d;
    		scanf("%d", &c);
    		printf("You entered '%d'\n",c);
    		d = a + c;
    		printf("%d + %d = %d\n", a, c, d);
    	}
    }
    
    and the input (first line, entering 5+7 because if I entered 5 then pressed return, the scanf...&b read the end of line as the next character. This is partly why scanf is a terrible function.) and output:

    5+7
    You entered '5'
    You entered '+'
    You entered '7'
    5 + 7 = 12
     
  17. Silcrome

    Silcrome New Member

    Joined:
    Jul 28, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    i understand that changing to %d wasn't a great idea for a final solution... but i don't use scanf much and didn't understand why no matter what i was giving it was running teh inside of the if statement...once i noticed that it had a semicolon i just didn't change it back, i thought i had specified that in my post but maybe it wasn't clear that a correct solution wouldn't have that "hack" in it
     
  18. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    one last question, though it isn't NEEDED for the code. is there a command or function the will skip a line with printf, beacuase as it is the program will display the answer, then I want it to skip a line and ask if the user is finished. Ihave the code wirtten so that it asks, but it is RIGHT after the problem, same line and no space
    (sorry if some of my terms are incorrect)
     
  19. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Embed \n in the code; it prints a newline. For example printf("Hello\nWorld\n") will print
    Hello
    World
    <-- the next printf will print from here
     
  20. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Well, I dunno if this will be of any use to you guys, but here is the finished code. I have compiled it and everything, it works great.
    Code:
    #include <stdio.h>
    
    int main()
    {
    int z;
    z = 0;
    while (z < 1)
    {
    	int a;
    	scanf("%d", &a);
    	{
    		char b;
    		scanf("%c", &b);
    		if (b=='+')
    			{	
    				int c, d;
    				char e;
    				scanf("%d", &c);
    				d = a + c;
    				printf("%d + %d = %d\n", a, c, d);
    				printf("Are you finished?");
    				scanf("%c", &e);
    				if (e=='y')
    					{
    						return 0;
    					}
    			}
    		if (b=='-')
    			{
    				int c, d;
    				char e;
    				scanf("%d", &c);
    				d = a - c;
    				printf("%d - %d = %d\n", a, c, d);
    				printf("Are you finished?");
    				scanf("%c", &e);
    				if (e=='y')
    					{
    						return 0;
    					}
    			}
    		if (b=='*')
    			{
    				int c, d;
    				char e;
    				scanf("%d", &c);
    				d = a * c;
    				printf("%d * %d = %d\n", a, c, d);
    				printf("Are you finished?");
    				scanf("%c", &e);
    				if (e=='y')
    					{
    						return 0;
    					}
    			}
    		if (b=='/')
    			{
    				int c, d;
    				char e;
    				scanf("%d", &c);
    				d = a / c;
    				printf("%d / %d = %d\n", a, c, d);
    				printf("Are you finished?");
    				scanf("%c", &e);
    				if (e=='y')
    					{
    						return 0;
    					}
    			}
    	}
    }
    }
     
    Last edited by a moderator: Aug 2, 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice