2015-04-17 86 views
0

美好的一天!多個輸入C

我試圖創建一個函數,獲取多個輸入的電阻值。在我的主要功能中,程序要求用戶詢問需要多少個電阻。用戶需要的電阻數量將成爲程序要求用戶輸入電阻值的次數。問題是我應該使用什麼循環來創建錯誤語句,並讓用戶再次輸入值。輸入只接受整數。這裏是代碼:

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

輸出應該是這樣的:

多少電阻你要在電路的地方?

請輸入電阻的值:

請輸入電阻器#1:

ABCD

無效電阻!

重新輸入電阻#1:

請輸入電阻#2:

...

不發生在我的代碼上。請幫忙謝謝!

+0

你可以使用['strtol'(http://en.cppreference.com/w/c/string/byte/strtol)或['sscanf'(HTTP://en.cppreference。 COM/W/C/IO /的fscanf)。兩者都可用於轉換和驗證字符串。 –

+0

我已經完成驗證部分。我的問題是如何循環說重新輸入阻力的聲明。 – JayCastillo

+0

我只是說有更簡單的方法來處理驗證和轉換。至於如何在驗證失敗時循環,沒有「標準」的方式來做這件事,你可以使用任何你想要的循環,常見的是例如'while(scanf(resist,「%d」,&numres)!= 1){/ *不正確的輸入,再試一次* /}' –

回答

0

您代碼中的兩處更改。

第一個,

在循環,就不得不提到的rescount*val_res

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

然後,而不是do..while使用while循環來檢查錯誤。

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

      }  
+0

它不考慮第一個輸入的錯誤。我應該把第一個fgets放在while循環中嗎? – JayCastillo

0

刪除do-while循環。只是不要爲非法輸入計算該輸入(不要增加z)。這裏是代替你的for循環的代碼。

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