【解決方法】入力を取得する関数を書きましたが、間違っていて、この問題を解決できません


#include<stdio.h>
#include<ctype.h>
void enter_num(char base_num[], int num);
int main()
{
	char base_num[7] = {};
	enter_num(base_num, 7);
	printf("%c", base_num[1]);
	return 0;
}
void enter_num(char base_num[], int num)
{
	int i;
	for ( i = 0; i < num; i++)//for user input,stop when input is blank
	{
		base_num[i] = getchar();
		if (isspace(base_num[i]))
		{
			base_num[i] = '\0';
		}
		if (isalnum(base_num[i])||base_num[i]=='\0')//Verify if the digital input is correct, if not, re-enter
			;
		else
		{
			printf("The number is wrong!Please enter again.\n");
			i = 0;
		}
		if (base_num[i] = '\0')
		{
			break;
		}
	}
	int n = 0;
	while (n < i)//Convert all input letters to lowercase
	{
		if (isalpha(base_num[n]))
		{
			base_num[n] = tolower(base_num[n]);
		}
		n++;
	}
}

私が試したこと:

I tried to output the second array element, but I output blank space. And I need to output two line breaks to end the program.

解決策 1

引用:

そして、プログラムを終了するには、2 つの改行を出力する必要があります。

それは簡単です:

C
printf("\n\n");

C では、「=」は割り当てです。

C
if (base_num[i] = '\0')

必要なのは比較です:

C
if (base_num[i] == '\0')

コメント

タイトルとURLをコピーしました