C 浮前跳线

C 浮前跳线,c,C,我的功能有问题 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[2

我的功能有问题

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;
}
#包括
#包括
#包括
类型定义结构产品
{
int codigo_producto;
字符描述[20];
浮式沉淀池;
}t_producto;
浮动应用程序iva(浮动精度基准);
void emitter_saludo(void);
无效的进口产品(t产品);
产品无效;
int main()
{
t_producto productos[2];
t_producto producto;
字符决策;
int i,康蒂达;
浮动总额;
cantidad=0;
总数=0.0;
emitter_saludo();
while(cantidad)
  • 您已经两次编写了
    =='n'
    而不是
    ='\n'
    (我猜您是在试图摆脱尾随的换行符。)

  • 在相同的两个地方,您错误地查找了字符在<代码> [StRelf(+)+1 ] < /C> >而不是<代码> [STROLN()- 1 ] < /代码>。还考虑当<代码> STARLY()//> > 0时发生了什么。


  • if(entrada[strlen(entrada)+1]='n')
    -您在
    strlen(entrada)检查什么+1
    索引以及为什么?这超出了字符串的结尾。而
    'n'
    的重要性是什么?我写错了,但我更正了它。谢谢,谢谢,我没有意识到我放了'n',但是,我有一个问题…我知道strlen返回数组的大小而不计算'\0',例如,如果数组的大小是10 pl我们\0是11,所以我把strlen+1@EmiliOrtega:假设字符串是
    “abc”
    字符串(“abc”)
    是3。
    “abc”[0]
    “a”
    “abc”[1]
    “b”
    “abc”[2]
    “c”
    。通常,字符串中的第一个字符具有索引
    [0]
    最后一个有索引
    [strlen()-1]
    。最后一个
    '\0'
    [strlen()]
    处。在字符串结尾处切换换行符的安全习惯用法是
    entrada[strcspn(entrada,“\n”)]='\0';
    -如果没有换行符,它会在空字节上写入一个空字节。