mrarray
mrarray

Reputation: 1

Using function if, if condition is true, not working?

I want so that when the user enters e it will run my function called enter and scan in values but all I get is VECRQ?:, why is this? Did I call upon the function wrong?

I tried having the while loop to 1 also instead of menuoption != 'q' didn't work. I was thinking that with the menuoption is not equal to 'q' it will keep the loop running until the user actually enters 'q' to shut the program off.

#include <stdio.h>

int enter(int measurments[], int nrOfmeasurments)
{
    while(nrOfmeasurments<10) 
    {
        printf("Enter measurment #%d (or q to quit): ",nrOfmeasurments+1);
        int oneMeasurment;
        int readInteger = scanf("%d",&oneMeasurment);
        if(readInteger)
        {
            measurments[nrOfmeasurments] = oneMeasurment;
            nrOfmeasurments ++;
            //return nrOfmeasurments;
        }
        else
        {
            char tmp;
            scanf(" %c",&tmp);
            break;
        }

    }
    if(nrOfmeasurments==10)
    {
        printf("Array is full\n");
    }

    return nrOfmeasurments;
}

int main(void)
{
    int measurments[10];
    int nrOfmeasurments;
    char menuoption;
    printf("Measurment tool 2.0\n");

    while (menuoption != 'q')
    {
        printf("VECRQ?:\n");
        scanf(" %c",&menuoption);

        if (menuoption == 'e')
        {
            //int MeasurmentData[10];
            //int nrOfmeasurments;
            //enter(measurments, nrOfmeasurments);
            nrOfmeasurments = enter(measurments, nrOfmeasurments);
        }
        else if(menuoption == 'v')
        {

        }
        else if(menuoption == 'c')
        {

        }
        else if(menuoption == 'q')
        {
            printf("Exiting Measurment tool 2.0\n");
            break;
        }

    }
}

Upvotes: 0

Views: 62

Answers (1)

P. Dmitry
P. Dmitry

Reputation: 1121

Don't forget to init your variables with default values. Your problem is that nrOfmeasurments is not initialized and have some trash value. Also, set a default value to menuoption for some non q char to be sure, that your loop will be executed at least one time

Upvotes: 1

Related Questions