0

Ok so i am working on some school work in Visual Studio 2015 for the first time and my program will not go into an if statements and just exits with code 0, i tried removing the if statement and it works but I need to have it.

    #include <stdio.h>

void main() {
    char c1, c2;
    int controlPoint;
    int counter, counterTwo;
    float intSingle;
    int isTrue = 0;
    float* x;
    float* y;
    float* fx;
    float sum = 0;
    float temp;
    int k;



    printf(" \n Please select an option");
    printf(" \n Press i to do an interlopation");
    printf(" \n Press q to quit");

    scanf("%c", &c1);


    if (c1 == 'q' || c1 == 'Q') {
        printf("Now Quiting");
        exit(0);
    }
    else if (c1 == 'i' || c1 == 'I') {
        printf(" \n Do a Lagrange Interlopation");
        printf(" \n How many control points are there?");

        scanf("%d", &controlPoint);
        x = malloc(controlPoint * sizeof(float));
        y = malloc(controlPoint * sizeof(float));

        for (counter = 0; counter < controlPoint; counter++) {
            printf("Please Enter the x coordinate for control point #%d: ", counter);
            scanf("%f", &x[counter]);
            printf("Please Enter the y coordinate for control point #%d: ", counter);
            scanf("%f", &y[counter]);
        }
    }
    else {
        printf("Invalid Option Quitting");

    }









    printf("\nPlease select an option");
    printf("\n Press s to do single interlopation");
    printf("\n Press r to interlope in increments over the entire range");
    printf("\n Press q to quit");

    scanf("%c", &c2);

    if (c2 == 's' || c2 == 'S') {
        printf(" \n Interlope Single");
        printf(" \n Please enter the value of x you wish to interlope for: ");
        scanf("%f", &intSingle);
        fx = malloc(controlPoint * sizeof(float));

        for (counter = 0; counter < controlPoint; counter++)
        {
            temp = 1;
            int k = counter;

            for (counterTwo = 0; counterTwo < controlPoint; counterTwo++)
            {
                if (k == counterTwo)
                {

                }
                else
                {
                    temp = temp * ((intSingle - x[counterTwo]) / (x[k] - x[counterTwo]));
                }
            }
            fx[counter] = y[counter] * temp;

        }

        for (counter = 0; counter < controlPoint; counter++)
        {
            sum = sum + fx[counter];
        }
        printf("\n Interpolated pair is (%f, %f) ", intSingle, sum);
    }

    else if (c2 == 'q' || c2 == 'Q'){
        printf("Now Quiting");
        exit(0);

    }
    int holder;

    scanf("%d", &holder);



}

the problem is when user inputs second option, it just closes if i press s with code 0 i also removed the if statement and the code works fine but i need them

Sandy
  • 21
  • 3
  • 1
    Have you considered stepping through your code in the debugger? Not sure which if statement you're referring to or what your input is. Perhaps you could clarify those things. – Retired Ninja Mar 19 '17 at 23:16
  • when the code reaches here if (c2 == 's' || c2 == 'S') { – Sandy Mar 19 '17 at 23:22
  • http://stackoverflow.com/questions/6582322/what-does-space-in-scanf-mean – Retired Ninja Mar 19 '17 at 23:36
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter value) to assure the operation was successful. – user3629249 Mar 20 '17 at 11:26
  • when calling any of the heap allocation functions: (malloc, calloc, realloc), always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Mar 20 '17 at 11:29

1 Answers1

3

First, your code doesnt look very good, you are not freeing dynamic allocation, some missing \n, etc..

For your question, scanf %c read only one char, when you type a letter and press enter you actaully type i\n.

The first scanf read the letter i, but \n stays in the buffer, until the next scanf reads it instead of s.

You can use something like this to clear the stdin buffer:

while ((temp = getchar()) != '\n' && temp != EOF);

Or, if you are sure your input is only 1 char, you can just add space before %c to ignore \n: scanf("%c",... to scanf(" %c",...

t.elazari
  • 300
  • 2
  • 11