繁体   English   中英

使用结构在 c 中获得奇怪的输出

[英]Getting weird output in c with structures

我是 C 编程的新手,只是做作业,事情是,我不明白为什么选择“Visualizar platos del dia”会输出奇怪的符号,而不是我在 cargar 结构中输入的东西。

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

struct platos{
    char aperitivo[40];
    char plato1[40];
    char plato2[40];
    char postre[40];
    char bebida1[40];
    char bebida2[40];
    char bebida3[40];
};

void cargar(struct platos a);
void vis(struct platos a);
void emit(struct platos a);

int main(){
    int a=0, b=0;
    struct platos plato;
do{
    printf("Bienvenido a restaurante 'Senior bigotes'\n\n");
    printf("1:Cargar platos del dia\n");
    printf("2:VIsualizar platos del dia\n");
    printf("3:Emitir comanda y factura\n");
    printf("4:Salir\n");
    scanf("%d", &a);
    
    switch(a){
        case 1: cargar(plato);
        break;
        case 2: vis(plato);
        break;
        case 3: emit(plato);
        break;    
        case 4: 
         b++;
        break;    
    }
    
    }while(b<1);

  return 0;
}

void cargar(struct platos a){
    printf("Ingrese aperitivo\n");
    __fpurge(stdin); 
    gets(a.aperitivo);
    printf("Ingrese plato 1\n");
    __fpurge(stdin);
    gets(a.plato1);
    printf("Ingrese plato 2\n");
    __fpurge(stdin);
    gets(a.plato2);
    printf("Ingrese postre\n");
    __fpurge(stdin);
    gets(a.postre);
    printf("Ingrese bebida 1\n");
    __fpurge(stdin);
    gets(a.bebida1);
    printf("Ingrese bebida 2\n");
    __fpurge(stdin);
    gets(a.bebida2);
    printf("Ingrese bebida 2\n");
    __fpurge(stdin);
    gets(a.bebida3);
}


void vis(struct platos a){
    printf("\nAperitivo: %s", a.aperitivo);
    printf("\nPlato 1: %s", a.plato1);
    printf("\nPlato 2: %s", a.plato2);
    printf("\nPostre: %s", a.postre);
    printf("\nBebidas: %s | %s | %s\n", a.bebida1, a.bebida2, a.bebida3);
    int c = getchar();
}

void emit(struct platos a){
    printf("\nAperitivo: %s $10", a.aperitivo);
    printf("\nPlato 1: %s $25", a.plato1);
    printf("\nPlato 2: %s $35", a.plato2);
    printf("\nPostre: %s $20", a.postre);
    printf("\nBebidas: %s | %s | %s $15\n", a.bebida1, a.bebida2, a.bebida3);
    printf("Total: $105");
    int c = getchar();
}

我正在使用带有可视代码的 manjaro 顺便说一句。 我问老师,他要我调试代码,但我知道如何在可视代码中做到这一点。 有什么帮助吗?

如果您指的是 Visual Studio Code,它有一个Run菜单项,其中包含调试项,例如Toggle BreakpointStart Debugging

第一个可用于您要开始调试的语句,第二个可用于开始运行您的代码。 一旦断点被​​命中,屏幕上应该有一个小面板,其中包含继续、跳过、进入等控件:

在此处输入图像描述

如果您将鼠标悬停在这些控件上,它还应该显示您可以使用的等效键盘命令。


但是,您应该知道,对于您的直接问题,结构在 C 中是通过传递的,而不是通过引用传递的。 这意味着cargar函数获取plato副本并填充该副本的字段。

然后该副本在从cargar返回时被丢弃,留下原始plato与它在创建时给出的任意字段。 因此,当随后将其传递给visemit时,您将看不到您输入的数据。

最简单的解决方案可能是将指针( &plato )传递给cargar ,并使用->而不是. 访问该函数中的字段。 换句话说,类似(请参阅下面关于我为什么使用fgets()的注释):

switch (a) {
    case 1: cargar(&plato); break;

void cargar(struct platos const *a_ptr){
    printf("Ingrese aperitivo\n");
    __fpurge(stdin); 
    fgets(a_ptr->aperitivo, sizeof(a_ptr->aperitivo), stdin);

    // And so on for the other fields ...

*a_ptr之前的特定const适用于指针值本身,而不是指针“后面”的值。 需要能够更改该值,因为这是该功能的全部目的。

但是,我也会为vis()emit()做类似的事情,以免复制大型结构,但我会使用(例如):

void vis(const struct platos const *plato) {

明确指出指针和指针后面的值都不会改变。


而且,正如其他人在评论中提到的那样,您应该使用fgets()而不是gets() ,没有办法安全地使用后者。 如果您想知道如何将其用于用户输入,请参阅此答案

它应该表明gets()被认为是多么糟糕,它实际上已被弃用并从标准中删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM