[C language] Multiple input

C Series Article Directory


Table of contents

C Series Article Directory

1. What is multiple input?

2. How to use multiple sets of input

2.1, Exam questions with examples 

2.2, wrong solution

2.3, we realize the idea of ​​multiple sets of input

2.4, the first correct solution

2.5, the second correct solution

2.6, the second input method for multi-group input 

2.7, how to stop multiple input 

Summarize


foreword

We learn the C language, the adventure of growing from a small boy to a master, we will initially explore the knowledge level of multi-group input in this chapter


1. What is multiple input?

In C, "multiple sets of input" generally refers to the situation where multiple input values ​​are received from the user. These input values ​​can be of different data types such as integers, floating point numbers, characters, etc.

2. How to use multiple sets of input

2.1, Exam questions with examples 

We use this question to explain multiple sets of input:

2.2, wrong solution

This solution can only pass through Niuke.com by luck, but it does not really meet the requirements of multiple sets of input for this question

//如果这样写,通过了很侥幸,因为题目要求多组输入
//只能跑一组数据,这个代码只接收一组数据,判断完就就结束了
int main()
{
	//输入
	int iq = 0;
	scanf("%d", &iq);
	//判断
	if (iq >= 140)
	{
		printf("Genius\n");
	}
	return 0;
}

 

2.3, we realize the idea of ​​multiple sets of input

How to implement multiple sets of loop ideas
Multiple sets of input means to test multiple sets of data, instead of only testing one set of data, it ends after judging In
order to achieve multiple sets of input, if we want this code to have multiple sets of input, we must let the code loop
Do we want this scanf to receive a data, make a judgment, and then receive the data, judge, and so on.
We write a while(), and put scanf("%d", &iq) in the place of the condition judged by while().
We all know that the while loop first executes the judgment statement
while loop judges scanf and gets a return value. If the value is true, the while loop will come in.
Just as you read iq, while will judge iq until we fail to read the data here. If it is false, we will jump out of the loop. If
we want to judge by the return value of scanf, we will Know what the return value of scanf is.

We open a website called https://cplusplus.com/doc/tutorial/
, go back to the old version, and then enter search scanf, then we can see that the return type of scnaf is int and
what is the integer returned, let’s go down Turn over, you can see a Return Value, which means to receive the return value.
It is a meaning related to the return value of scanf described. What does this English description mean?
If scanf reads a few numbers, it returns to read To the number of numbers,
 if it returns 1, the reading is normal

2.4, the first correct solution

The scanf function is an input function.
The return value of the function is:
if the int is read successfully, it returns the number of data actually read.
If the function fails to read, it returns EOF.
The very simple way of writing is scanf == 1, scanf read Get a number, if the return value = 1, it proves that the reading is normal, then go in and judge

We'll use the code, comments, and graphs to understand:

int main()
{
	//输入
	int iq = 0;
	//scanf 函数是输入函数
	//函数的返回值是:int
	// 如果读取成功,返回的就是实际读取到的数据的个数
	// 如果函数读取失败,就返回EOF
	//非常简单的写法就是scanf == 1,scanf读取到一个数字,如果返回值=1,就证明读取正常,就进去判断
	//
	while (scanf("%d", &iq) == 1)
	{
		//判断
		if (iq >= 140)
		{
			printf("Genius\n");
		}
	}
	return 0;
}

2.5, the second correct solution

The scanf function is an input function.
The return value of the function is:
if the int is read successfully, it returns the number of data actually read.
If the function fails to read, it returns EOF, and the value of EOF is -1. The
very simple way of writing is scanf == 1, scanf reads a number, if the return value = 1, it proves that the reading is normal, and we go in to judge and we judge
if scanf reads an integer and finds that it has not been read, and finds that it fails, then returns EOF

If the return is not EOF, the read is successful. It is true, not equal to EOF so it is true     

We'll use the code, comments, and graphs to understand:

int main()
{
	//输入
	int iq = 0;
	//scanf 函数是输入函数
	//函数的返回值是:int
	// 如果读取成功,返回的就是实际读取到的数据的个数
	// 如果函数读取失败,就返回EOF,EOF的值-1
	//非常简单的写法就是scanf == 1,scanf读取到一个数字,如果返回值=1,就证明读取正常,就进去判断
	//我们就去判断如果scanf读取一个整数的时候,发现没有读到,发现失败了,就返回EOF
	while (scanf("%d", &iq) != EOF)//如果返回的不是EOF,就是读取成功了。就为真,不等于EOF所以为真                
	{
		//判断
		if (iq >= 140)
		{
			printf("Genius\n");
		}
	}
	return 0;
}

2.6, the second input method for multi-group input 

We can also enter it like this. Similarly, when the while is executed, one is read, and after the judgment, the next one is read.

We'll use the code, comments, and graphs to understand: 

2.7, how to stop multiple input 

How do we stop this code?
The first method is ctrl+z to stop
because ctrl+Z can make scanf return to EOF
, but on vs2022, you will find that you need to press three ctrl+z to end this program.
This is vs. A bug, other compilers end by pressing a ctrl+z


Summarize

The above is what I want to talk about today. This article only comprehensively introduces the multiple input problems of C language, which can make your adventure in C language more interesting and fulfilling.

Guess you like

Origin blog.csdn.net/weixin_73466540/article/details/131661158