简体   繁体   English

C 程序只对第一个提示进行输入

[英]C program only takes input for the first prompt

My program only takes input for the first prompt and it excludes the succeeding ones, and instead, it just directly prints them.我的程序只接受第一个提示的输入,它排除了后面的提示,而是直接打印它们。

#include <stdio.h>

int main(){
    
    // Initialize
    char cCurrencyA, cCurrencyB;
    float fCurrencyA;
    float fRate;
    
    // Prompt user for currency names
    printf("Enter a currency name: ");
    scanf("%c%c", &cCurrencyA, &cCurrencyB);
    
    // Prompt user for the current rate
    printf("Enter the rate:");
    scanf("%f", &fRate);
    
    // Prompt user to enter value for currency A
    printf("Enter value for %c", cCurrencyA);
    scanf("%f", &fCurrencyA);
    
    // Convert currency A to B
    int nResult = fCurrencyA * fRate;
    
    // Print the result
    printf("%f%c is %d%c", fCurrencyA, cCurrencyA, nResult, cCurrencyB);
    return 0;
    
}

Output: Output:

Enter a currency name:USD PHP
Enter the rate:
Enter value for U:
0.000000U is 0S
  1. so you have major problems, you are scanning a whole string into only one char , that's wrong, you should do char cCurrencyA[20], cCurrencyB[20];所以你有很大的问题,你把整个字符串扫描成一个char ,这是错误的,你应该做char cCurrencyA[20], cCurrencyB[20]; not char cCurrencyA, cCurrencyB;不是char cCurrencyA, cCurrencyB; to scan a string not only one char .不仅要扫描一个字符串char

  2. when you are scanning using scanf() , use the quantifier %s not %c , as %c will get only one character but %s will scan a whole string.当您使用scanf()进行扫描时,请使用量词%s而不是%c ,因为%c只会得到一个字符,但%s会扫描整个字符串。

  3. also to round the result, you could use math header file, by using the function called lround() , so do this int nResult = round((double)fCurrencyA * fRate);也为了四舍五入结果,你可以使用math header 文件,通过使用 function 调用lround() ,所以这样做int nResult = round((double)fCurrencyA * fRate); not int nResult = fCurrencyA * fRate;不是int nResult = fCurrencyA * fRate; as the lValue is int while RValue is float so you have to cast it.因为 lValue 是int而 RValue 是float所以你必须转换它。

and here the code edited:在这里编辑了代码:

#include <stdio.h>
#include <math.h>
int main(){

    // Initialize
    char cCurrencyA[20], cCurrencyB[20];
    float fCurrencyA;
    float fRate;

    // Prompt user for currency names
    printf("Enter a currency name: ");
    scanf("%19s %19s", cCurrencyA, cCurrencyB);

    // Prompt user for the current rate
    printf("Enter the rate:");
    scanf("%f", &fRate);

    // Prompt user to enter value for currency A
    printf("Enter value for %s", cCurrencyA);
    scanf("%f", &fCurrencyA);

    // Convert currency A to B
    int nResult = lround((double)fCurrencyA * fRate);

    // Print the result
    printf("%f%s is %d%s", fCurrencyA, cCurrencyA, nResult, cCurrencyB);
    return 0;

}

and here is the output:这是 output:

Enter a currency name:USD PHP
Enter the rate:10.5
Enter value for USD10
10.000000USD is 105PHP
Process finished with exit code 0

First line of input was 8 characters: "USD PHP\n" .输入的第一行是 8 个字符: "USD PHP\n"

scanf("%c%c", &cCurrencyA, &cCurrencyB); only reads and saves the first 2 in cCurrencyA , cCurrencyB .仅读取并保存cCurrencyAcCurrencyB中的前 2 个。

