Home > Blockchain >  Getting weird output in c with structures
Getting weird output in c with structures

Time:06-25

I'm new to C programming and just doing homework, thing is, I cant figure out why this the choice "Visualizar platos del dia" outputs weird symbols and not the things that I've input in with the cargar structure.

#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();
}

I'm using manjaro btw with visual code. I asked the teacher and he wants me to debug the code but Idk how to do that in visual code. Any help?

CodePudding user response:

If you mean Visual Studio Code, it has a Run menu item that contains the the debugging items, such as Toggle Breakpoint and Start Debugging.

The first can be used on the statement you want to start debugging with, and the second to start running your code. Once the breakpoint is hit, there should be a small panel on the screen somewhere with controls for continue, step over, step into, and so on:

enter image description here

If you hover over those controls, it should also show you the equivalent keyboard commands you can use.


However, you should be aware that, for your immediate problem, structures are passed by value in C, not by reference. That means that the cargar function gets a copy of plato and populates the fields of that copy.

Then that copy is thrown away when returning from cargar, leaving the original plato with the arbitrary field it was given at creation time. So, when that is subsequently passed to vis or emit, you won't see the data you entered.

The easiest solution is probably to pass a pointer (&plato) to cargar, and use -> rather than . to access the fields within that function. In other words, something like (see notes below as to why I'm using fgets()):

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

and

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 ...

That particular const before *a_ptr applies to the pointer value itself rather than the value "behind" the pointer. You need to be able to change the value since that's the whole purpose of the function.

However, I'd also do something similar for vis() and emit() so as to not be copying large structures around but I'd use (for example):

void vis(const struct platos const *plato) {

to make it clear neither the pointer nor the value behind the pointer is expected to change.


And, as others have mentioned in comments, you should use fgets() rather than gets(), there is no way to use the latter safely. If you want to know how to use it for user input, see this answer.

It should be an indication of how bad gets() is considered that it's actually been deprecated and removed from the standard.

  •  Tags:  
  • c
  • Related