ç跳过函数的一个命令? [英] C skipping one command of a function?

查看:96
本文介绍了ç跳过函数的一个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程模拟,当用户选择创建一个新的标签,用户应该输入标签ID,标签的拥有者,该对象的标签重新presents。什么程序做的是刚刚跳过,扫描所有者的命令,我不明白为什么。我的codeS低于(该功能在iotlib.cpp):

iotlib.cpp

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&math.h中GT;
#定义MAX 20结构tagInfo
{
    焦炭老板[MAX];
    char对象[MAX];
    INT ID;
};
结构TRE // TRE =标签读取事件
{
    INT ID;
    焦炭节点[MAX];
    INT DX;
};
无效initTag(结构tagInfo标签[],诠释numTags)
{
    的for(int i = 0; I< numTags;我++)
    {
        的printf(请输入标签的ID号:);
        scanf函数(%i的,&安培;标签[I] .ID);
        的printf(请输入标签的老板:);
        scanf函数(%C,&安培;标签[I] .owner);
        输出(输入标签附着到对象:);
        scanf函数(%C,&安培;标签[I] .object);
    }
}无效generateTRE(TRE结构活动[],诠释numEvents)
{
    的for(int i = 0; I< numEvents;我++)
        {
            的printf(请输入标签编号:);
            scanf函数(%i的,&安培;事件[I] .ID);
            的printf(请输入节点:);
            scanf函数(%C,&安培;事件[I] .node);
            的printf(从节点英尺的整数输入距离:);
            scanf函数(%C,&安培;事件[I] .dx);
        }
}无效triangulationSimulate(TRE结构EVENT1,结构TRE EVENT2,诠释numEvents)
{
    如果(numEvents→1&放大器;&放大器; event1.id == event2.id)
    {
        的printf(%节点C,event1.node);
        的for(int i = 0; I< event1.dx;我++)
        {
            的printf();
        }        的printf(%标签I,event1.id);        的for(int i = 0; I< event2.dx;我++)
        {
            的printf();
        }        的printf(%节点C,event2.node);
    }
}无效getTagInfo(结构tagInfo标签)
{
    的printf(,tag.id,tag.object,tag.ownerID为%我重新presents一/一%C属于%C标签);
}

的main.cpp

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&math.h中GT;
#包括iotlib.cpp无效的execute();INT主要(无效)
{
    执行();
    返回0;
}无效的execute()
{
    结构tagInfo代码[5];
    结构特雷事件[5];
    INT选择,numTags,numEvents;
    的printf(!仿真支持最多5个标签和5个节点的\\ n \\ n!);
    的printf(选择输入功能它的一些方面:\\ n1创建变量\\ N2生成标签读取活动\\ N3三角测量标签\\ N4回想标签元\\ n \\ n。);
    scanf函数(%i的,&安培;选择);
    如果(选择== 1)
    {
        的printf(请输入标签的数量来初始化5(最大值):);
        scanf函数(%i的,&安培; numTags);
        如果(numTags&。1 || numTags→5)
        {
            的printf(无效的数据\\ n);
        }
        其他
        {
            initTag(标签,numTags);
        }
    }
    否则,如果(选择== 2)
    {
        的printf(输入TRE的数量将要生成的5(最大:);
        scanf函数(%i的,&安培; numEvents);
        如果(numEvents&。1 || numEvents→5)
        {
            的printf(无效的数据\\ n);
        }
        其他
        {
            generateTRE(事件,numEvents);
        }
    }
    否则,如果(选择== 3)
    {
        INT eventX,eventY;
        的printf(请输入现有的两个数字TRE使用,用空格分隔:);
        scanf函数(%I%I,&安培; eventX,&安培; eventY);
        triangulationSimulate(事件[eventX],事件[eventY],numEvents);
    }
    否则,如果(选择== 4)
    {
        INT tagNum;
        的printf(请输入标签编号:);
        scanf函数(%i的,&安培; tagNum);
        getTagInfo(标签[tagNum-1]);
    }
    其他
    {
        的printf(无效的选择\\ n);
    }    执行();
}


解决方案

点1 [编程错误]

这里的问题是与%C 格式说明符的使用。它的计数的pviously进入了$ P $ \\ n ,由pressing的<大骨节病> ENTER $ P键后存储$ pvious输入。你想要的是

  scanf函数(%C,&安培;标签[I] .owner);
       ^
       |
    注意空格

要跳过任何前导空格字符一样的实际输入之前(包括 \\ n )。

点2 [逻辑错误]

根据您的code在这里,要的扫描的一个的字符串的输入,你需要使用%S 格式说明。

所以,最后,你的code应该像

  scanf函数(%S标记[I] .owner); //如果标记[I] .owner是字符数组

  scanf函数(%C,&安培;标签[I] .owner); //如果标记[I] .owner是char,以防万一

I am programming a simulation, and when the user chooses to create a new tag, the user is supposed to enter a tag ID, the tag's owner, and the object the tag represents. What the program is doing is just skipping over command that scans for the owner, and I'm not quite sure why. My codes are below (the functions are in 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();
}

解决方案

Point 1 [Programmatic error]

The problem here is the usage with %c format specifier. It counts the previously entered \n, stored by pressing the ENTER key after previous input. What you want is

scanf(" %c", &tag[i].owner);
       ^
       |
    note the space

to skip any leading whitespace like character (including \n) before the actual input.

Point 2 [Logical Error]

As per your code here, to scan a string input, you need to use %s format specifier.

So, finally, your code should look like

   scanf("%s", tag[i].owner);    // if tag[i].owner is char array

or

  scanf(" %c", &tag[i].owner);    // if tag[i].owner is a char, just in case

这篇关于ç跳过函数的一个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