The remaining 6 are not numeric text for the next scanf("%f",... so it does not assign anything and returns 0. Same for the next scanf("%f",... .剩下的 6 个不是下一个scanf("%f",...的数字文本,因此它不分配任何内容并返回 0。下一个scanf("%f",...


Best to read a line of user input with fgets() and then parse the string.最好用fgets()读取一行用户输入,然后解析字符串。


Yet if one must use the problematic scanf() , read currency names as a string.然而,如果必须使用有问题的scanf() ,请将货币名称读取为字符串。

// char cCurrencyA, cCurrencyB;
char cCurrencyA[100], cCurrencyB[100];

// scanf("%c%c", &cCurrencyA, &cCurrencyB);
scanf("%99s %99s", cCurrencyA, cCurrencyB);

// printf("%f%c is %d%c", fCurrencyA, cCurrencyA, nResult, cCurrencyB);
printf("%f%s is %d%s\n", fCurrencyA, cCurrencyA, nResult, cCurrencyB);

When using scanf with the %c conversion format specifier, scanf will only match a single character.当使用带有%c转换格式说明符的scanf时, scanf将只匹配单个字符。 Therefore, with the input USD PHP , the function call因此,输入USD PHP , function 调用

scanf("%c%c", &cCurrencyA, &cCurrencyB);

will match the U and the S , but D PHP will be left on the input stream.将匹配US ,但D PHP将留在输入 stream 上。 Afterwards, the function calls之后,function 调用

scanf("%f", &fRate);

and

scanf("%f", &fCurrencyA);

will both fail, because scanf is unable to convert D PHP to a floating-point number.都会失败,因为scanf无法将D PHP转换为浮点数。

If you want to use scanf to match a whole word, you could use the %s format specifier instead, but you should normally limit it to the size of the memory buffer.如果要使用scanf匹配整个单词,可以使用%s格式说明符,但通常应将其限制为 memory 缓冲区的大小。 For example, if the size of the memory buffer is 4 , then you could use %3s as the format specifier, which limits the number of matched characters to 3 , so that you still have room for the terminating null character.例如,如果 memory 缓冲区的大小是4 ,那么您可以使用%3s作为格式说明符,它将匹配字符的数量限制为3 ,以便您仍有空间用于终止 null 字符。

However, it is generally not recommended to use scanf for line-based user input, because, as you have now discovered yourself, scanf does not always behave in an intuitive manner, because it does not always read one line at once.但是,通常不建议将scanf用于基于行的用户输入,因为正如您现在发现的那样, scanf并不总是以直观的方式运行,因为它并不总是一次读取一行。

For this reason, you may want to consider using the fgets function instead, which always reads exactly one line of input at once, if possible.出于这个原因,您可能需要考虑使用fgets function 代替,如果可能的话,它总是一次只读取一行输入。

Therefore, I recommend that you first read an entire line of input as a string using fgets , and then use strtof , sscanf and/or maybe strtok to parse the string:因此,我建议您首先使用fgets将整行输入读取为字符串,然后使用strtofsscanf和/或strtok来解析字符串:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    char line[100];
    char strCurrencyA[4], strCurrencyB[4];
    float fCurrencyA, fRate, fResult;
    
    // Prompt user for currency names
    printf( "Enter a currency name: " );
    fgets( line, sizeof line, stdin );
    sscanf( line, "%3s %3s", strCurrencyA, strCurrencyB );
    
    // Prompt user for the current rate
    printf( "Enter the rate: " );
    fgets( line, sizeof line, stdin );
    fRate = strtof( line, NULL );
    
    // Prompt user to enter value for currency A
    printf("Enter value for %s: ", strCurrencyA);
    fgets( line, sizeof line, stdin );
    fCurrencyA = strtof( line, NULL );
    
    // Convert currency A to B
    fResult = fCurrencyA * fRate;
    
    // Print the result
    printf( "%0.2f %s is %0.2f %s", fCurrencyA, strCurrencyA, fResult, strCurrencyB);

    return 0;    
}

This program has the following behavior:该程序具有以下行为:

Enter a currency name: USD PHP
Enter the rate: 56.8
Enter value for USD: 25
25.00 USD is 1420.00 PHP

Note that this program does not perform any input validation.请注意,此程序不执行任何输入验证。 It will probably misbehave if the input is invalid in any way.如果输入以任何方式无效,它可能会出现异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM