carrayscstdio

Multiple inputs in C


I'm trying to create a function that gets multiple inputs for resistances value. In my main function the program asks the user to ask how many resistors are needed. The number of resistors needed by the user will become the number of times the program will ask the user to input for resistance value. The problem is what loop should i use to make an error statement and make the user input the value again. the input only accepts integer. here is the code:

    #include <stdio.h>
    #include <ctype.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>

    #define MAXSIZE 500
    int resistors(int rescount, int *val_res);
    int validate ( char *a )
    {
     unsigned x;
     for ( x = 0; x < strlen ( a ); x++ )
     if ( !isdigit ( a[x] ) ) 
        return 1;
     return 0;
    }

    int main()
    {
    int numres;
    int resistance[MAXSIZE];
    char resist[MAXSIZE];
    do
    {
        printf("How many resistors do you want to place in the circuit?\n");

        if (fgets(resist, sizeof(resist), stdin) != NULL)
        {
           resist[strlen (resist) - 1] = '\0';
           if(validate (resist) == 0)
           {
             numres = atoi(resist);
           }
        }   

    } while(numres < 1 || numres > 100);
    resistors(numres, resistance);
    return 0;
    } 

    int resistors(int rescount, int *val_res)
    {
        char resistor_value[MAXSIZE];
        int z;

        printf("Please input the value of resistors:\n");
        for(z = 1; z < (*val_res); z++)
        {
           printf("\tPlease input Resistor #%d: \n", z);
           if (fgets(resistor_value, sizeof(resistor_value), stdin) != NULL)
            {   
            resistor_value[strlen (resistor_value) - 1] = '\0';
            if(validate (resistor_value) == 0)
                {
                    val_res[z-1] = atof(resistor_value);
                }
            }
         do{
            printf("\tInvalid Resistance\n");
            printf("\tRe-input Resistor #%d: \n", z);
            if (fgets(resistor_value, sizeof(resistor_value), stdin) !=   NULL)
            {   
               resistor_value[strlen (resistor_value) - 1] = '\0';
               if(validate (resistor_value) == 0)
                {
                    val_res[z-1] = atof(resistor_value);
                }
            }

            }while(val_res[z-1] < 0);
        }
    }

the output should be like this:

How many resistors do you want to place in the circuit?

3

Please input the value of resistors:

Please input resistor #1:

abcd

Invalid resistance!

Re-input resistance #1:

5

Please input resistor #2:

6

...

etc

that does not happen on my code. please help thanks!


Solution

  • Two changes in you code.

    First One,

    In loop , you have to mention the rescount not *val_res.

     for(z = 1; z <= rescount; z++){
      . . . 
     }
    

    Then instead of do..while use while loop for checking the error.

      while(val_res[z-1] <= 0){
                        printf("\tInvalid Resistance\n");
                        printf("\tRe-input Resistor #%d: \n", z);
                        if (fgets(resistor_value, sizeof(resistor_value), stdin) !=   NULL)
                        {   
                                resistor_value[strlen (resistor_value) - 1] = '\0';
                                if(validate (resistor_value) == 0)
                                {
                                        val_res[z-1] = atof(resistor_value);
                                }
                        }
    
                }