gpt4 book ai didi

c - 如何避免C中的sscanf()引起无限的while循环,而不是存储用户输入?

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

我一直工作在C项目,而现在,我是相当新的C,我现在尝试过许多东西,但似乎没有任何工作。每当我进入while循环并进入sscanf()函数时,循环就会变成无限循环,并且无法接收输入。意思是说我不能抓住,并将其存储在MT二维数组

我也尝试过使用strtok()作为另一个选项,但是代码变得太乱了。

typedef struct item {
char name[50];
double price;
} item;


int main() {
// 1 begins
char userInput[100];
printf("Hello, welcome to the shelf-O-matic!\n");

printf("Please press enter and then type the number of shelves followed by ',' and number of slots\n");
int shelf = 0;
int slot = 0;
scanf("%d , %d", &shelf, &slot);

struct item **thing = malloc(shelf * sizeof(item));
for (int i = 0; i < slot; i++) {
thing[i] = malloc(slot * sizeof(item));
}
while (strcmp(userInput , "quit")!= 0) {

//variables symbolizing the slots and shelves
printf("type quit to exit or set input to the array\n");
//scanf("%100s", userInput);

//Putting the size in memory location


double *cash = malloc(sizeof(cash));
int *x = malloc(sizeof(x));
int *y = malloc(sizeof(y));
//scanf("%100s", userInput);

// 1 ends
char *itemName = malloc(sizeof(itemName));
printf("Please add things to the shelves and slots by specifying <name> <price> <shelf> <slot> or type quit to finish adding\n");
sscanf(userInput,"%100s, %lf, %d, %d", itemName, &cash, &x, &y);

printf(itemName);
printf(cash);
printf(x);
printf(y);


}

return 0;
}


预期结果应该是输入到内存中的输入,以便随后可以将其分配给数组值并打印出来以进行验证。

最佳答案

while循环是无限的,部分原因是因为对输入的请求已被注释掉并且sscanf没有任何内容可解析。
另一个问题可能是使用%s解析itemName。如果在空间itemName,%s将会停止在第一空间。如果要使用逗号来分隔值,则%s会将逗号视为itemName的一部分而视为另一个字符。
扫描集可以帮助您。 scanset %[^,]告诉sscanf扫描不是逗号的字符并将其放在itemName中。因此,%49[^,],将最多扫描49个不是逗号的字符,然后再扫描逗号。
考虑对所有输入使用fgets并使用sscanf进行解析。其他一些有用的解析函数是strtolstrtodstrspnstrcspn。还有更多。

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

typedef struct item {
char name[50];
double price;
} item;

int main ( void) {
char userInput[100];
int result = 0;
int shelf = 0;
int slot = 0;

printf ( "Hello, welcome to the shelf-O-matic!\n");
do {
printf ( "Please enter the number of shelves\n");
if ( fgets ( userInput, sizeof userInput, stdin)) {
result = sscanf ( userInput, "%d", &shelf);
}
else {
fprintf ( stderr, "fgets EOF\n");
return 0;
}
} while ( 1 != result || 0 >= shelf);
printf ( "shelf = %d\n", shelf);
do {
printf ( "Please enter the number of slots\n");
if ( fgets ( userInput, sizeof userInput, stdin)) {
result = sscanf ( userInput, "%d", &slot);
}
else {
fprintf ( stderr, "fgets EOF\n");
return 0;
}
} while ( 1 != result || 0 >= slot);
printf ( "slot = %d\n", slot);
//allocate pointers then allocate structures to the pointers
struct item **thing = malloc ( sizeof *thing * shelf);
if ( NULL == thing) {
fprintf ( stderr, "malloc problem\n");
return 0;
}
for (int i = 0; i < shelf; i++) {//iterate through shelf number of pointers
thing[i] = malloc( sizeof **thing * slot);
if ( NULL == thing[i]) {
fprintf ( stderr, "malloc problem\n");
return 0;
}
}

while ( 1) {//keep looping
double cash = 0.0;
int x = 0;
int y = 0;
char itemName[50] = "";

result = 0;
printf ( "Please add things to the shelves and slots\n");
printf ( "specify: name, price, shelf, slot\n");
printf ( "when finished type quit or exit\n");
if ( fgets ( userInput, sizeof userInput, stdin)) {//get input
if ( ( strcmp ( userInput, "quit\n") == 0)
|| ( strcmp ( userInput, "exit\n") == 0)) {//exit loop
break;
}
result = sscanf ( userInput, "%49[^,], %lf ,%d ,%d"
, itemName, &cash, &x, &y);//sscanf to parse input

if ( 4 == result
&& 0 < x && x < shelf
&& 0 < y && y < slot) {//scanned four items. shelf slot in range
printf ( "%s\n", itemName);
printf ( "%f\n", cash);
printf ( "%d\n", x);
printf ( "%d\n", y);

//store in thing[x][y]
}
else {
printf ( "\n\tproblem with input.");
if ( 0 > x || x >= shelf) {
printf ( " bad shelf.");
}
if ( 0 > y || y >= slot) {
printf ( " bad slot.");
}
printf ( " try again\n\n");
}
}
else {
fprintf ( stderr, "fgets EOF\n");
//free structures then the pointers
for (int i = 0; i < shelf; i++) {
free ( thing[i]);
}
free ( thing);

return 0;
}
}
//free structures then the pointers
for (int i = 0; i < shelf; i++) {
free ( thing[i]);
}
free ( thing);

return 0;
}

关于c - 如何避免C中的sscanf()引起无限的while循环,而不是存储用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58055274/

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