0

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!

joe
  • 2,305
  • 2
  • 14
  • 25
  • You could use [`strtol`](http://en.cppreference.com/w/c/string/byte/strtol) or [`sscanf`](http://en.cppreference.com/w/c/io/fscanf). Both can be used to both convert and validate the string. – Some programmer dude Apr 17 '15 at 04:55
  • I've already done the validation part. My problem is how I will loop the statement that says reinput the resistance. – JayCastillo Apr 17 '15 at 04:57
  • I'm just saying that there are simpler ways to handle both the validation and conversion. As for how to loop while the validation fails, there are no "standard" way of doing that, you could use any kind of loop you want, common is e.g. `while (scanf(resist, "%d", &numres) != 1) { /* not correct input, try again */ }` – Some programmer dude Apr 17 '15 at 05:01

2 Answers2

0

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);
                            }
                    }

            }       
Karthikeyan.R.S
  • 3,921
  • 1
  • 17
  • 31
0

Remove the do-while loop. Just don't count that input (don't increment z) for illegal inputs. Here is the code to replace your for loop.

    z = 1;
    while(z < (*val_res))
    {
       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);
                z++;
            }
            else
            {
                printf("\tInvalid Resistance\n");
            }
    }
Brad Budlong
  • 1,715
  • 10
  • 10