- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直工作在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进行解析。其他一些有用的解析函数是strtol
,strtod
,strspn
和strcspn
。还有更多。
#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/
我正在尝试从par_iter()内部捕捉到 panic ,并继续执行par_iter块之后的操作。 如果我有这个,我会正确地获得一切,并且不会出现 panic : let dog: Dog = Dog
我可以假设从 JDK 类加载机制抛出的每个 NoClassDefFoundError 总是在堆栈跟踪中将 ClassNotFoundException 作为原因吗? 另外,NoClassDefFoun
我有下面的程序 package com; import java.io.PrintStream; import java.net.URL; import java.net.UR
我有一些由另一组人编写的简单代码(目前不可用),它引用了我得到的一些 jar 文件。当我编译代码时,一切都构建得很好,但是一旦代码尝试创建在其中一个 jar 文件中定义的类的实例,我就会收到 java
我正在尝试按照 https://github.com/airbrake/airbrake-django#manually-sending-errors-to-airbrake 上的示例进行操作手动向
我不确定为什么这是递归的。 jTable1.getModel().addTableModelListener(new TableModelListener() { public void table
我按照 https://github.com/cloudfoundry/vcap 上的自述文件进行操作 它应该工作正常... 但我得到了这样的错误: 有谁知道发生了什么? 我在 Ubuntu10.04
我只是想知道当你有 UI-Router 的空白页面时,有人知道如何调试情况。 (当然,控制台没有任何错误) 通过为路由器事件执行 console.log(取自 here),我发现它进入了正确的状态,但
我们的网站上有一个问题,一些 Firefox 用户在访问我们的网站时会收到“错误请求”消息(仅此而已,只是“错误请求”字样!) 这似乎是由于 google 跟踪 cookie 损坏,可能是 __utm
在使用guard-rspec在Rails 4项目中运行guard时,在vim中打开/关闭文件时偶尔会看到以下错误。我试过升级/降级guard,guard-rspec,pry和其他没有运气的库。 rub
今天我在编写程序时遇到了这个错误。 Caused by:java.lang.ClassCastException: org.cubeville.blocks.CrossedBlockBrush can
我在执行应用程序时遇到空指针异常,但我不确定原因。问题发生在线路上: task.execute(""); 但我不确定为什么会出现空指针异常。 (我已经验证我有互联网连接,并且它所连接的 XML
嗨,我有一个 java 应用程序,我正在尝试使用它写入 tempDir,但我仍然遇到以下异常。我承认我对编写文件不太了解,所以希望我缺少一些小东西。 Caused by: java.io.FileNo
我不明白为什么会发生这种情况。我对其他问题做了一些研究,发现使用 for 循环时无法修改集合。但是,我正在使用迭代器,为什么它不起作用? int counter = 0; int otherC
目前我正在使用 OSX Server (Yosemite) 来托管一堆 PHP 应用程序,其中一些应用程序在网站文档根目录下有一个子目录用于子域。自更新到 Yosemite 版本的 OSX Serve
SqlCommand objsql = new SqlCommand(); . . objsql.Parameters.AddWithValue("@Param1", DBNull.Value); .
当我尝试将“对象”添加到数据库然后将其显示到 TableView 时,它显示 UnsupportedOperationException 。一切都很好,直到我将此代码添加到“public void i
我收到以下错误日志: 05-29 20:57:29.886: D/AndroidRuntime(359): Shutting down VM 05-29 20:57:29.896: W/dalvikv
我有两个项目,第一个是Ejb3项目,名称是SessionBean,另一个是java项目,名称是SessionBeanClient。对于 IDE,我使用 eclipse indigo。我已经完成了代码,
我有一个使用表单成员身份验证的 ASP.NET Web 应用程序。我们最近进行了渗透测试,标记的一个问题是窃取用户帐户的能力。如果 .ASPXAUTH cookie 值是在注销之前从用户复制的,用户可
我是一名优秀的程序员,十分优秀!