Thread: Computation Loop and duplicate entry error

  1. #1
    Registered User
    Join Date
    Apr 2010
    La Jolla, CA

    Computation Loop and duplicate entry error

    I'm stuck! Here is problem I am having: The loop I have made calculates values based off an integer value entered by the user from the keyboard where if they enter the same value from a given interval a warning message is displayed followed by a return to the beginning of the loop.

    Here is my code so far:

    #include <stdio.h>
    #include <math.h>
    int main(void)
            int x;
            double A, B, C, f1, f2, f3;
            printf("Enter a value for A, B, and C:\n");
            scanf("%lf %lf %lf", &A, &B, &C);
            printf("A = %g \tB = %g \tC = %g\n", A, B, C);
            printf("Enter a value for x:\n");
            scanf("%d", &x);
            printf("x = %d\n",x);
                    printf("Case 1\n");
                    f1 = (A * pow(x, 3)) - (B * pow(x, 2)) + (x / 1.25);
                    printf("Value of x = %3d and y = %6.2f\n", x, f1);
            else if(0<x<1){
                    printf("Case 2\n");
                    f2 = A * x - B * pow(x, 4) + pow(C, 2);
                    printf("Value of x = %3d and y = %6.2f\n", x, f2);
            else if(x>=1){
                    printf("Case 3\n");
                    f3 = A * log(x) - B / x - 2 * A;
                    printf("Value of x = %3d and y = %6.2f\n", x, f3);
    Very simple and basic code, as this is my first C/C++ class. The loop works great but I don't know where to go from here to create the additional inputs with the warning message and return to the beginning of the loop.

    i hope what I am saying is making sense, I am going on about 4 hours sleep and being up for almost 20 hours at this point (gotta love being a student!).

    Any help would be greatly appreciated.


  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    I think you'd better get some rest.

    First of all, there's no loop in the code you posted.

    else if(0<x<1){
    If you want to check it x is between 0 and 1 you do
    if (0<x && x<1)
    but since you declared x as an int, x can be 0, it can be 1, but it can't be between 0 and 1.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Tags for this Thread