2017-02-11 96 views
-1

之前我有个问题与功能线跳飘

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

'如果(ENTRADA [strlen的(ENTRADA)+1] == 'N')' - 你这是在'strlen的(ENTRADA)+ 1'指数,为什么检查?这超出了字符串的末尾。 ''n''的重要性是什么? – AnT

+0

我写错了,但我纠正了它。由于 – EmiliOrtega

回答

1
  1. 您曾两次书面== 'n',而不是== '\n'。 (我假定你试图摆脱尾随换行符的。)

  2. 在你错误地寻找人物在[strlen()+1]而不是[strlen()-1]相同的两个地方。考虑当strlen()为零时会发生什么。

+0

大声笑,谢谢,我不知道,我把“N” 但是,我有一个问题......据我所知,strlen的返回数组的大小不计的“\ 0”,例如,如果阵列是大小10加\ 0是11,所以我把strlen的+ 1 – EmiliOrtega

+0

@EmiliOrtega:假设该字符串'“ABC”'。 'strlen(“abc”)'是3.'“abc”[0]'是''a'',''abc“[1]'是''b''和'”abc“[2]'是''c''。通常,字符串中的第一个字符的索引为[0],最后一个索引为[strlen() - 1]。最后的'\ 0''在'[strlen()]'处。现在 – AlexP

+0

哦我理解,由于 – EmiliOrtega