Thread: need help with an if else loop

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    need help with an if else loop

    im writing this code and i cant get the if else part to work, and posts that might help would be greatly appreciated
    thanks
    Code:
    #include<stdio.h>
    #include<math.h>
    main()
    {
    #include<stdio.h>
    #include<math.h>
    main()
    {
    int x, y;
    double A, B, C;
    printf("Enter (double ) A, B, C:\n");
    scanf("%f %f %f", A, B, C);
    printf("Enter (double) x:\n");
    scanf("%f", x);
    {
    if (x <=0)
            y= (A*x*x + B);
            printf("Case 1\nx value is = %d and y value is = %d", x, y);
    else{
            if (x <<1)
                    y= (B + C*x);
                    printf("Case 2\nx value is = %d and y value is = %d", x, y);
            else{
            (x >=1)
                    y= (B + C);
            printf("Case 3\nx value is = %d and y value is = %d", x, y);
    }
    }
    }
    exit(0) ;
    }
    these are the errors i get:
    Code:
    q4.c:15: parse error before "else"
    q4.c:19: parse error before "else"
    q4.c: At top level:
    q4.c:25: parse error before '}' token
    q4.c:26: parse error before numeric constant
    q4.c:26: warning: data definition has no type or storage class
    this was my assignment if it helps:
    Code:
    /*
    
                  MAE9: Homeworki #4, Spring 2003
    
              Due on Thursday, May 1, before 11:00 pm
    
    
    A mathematical relationship between x and y is described by
    the following expressions:
    
    y= A*x^2 + B  if  x<=0   (Case 1)
    
    y=B+C*x       if  0<x<1  (Case 2)
    
    y=B+C         if  x>=1   (Case 3)
    
    where A, B, and C are constants. Write a C program that reads
    the double values of the constants A,B,C, and the argument x
    (by using scanf), and computes the corresponding value of y.
    Print x and y by using %f format with 3 decimal places.
    
    Use the pow(a,b) function to calculate x^2, and if/else
    statements to choose the proper expression for y, corresponding
    to selected x.
    
    After reading A,B,C, your program should use a for loop to
    evaluate y for scanned x in each of the above three cases.
    
    Your output should look like:
    
    
    Enter (double ) A, B, C:
    1. 2. 3.
    
    Enter (double) x:
    0.5
    Case 2
    x value is = 0.500  and  y value is = 3.500
    
    Enter (double) x:
    -1.
    Case 1
    x value is = -1.000  and  y value is = 3.000
    
    Enter (double) x:
    2.
    Case 3
    x value is = 2.000  and  y value is = 5.000
    
    
    .......................................................
    
    Note: If you started working on hw4 before Friday, and
    made the output as below, it's acceptible. If you didn't,
    try to get above output.
    
    Enter (double ) A, B, C:
    1. 2. 3.
    
    Case 1
    Enter (double) x:
    -1.
    x value is = -1.000  and  y value is = 3.000
    
    Case 2
    Enter (double) x:
    0.5
    x value is = 0.500  and  y value is = 3.500
    
    Case 3
    Enter (double) x:
    2.
    x value is = 2.000  and  y value is = 5.000
    
    */
    thanks for any help

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You've also not included the braces after your if statement which is what is giving the 'parse error before else' errors. Correct usage is:
    Code:
    if (expression)
    {
      statement;
    }
    else
    {
      statement;
    }
    the only time you can get away with not using {} is when the statement is only one line long eg.
    Code:
    if (i > 5)
      i = 0;
    else
    {
      i++;
      printf("%d\n", i);
    }
    If in doubt use braces...
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    you were right i added in the brackets and its actually compiling now, but it core dumps every time i put in numbers. heres what i have now:
    Code:
    #include<stdio.h>
    #include<math.h>
    main()
    {
    int x, y;
    double A, B, C;
    printf("Enter (double ) A, B, C:\n");
    scanf("%f %f %f", A, B, C);
    printf("Enter (double) x:\n");
    scanf("%f", x);
    {
    if (x <=0)
            {
            y= (A*x*x + B);
            printf("Case 1\nx value is = %d and y value is = %d", x, y);
            }
    else{
            if (x <<1)
            {
                    y= (B + C*x);
                    printf("Case 2\nx value is = %d and y value is = %d", x, y);
            }
            else{
                    y= (B + C);
            printf("Case 3\nx value is = %d and y value is = %d", x, y);
    }
    }
    }
    exit(0) ;
    }
    thanks for helping me out

  4. #4
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Code:
    #include<stdio.h>
    
    int main()
    {
    	int x, y;
    	double A, B, C;
    	printf("Enter (double ) A, B, C:\n");
    	//%lf instead %f because A, B, C are of type double
    	scanf("%lf %lf %lf", &A, &B, &C);
    	printf("Enter (double) x:\n");
    	//%d instead %f because x is of type int
    	scanf("%d", &x);
    	
    	if (x <=0)
    	{
    		y= (A*x*x + B);
    		printf("Case 1\nx value is = %d and y value is = %d", x, y);
    	}
    	else
    	{
    	    if (x <<1)
    	    {
    	        y= (B + C*x);
    	        printf("Case 2\nx value is = %d and y value is = %d", x, y);
    	    }
    	    else
    	    {
    	    	y= (B + C);
    	    	printf("Case 3\nx value is = %d and y value is = %d", x, y);
    		}
    	}
    	
    	return 0;
    }
    The scanf function takes pointers to variables as arguments. Thus, you need to add the "address of" operator & like I did above. The program should work now, but I have not tested it.
    Last edited by Sargnagel; 05-01-2003 at 02:01 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    Thanks, the & worked for the A B and C when i put in numbers for them this is what I got:
    Code:
    iacs5.ucsd.edu 34: a.out
    Enter (double) A, B, C:
    1. 2. 3.
    Enter (double) x:
    Case 1
    x value is = 0 and y value is = 0iacs5.ucsd.edu 35: vi q4.c
    something is wrong with my x, its not asking for a value, its just running the rest of the program.
    thanks for the feedback

  6. #6
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    I just looked tru this quikly and took a look at you´re homework, are you sure you use x correctly? to me it semese like you whant an int to give you a double value.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  7. #7
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    @x:
    You must empty stdin before the next scanf call, because a '\n' remains in the input buffer stdin.
    Code:
    void tidy(FILE *in)
    {
    	int c;
    	
    	while((c = fgetc(in)) != EOF && c != '\n');
    }
    And remember:
    fflush(stdin) is undefined by ANSI C99 standard!

    I hope this helps.
    Last edited by Sargnagel; 05-01-2003 at 03:36 PM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    i dont understand

    im sorry i dont understand what youre saying i have to do, this is the first class ive ever taken in programming and about as new to it as you can get ummm, i tried writing a second program using the same begining but with a do while loop instead of the if else's but it never prints the part asking for the value of x and then the equations are all off below, if you think i was closer in the first code than this one ill go back to it, im just trying to get something to go through. so here it is:
    Code:
    #include<stdio.h>
    #include<math.h>
    main()
    {
    {
    int y;
    double A, B, C, x;
    printf("Enter (double) A, B, C:\n");
    scanf("%d %d %d", &A, &B, &C);
    
    printf("Enter (double) x:\n");
    scanf("%d", &x);
    
    do
    {
     y= (A*x*x + B);
     printf("Case 1\nx value is = %d and y value is = %d\n", x, y);
    break;
    }
    while (x <= 0);
    do
    {
     y= (B + C*x);
     printf("Case 2\nx value is = %d and y value is = %d\n", x, y);
    break;
    }
    while (0 < x & x > 1);
    do
    {
     y= (B + C);
     printf("Case 3\nx value is = %d and y value is = %d\n", x, y);
    break;
    }
    while (1 <= x);
    }
    exit(0);
    }
    and heres what i get
    Code:
    Enter (double) A, B, C:
    1. 2. 3. 
    Enter (double) x:
    Case 1
    x value is = 8 and y value is = 0
    Case 2
    x value is = 130 and y value is = 0
    Case 3
    x value is = 130 and y value is = 0

  9. #9
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Oh, sorry, I didn't know you where a real newbie. Ok, try to compile and run this source code:
    Code:
    #include<stdio.h>
    
    void tidy(FILE *);
    
    void tidy(FILE *in)
    {
    	int c;
    	
    	while((c = fgetc(in)) != EOF && c != '\n');
    }
    
    int main()
    {
    	int x, y;
    	double A, B, C;
    	printf("Enter (double) A, B, C:\n");
    	//%lf instead %f because A, B, C are of type double
    	scanf("%lf %lf %lf", &A, &B, &C);
    	printf("Enter (int) x:\n");
    	tidy(stdin);
    	//%d instead %f because x is of type int
    	scanf("%d", &x);
    	
    	if (x <=0)
    	{
    		y= A*x*x + B;
    		printf("Case 1\nx value is = %d and y value is = %d", x, y);
    	}
    	else
    	{
    	    if (x <<1)
    	    {
    	        y= B + C*x;
    	        printf("Case 2\nx value is = %d and y value is = %d", x, y);
    	    }
    	    else
    	    {
    	    	y= B + C;
    	    	printf("Case 3\nx value is = %d and y value is = %d", x, y);
    		}
    	}
    	
    	return 0;
    }
    I will go to bed now. I hope somebody else will help you during the next hours if you should need further assistance.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Talking Thanks

    I compiled it and it works, the x's are off but im sure thats my equations, thanks so much for your help, I really appreciate it.

  11. #11
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    You should read again Salem's first post! He already pointed out what's wrong.
    Just take a book on C programming and read about if ... else and >, >=, ==, <=, < and left/right shift operators ( <<, >> ).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM