如何在scanf字符串中包含空格?

时间:2022-07-24 02:07:08

This program works as long as the "major" variable is only one word. For example, CIS is a valid input but Computer Information Systems is not. How do I fix this?

只要“主要”变量只有一个单词,该程序就可以正常工作。例如,CIS是有效的输入,但计算机信息系统则不是。我该如何解决?

#include<stdio.h>

int main()
{
char major [51];
int classes;

printf("What is your major? ");
scanf("%50s", major);

printf("How many classes are you taking this semester? ");
scanf("%d", &classes);

printf("Your major is %s and you are taking %d classes this semester.\n", major, classes);
}

2 个解决方案

#1


3  

You can use %50[^\n] to match up to 50 characters until a newline is encountered, and store them in major[]. The newline will be left behind in the input stream, but the %d conversion specifier automatically skips over leading whitespace characters.

您可以使用%50 [^ \ n]匹配最多50个字符,直到遇到换行符,并将它们存储在major []中。换行符将留在输入流中,但%d转换说明符会自动跳过前导空白字符。

Note that if the user enters more than 50 characters, there will be extra characters in the input stream to discard before calling scanf() to get the number of classes.

请注意,如果用户输入的字符超过50个,则在调用scanf()以获取类的数量之前,输入流中将有额外的字符要丢弃。

#2


2  

Your problem is scanf stops at the first white space. What you need to do is keep reading until you read the end line character.

你的问题是scanf在第一个空格处停止。您需要做的是继续阅读,直到您阅读结束字符。

It's possible to do this with scanf but there are better options. fgets() comes to mind immediately. https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

使用scanf可以做到这一点,但有更好的选择。立即想到fgets()。 https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

A few searches on Stack overflow will yield a ton of topics about the scanf() vs fgets() vs others. Using fscanf() vs. fgets() and sscanf()

关于Stack溢出的一些搜索将产生关于scanf()vs fgets()vs其他的大量主题。使用fscanf()与fgets()和sscanf()

#1


3  

You can use %50[^\n] to match up to 50 characters until a newline is encountered, and store them in major[]. The newline will be left behind in the input stream, but the %d conversion specifier automatically skips over leading whitespace characters.

您可以使用%50 [^ \ n]匹配最多50个字符,直到遇到换行符,并将它们存储在major []中。换行符将留在输入流中,但%d转换说明符会自动跳过前导空白字符。

Note that if the user enters more than 50 characters, there will be extra characters in the input stream to discard before calling scanf() to get the number of classes.

请注意,如果用户输入的字符超过50个,则在调用scanf()以获取类的数量之前,输入流中将有额外的字符要丢弃。

#2


2  

Your problem is scanf stops at the first white space. What you need to do is keep reading until you read the end line character.

你的问题是scanf在第一个空格处停止。您需要做的是继续阅读,直到您阅读结束字符。

It's possible to do this with scanf but there are better options. fgets() comes to mind immediately. https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

使用scanf可以做到这一点,但有更好的选择。立即想到fgets()。 https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

A few searches on Stack overflow will yield a ton of topics about the scanf() vs fgets() vs others. Using fscanf() vs. fgets() and sscanf()

关于Stack溢出的一些搜索将产生关于scanf()vs fgets()vs其他的大量主题。使用fscanf()与fgets()和sscanf()