C 跳过函数的一个命令?
C skipping one command of a function?
我正在编写模拟程序,当用户选择创建新标签时,用户应该输入标签 ID、标签所有者以及标签代表的对象。该程序正在做的只是跳过扫描所有者的命令,我不太清楚为什么。我的代码如下(功能在iotlib.cpp):
iotlib.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 20
struct tagInfo
{
char owner[MAX];
char object[MAX];
int id;
};
struct tre //TRE = Tag Read Event
{
int id;
char node[MAX];
int dx;
};
void initTag(struct tagInfo tag[], int numTags)
{
for(int i=0; i<numTags; i++)
{
printf("Enter the tag ID number: ");
scanf("%i", &tag[i].id);
printf("Enter owner of tag: ");
scanf("%c", &tag[i].owner);
printf("Enter the object the tag is attached to: ");
scanf("%c", &tag[i].object);
}
}
void generateTRE(struct tre event[], int numEvents)
{
for(int i=0; i<numEvents; i++)
{
printf("Enter tag ID: ");
scanf("%i", &event[i].id);
printf("Enter node: ");
scanf("%c", &event[i].node);
printf("Enter distance from node as an integer number of feet: ");
scanf("%c", &event[i].dx);
}
}
void triangulationSimulate(struct tre event1, struct tre event2, int numEvents)
{
if(numEvents>1 && event1.id==event2.id)
{
printf("Node %c", event1.node);
for(int i=0; i<event1.dx; i++)
{
printf(" ");
}
printf("Tag %i", event1.id);
for(int i=0; i<event2.dx; i++)
{
printf(" ");
}
printf("Node %c", event2.node);
}
}
void getTagInfo(struct tagInfo tag)
{
printf("The tag with ID %i represents a/an %c belonging to %c", tag.id, tag.object, tag.owner);
}
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "iotlib.cpp"
void execute();
int main(void)
{
execute();
return 0;
}
void execute()
{
struct tagInfo tags[5];
struct tre events[5];
int choice, numTags, numEvents;
printf("!Simulation supports a maximum of 5 tags and 5 nodes!\n\n");
printf("Choose a function by entering it's number:\n1. Create tags\n2. Generate Tag Read Events\n3. Triangulate tag\n4. Recall tag metadata\n\n");
scanf("%i", &choice);
if(choice==1)
{
printf("Enter the number of tags to initialize (max of 5): ");
scanf("%i", &numTags);
if(numTags<1 || numTags>5)
{
printf("Invalid datum.\n");
}
else
{
initTag(tags, numTags);
}
}
else if(choice==2)
{
printf("Enter the number of TRE's to be generated (max of 5: ");
scanf("%i", &numEvents);
if(numEvents<1 || numEvents>5)
{
printf("Invalid datum.\n");
}
else
{
generateTRE(events, numEvents);
}
}
else if(choice==3)
{
int eventX, eventY;
printf("Enter two existing TRE numbers to use, separated by a space: ");
scanf("%i %i", &eventX, &eventY);
triangulationSimulate(events[eventX], events[eventY], numEvents);
}
else if(choice==4)
{
int tagNum;
printf("Enter a tag number: ");
scanf("%i", &tagNum);
getTagInfo(tags[tagNum-1]);
}
else
{
printf("Invalid selection.\n");
}
execute();
}
点 1[程序错误]
这里的问题是使用 %c
格式说明符。它 计数 先前输入的 \n
,在先前输入后按 ENTER 键存储。你要的是
scanf(" %c", &tag[i].owner);
^
|
note the space
在实际输入之前跳过任何前导空白字符(包括 \n
)。
点2[逻辑错误]
根据您此处的代码,要 scan 一个 string 输入,您需要使用 %s
格式说明符。
所以,最后,您的代码应该如下所示
scanf("%s", tag[i].owner); // if tag[i].owner is char array
或
scanf(" %c", &tag[i].owner); // if tag[i].owner is a char, just in case
%c 是字符的说明符,您要输入的是字符串,而不是字符。 See scanf documentation。您要使用的是字符串的 %s 说明符。
我正在编写模拟程序,当用户选择创建新标签时,用户应该输入标签 ID、标签所有者以及标签代表的对象。该程序正在做的只是跳过扫描所有者的命令,我不太清楚为什么。我的代码如下(功能在iotlib.cpp):
iotlib.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 20
struct tagInfo
{
char owner[MAX];
char object[MAX];
int id;
};
struct tre //TRE = Tag Read Event
{
int id;
char node[MAX];
int dx;
};
void initTag(struct tagInfo tag[], int numTags)
{
for(int i=0; i<numTags; i++)
{
printf("Enter the tag ID number: ");
scanf("%i", &tag[i].id);
printf("Enter owner of tag: ");
scanf("%c", &tag[i].owner);
printf("Enter the object the tag is attached to: ");
scanf("%c", &tag[i].object);
}
}
void generateTRE(struct tre event[], int numEvents)
{
for(int i=0; i<numEvents; i++)
{
printf("Enter tag ID: ");
scanf("%i", &event[i].id);
printf("Enter node: ");
scanf("%c", &event[i].node);
printf("Enter distance from node as an integer number of feet: ");
scanf("%c", &event[i].dx);
}
}
void triangulationSimulate(struct tre event1, struct tre event2, int numEvents)
{
if(numEvents>1 && event1.id==event2.id)
{
printf("Node %c", event1.node);
for(int i=0; i<event1.dx; i++)
{
printf(" ");
}
printf("Tag %i", event1.id);
for(int i=0; i<event2.dx; i++)
{
printf(" ");
}
printf("Node %c", event2.node);
}
}
void getTagInfo(struct tagInfo tag)
{
printf("The tag with ID %i represents a/an %c belonging to %c", tag.id, tag.object, tag.owner);
}
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "iotlib.cpp"
void execute();
int main(void)
{
execute();
return 0;
}
void execute()
{
struct tagInfo tags[5];
struct tre events[5];
int choice, numTags, numEvents;
printf("!Simulation supports a maximum of 5 tags and 5 nodes!\n\n");
printf("Choose a function by entering it's number:\n1. Create tags\n2. Generate Tag Read Events\n3. Triangulate tag\n4. Recall tag metadata\n\n");
scanf("%i", &choice);
if(choice==1)
{
printf("Enter the number of tags to initialize (max of 5): ");
scanf("%i", &numTags);
if(numTags<1 || numTags>5)
{
printf("Invalid datum.\n");
}
else
{
initTag(tags, numTags);
}
}
else if(choice==2)
{
printf("Enter the number of TRE's to be generated (max of 5: ");
scanf("%i", &numEvents);
if(numEvents<1 || numEvents>5)
{
printf("Invalid datum.\n");
}
else
{
generateTRE(events, numEvents);
}
}
else if(choice==3)
{
int eventX, eventY;
printf("Enter two existing TRE numbers to use, separated by a space: ");
scanf("%i %i", &eventX, &eventY);
triangulationSimulate(events[eventX], events[eventY], numEvents);
}
else if(choice==4)
{
int tagNum;
printf("Enter a tag number: ");
scanf("%i", &tagNum);
getTagInfo(tags[tagNum-1]);
}
else
{
printf("Invalid selection.\n");
}
execute();
}
点 1[程序错误]
这里的问题是使用 %c
格式说明符。它 计数 先前输入的 \n
,在先前输入后按 ENTER 键存储。你要的是
scanf(" %c", &tag[i].owner);
^
|
note the space
在实际输入之前跳过任何前导空白字符(包括 \n
)。
点2[逻辑错误]
根据您此处的代码,要 scan 一个 string 输入,您需要使用 %s
格式说明符。
所以,最后,您的代码应该如下所示
scanf("%s", tag[i].owner); // if tag[i].owner is char array
或
scanf(" %c", &tag[i].owner); // if tag[i].owner is a char, just in case
%c 是字符的说明符,您要输入的是字符串,而不是字符。 See scanf documentation。您要使用的是字符串的 %s 说明符。