gpt4 book ai didi

c - 输入验证分配并显示是否为二维字符数组

转载 作者:行者123 更新时间:2023-11-30 19:22:46 24 4
gpt4 key购买 nike

我正在尝试在 C 中填充二维数组。一切正常,但分配给数组的值未打印。

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

main()
{
char parkingspace[25][4];
char CarReg[7], validreg[7];
int row, position;

printf( "Enter the car Registration number \n" );
fgets( CarReg, sizeof( CarReg ), stdin );
if( isdigit( CarReg[0] )&& isdigit( CarReg[1] ) && (CarReg[2]=='H' ) && ( CarReg[3]=='I' ) && ( CarReg[4]=='R' ) && ( CarReg[5]=='E' ))
{
puts( "Valid Registration \n" );
printf( "==================================================\n\n\n" );
}
else
{
puts( "Invalid registration .\n Please put a value of two digits followed by the word HIRE! in caps" );
}
printf( "You entered: %s\n", CarReg );
if( isdigit( CarReg[0] )&& isdigit( CarReg[1] ) && ( CarReg[2]=='H' ) && (CarReg[3]=='I' ) && ( CarReg[4]=='R' ) && ( CarReg[5]=='E' ))
{
strcpy(validreg, CarReg);
printf( "Accepted Car Reg is : %s\n\n\n\n", validreg );
printf( "==================================================\n\n\n");
}
for (row=1; row<26; row++)
{
for (position=1;position<5; position++)
{
parkingspace[row][position]=validreg;
printf("parkingspace \t row[%d] position[%d] =[ %c ]\n", row,position,parkingspace[row][position]);
}
}
}

最佳答案

您遇到的一个问题是数组的索引从零到数组大小减一。因此,row 的有效索引是 024,而不是 125你会的。

<小时/>

另一个问题是您尝试将字符数组分配给单个字符:

parkingspace[row][position]=validreg;

如果您只想在 parkingspace[row] 中使用 validreg 的前四个字符,那么请执行以下操作:像这样的内循环:

for (position=0; position < 4; position++)
{
parkingspace[row][position] = validreg[position];
printf("parkingspace \t row[%d] position[%d] =[ %c ]\n", row, position, parkingspace[row][position]);
}

关于c - 输入验证分配并显示是否为二维字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15246326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com