2017-02-11 6 views
-1

У меня есть проблема с функциейLine прыжок, прежде чем поплавок

void imprimir_producto(t_producto); 

Линия разрыва печатается перед тем поплавка. Я думаю, что проблема в функции t_producto leer_producto(void); также.

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

typedef struct Producto 
{ 
    int codigo_producto; 
    char descripcion[20]; 
    float precio_unitario; 

} t_producto; 

float aplicar_iva(float precio_base); 
void emitir_saludo(void); 
void imprimir_producto(t_producto); 
t_producto leer_producto(void); 

int main() 
{ 

    t_producto productos[2]; 
    t_producto producto; 
    char decision; 
    int i, cantidad; 
    float total; 

    cantidad =0; 
    total = 0.0; 

    emitir_saludo(); 

    while(cantidad <2) 
    { 
     do 
     { 
      printf("\nHay %d productos en el carrito. ¿Quiere pasar otro producto? [s/n]: ",cantidad); 
      decision = getchar(); 
      while(getchar()==EOF); 
     }while(decision != 's' && decision != 'S' && decision != 'n' && decision != 'N'); 

     if(decision=='n' || decision == 'N') 
     { 
      break; 
     } 

     producto = leer_producto(); 
     productos[cantidad++] = producto; 
    } 
    puts("\nPRODUCTOS:\n"); 
    for(i=0;i<cantidad;i++) 
    { 
     imprimir_producto(productos[i]); 
     total += productos[i].precio_unitario; 
     //es lo mismo que total = productos[i].precio_unitario + toal; 
    } 

    printf("\nTotal deproductos: %d\n\n",cantidad); 
    printf("Precio total sin IVA: %.2f\n",total); 
    printf("Precio total con IVA: %.2f\n",aplicar_iva(total)); 
    printf("\nBuenos dias.\n"); 


    return 0; 
} 

float aplicar_iva(float precio_base) 
{ 
    return precio_base * 1.21; 
} 

void emitir_saludo(void) 
{ 
    printf("\n* * * * * * * * * * * * * * * * * *"); 
    printf("\n* * PROGRAMA SUPERMERCADO * *\n"); 
    printf("* * La calidad es lo primero * *\n"); 
    printf("* * * * * * * * * * * * * * * * * *\n"); 
} 

void imprimir_producto(t_producto t) 
{ 
    printf("%d %s %f\n",t.codigo_producto,t.descripcion,t.precio_unitario); 
} 

t_producto leer_producto(void) 
{ 
    t_producto p; 
    char entrada[80]; 

    printf("\nCodigo producto: "); 
    fgets(entrada,10,stdin); 
    if(entrada[strlen(entrada)+1] == 'n') 
    { 
     entrada[strlen(entrada)+1] == '\0'; 
    } 
    p.codigo_producto = (int) strtol(entrada,NULL,10); 

    printf("Descripcion: "); 
    fgets(p.descripcion,20,stdin); 
    if(p.descripcion[strlen(p.descripcion)+1] == 'n') 
    { 
     p.descripcion[strlen(p.descripcion)+1] == '\0'; 
    } 


    printf("Precio: "); 
    fgets(entrada,10,stdin); 
    if(entrada[strlen(entrada)+1] == 'n') 
    { 
     entrada[strlen(entrada)+1] = '\0'; 
    } 
    p.precio_unitario = strtof(entrada,NULL); 

    return p; 
} 
+0

'if (entrada [strlen (entrada) +1] == 'n')' - что вы проверяете на 'strlen (entrada) + 1' и почему? Это выходит за рамки строки. И в чем важность «n»? – AnT

+0

Я был не прав писать, но я исправил его. Спасибо – EmiliOrtega

ответ

1
  1. Вы дважды написали == 'n' вместо == '\n'. (Я полагаю, что вы пытались избавиться от задней новой строки.)

  2. В тех же двух местах вы ошибочно искали персонажа в [strlen()+1] вместо [strlen()-1]. Рассмотрим также, что происходит, когда strlen() равно нулю.

+0

Lol, Спасибо, я не понял, что я положил «n» , но у меня есть вопрос ... Я понимаю, что strlen возвращает размер массива без учета «\ 0», например, если массив имеет размер 10 плюс \ 0 - 11, поэтому я ставлю strlen + 1 – EmiliOrtega

+0

@EmiliOrtega: Предположим, что строка '' abc "'. 'strlen (" abc ")' равно 3. '" abc "[0]' is ''a'',' "abc" [1] 'is'' b'' и '" abc "[2]' is ' 'c''. В общем, первый символ в строке имеет индекс '[0]', а последний имеет индекс '[strlen() - 1]'. Окончательный '' \ 0'' находится в '[strlen()]'. – AlexP

+0

oooh теперь я понял, спасибо – EmiliOrtega