用户以以下格式插入数据:“[NAME],[SURNAME],[INDEX]”。错误代码:
0——一切都正确加载到结构中
1 -- 未正确加载到结构(用户未使用逗号或 p->name 出错)
2 -- 仅正确加载名称
3 -- 姓名正确加载(索引出错)
struct student_t
{
char name[20];
char surname[40];
int index;
};
例子:
输入:约翰·迪普
错误代码:2
输入:约翰,深,999
错误代码:0
输入:NULL //(什么都没有)
错误代码:1
... 所以我无法检测用户是否插入例如:“John,Deep”(err 2)或“John,Deep,”(err 3)或“John,”(err 2),..(结果是 err 1;或者如果一切正常,则 err 0)
我的尝试://edit1:使用这种方法的工作版本,进一步低于此方法。
char buffer[1024], *pch1, *pch2;
if (fgets(buffer,1024, stdin)!=NULL)
{
pch1=strchr(buffer, ',');
pch2=strrchr(buffer, ',');
if (pch1!=pch2 && pch1!=NULL) //detects if inserted are 2 different commas
{
char *name = strtok(buffer,","); // returns pointer to the beginning of the token
if (name) {//the place where "," occurs, becomes a "NULL" character
sscanf(name," %19s", p->name); // skip leading spaces
char *surname = strtok(NULL,",");
if (surname) {
sscanf(surname," %39s", p->surname); // skip leading spaces
char *index = strtok(NULL,",");
if (index) {
p->index = (int)strtol(index, NULL, 10);
} else {*err_code=3; return NULL;} //only NAME and SURNAME correctly, INDEX is loaded wrong
} else {*err_code=2; return NULL;} //only NAME loaded correctly
}
} else if (pch1==pch2 && pch1!=NULL)
{//but if there is 1 comma, input may be like: "John, Deep" so name'd be ok
char *name = strtok(buffer,",");
if (name) {
sscanf(name," %19s", p->name);
char *surname = strtok(NULL,",");
if (surname) {
sscanf(surname," %39s", p->surname);
char *index = strtok(NULL,",");
if (index) {
p->index = (int)strtol(index, NULL, 10);
}else if (p->index==0||p->index==0||p->index==' ') {*err_code=2; return NULL;}
}
}
} else {*err_code=1; return NULL;} //if there were 0 commas, err_code=1
}
if (p==NULL || p->name==NULL)
{
*err_code=1;
return NULL;
}
if (p->surname && p->name==NULL)
{
*err_code=2;
return NULL;
}
//because the above "if" construction didn't work, I added another one here:
if (p->index==NULL || p->index==0) //so if input was "John, Deep" then p->index should be NULL?
{
*err_code=3;
return NULL;
}
//edit1:好的,这段代码对我有用,一切都按设想进行。然而它非常混乱,所以我会尝试在其他版本中采用和使用答案......
char buffer[1024], *pch1, *pch2;
if (fgets(buffer,1024, stdin)!=NULL)
{
pch1=strchr(buffer, ',');
pch2=strrchr(buffer, ',');
if (pch1!=pch2 && pch1!=NULL)
{
char *name = strtok(buffer,","); // returns pointer to the beginning of the token
if (name) { //the place where "," is occured becomes a "NULL" character
sscanf(name," %19s", p->name); // skip leading spaces
char *surname = strtok(NULL,",");
if (surname) {
sscanf(surname," %39[^\t\n]", p->surname); // skip leading spaces
char *index = strtok(NULL,",");
if (index) {
p->index = (int)strtol(index, NULL, 10);
if (p->index==0) {*err_code=3; return NULL;}
} //else {*err_code=3; return NULL;} //only NAME and SURNAME correctly, INDEX is loaded wrong
} else {*err_code=2; return NULL;} //only NAME loaded correctly
}
} else if (pch1==pch2 && pch1!=NULL)
{
char *name = strtok(buffer,","); // returns pointer to the beginning of the token
if (name) { //the place where "," is occured becomes a "NULL" character
sscanf(name," %19s", p->name); // skip leading spaces
char *surname = strtok(NULL,",");
if (surname) {
sscanf(surname," %39[^\t\n]", p->surname); // skip leading spaces
char *index = strtok(NULL,",");
if (index) {
p->index = (int)strtol(index, NULL, 10);
} else if (p->index==0||p->index==' ') {*err_code=2; return NULL;}
} else {*err_code=1; return NULL;}
} else {*err_code=2; return NULL;}
} else {*err_code=1; return NULL;}
}
if (p==NULL || p->name==NULL)
{
*err_code=1;
return NULL;
}
我有一种感觉,它可以用一种完全不同的方式来完成……我会把所有的提示和答案放在心上,并尽我最大的努力去理解和学习它们。
//edit1:如果我丑陋的代码激怒了某人,我真的很乐意做一些园丁工作并剪掉一些恶魔灌木,以清理一下。我认为有些情况根本不需要它起作用......
附言。 (这是我之前问题的延续,其中输入的逗号被分配给结构:如何扫描逗号,但逗号未分配给结构? C https://stackoverflow.com/questions/51343178/how-to-scanf-commas-but-with-commas-not-assigned-to-a-structure-c但在这个主题中,我询问以一种用户输入错误的信息的方式进行操作)