2016-09-05 26 views
-3

Cでgetchar()を使用すると、入力された文字のascii値が取得されます。 getchar()を使用して入力された値をループし、入力された数値( '。'を含む)を浮動小数点に変換する必要がある場合、値の変換方法とユーザーが入力したい値の数に基づいて複数の行を読み込む必要がありますか?これまでは、カウンタを使ってwhileループを使用して、浮動小数点に変換する値の数をユーザー入力チェックで確認していました。私の問題は、内部ループを使用してgetchar()の値を一度に1つずつ浮動小数点数に変換し、浮動小数点数にフォーマットされてしまうことです。何か案は?Cでgetchar()を使用する

char input; 
float value; 
int count = 0; 
int i = 0; 
scanf("%i", count) 
while(i < count) 
{ 
    input = getchar(); 
    while(input != '\n') 
    { 
     input = input - '0'; 
     printf("%d",value); 
    } 
    i++; 
} 

私がこだわっているいずれかの無限ループまたは後に読ま0.0の値を取得する。任意のアイデア?

+1

最初に整数に対してscanf(間違っていますが修正可能です)を使用した場合、なぜ浮動小数点数を使用しないのでしょうか? –

+0

getchar()をもっと学びたいので、scanfを使わないでください。全体ループのカウンタを読み込むためにだけ使用してください。 –

+0

@ MD_90:scanf()をスキップしたい場合は、fgets()を使用してください。しかし、正直言って、あなたのコードがそれを(おそらく)間違えるようにするよりも、ライブラリを解析するほうが良い。 – siride

答えて

0

コメントに記載されている実際の問題と同様に、私はコードを編集し、あなたが記述したものに従って書きました。

#include<stdio.h> 

void main() 
{ 
    int count = 0; 
    int i = 0; 
    int count_digits=0; 
    char output[50];  //For storing the input 
    scanf("%d",&count); 
    while(i < count) 
    { 
     printf("\n\nInput:%d  ",i+1);  //For printing the input number. 
     scanf("%s",&output);   //taking input as a String 

     int char_no=0;     //character no pointer 
     count_digits=0;     //for counting number of characters (in our case : Digits) before decimal points which is then to be printed after decimal points 

     while(output[char_no]!='\0' && output[char_no]!='.') //printing all digits before decimal point or print whole number if it doesn't contain decimal point 
     { 
      printf("%c",output[char_no]);  //printing character(Digit) 
      char_no++;     //incrementing character number 
      count_digits++;    //Counting total number of digits before decimal point 
     } 



     if(output[char_no]!='\0')  //If it is not the end of character array means number contains decimal point 
     { 
      printf("%c",output[char_no]); //printing decimal point as a character 
      char_no++;     //Incrementing character pointer 
     } 
     else 
      printf(".");    //If number doesn't contain decimal point it will print it. 

     while(count_digits>0 && output[char_no]!='\0') //loop will run till no of digits after decimal point gets printed or reaching at the end of input 
     { 
      printf("%c",output[char_no]); //printing the character as it is 
      char_no++;    //incrementing character number 
      count_digits--;  //decrementing total no of digits to be printed after decimal point as one character gets printed. 
     } 

     while(count_digits>0) //if input ends but still the no of digits after decimal points are less than the no. of digits before decimal points, It will print zeros to make no.of digits after and before decimal points equal 
     { 
      printf("0");  //printing 0 
      count_digits--;  //decrementing total no of digits to be printed after decimal point as one character gets printed. 
     } 
     i++; 
    } 
} 

上記のサンプル出力は次のとおりです。 Output が、これはあなたの問題を解決することを願っています。

+0

私はgetchar()がintを返すと思った?入力に10進数を入力するとどうなりますか? –

+0

'getchar()'で直接ascii値を取得したい場合は、 'ascii_char = getchar();'のように直接割り当てることができます。 printf( "%d"、c);両方で同じ出力が得られます。 @ MD_90 –

+0

ユーザが浮動小数点に変換する行の数を5行入力するとします。最初の数字は10.2です。私はgetchar()から1つを読み込み、次にfloat形式で1に変換し、getcharは0を返し、次に同じ小数点は2を返し、同じ手順に従います。残りの4行は同じ考え方に従います。何が間違っているのですか?これらの数字は小数点以下2桁でフォーマットする必要があります –

関連する問題