The line of the printing code does not print well

1

I made this code when I asked for the data, but afterwards when I want to show the data I asked for the printout goes wrong, could you please tell me what I have to do to make the printout please. I leave an image so that they see the final impression, only the name of the last book nothing else shows me. Please I need your help to solve this and in advance thanks

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

void main(){
    int lib;
    char libro[50];
    cout<<"Cantidad de libros: ";
    cin>>lib;
    for(int i=0;i<lib;i++){
        cout<<"Nombre del libro "<<i+1<<" :";
        fflush(stdin);
        gets(libro);
        cout<<endl;
    }
    for(int i=0;i<lib;i++){
        cout<<"Nombre del libro "<<i+1<<" : "<<libro<<endl;
    }
    system("pause");
}
    
asked by Raul Martinez 03.09.2018 в 02:54
source

2 answers

3

As you said iBug you want to save two books, but you only have space for one.

If you want more books, you will need more space for books, the solution proposed by iBug is correct and functional (limited to a maximum of 10 books), but I advise you a solution more in line with the language, first of all I will point out your mistakes more obvious:

  • Headers <stdlib.h> and <stdio.h> are C, not C ++, if you really need to use them (you do not need them) you must use the C ++ equivalents: <cstdlib> and <cstdio> . See this question to find out why.
  • The way to work with character strings in C ++ is using objects of type std::string (accessible after include the header <string> ) not with formations 1 of characters.
  • The way to work with arbitrary size collections in C ++ is to use object containers, for example std::list (accessible after including header <list> ) not with formations 1 .
  • The use of using namespace std; in the global scope is not advised, if it is necessary to use it (it is not usually) it should be used in the smallest possible environment. See this thread to find out why.
  • It is not usually necessary to use std::endl , it is more efficient to use an explicit line break \n , see this thread to find out why.
  • The main function, must return a value. See this thread to find out why.
  • Encourages pre-increment versus post-increment .

    li>

Taking into account all these corrections your code could look like this:

#include <iostream>

int main(){
    using lista_libros = std::list<std::string>;

    lista_libros libros;
    int lib;

    std::cout << "Cantidad de libros: ";
    std::cin >> lib;

    for (int i = 0; i < lib; ++i) {
        std::cout << "Nombre del libro " << i + 1 << " :";
        std::string libro;

        std::cin >> libro;
        libros.push_back(libro);
    }

    lib = 0;

    for (const auto &libro : libros) {
        std::cout << "Nombre del libro " << ++lib << " : " << libro << '\n';
    }

    return 0;
}
  • Also known as arrays or in English arrays .
  • answered by 03.09.2018 в 08:14
    2

    You are writing libro in the first loop of for for each time. I think you should use an array of arrays:

    char libro[10][50];
    //        ^^^^
    cout<<"Cantidad de libros: ";
    cin>>lib;
    for(int i=0;i<lib;i++){
        cout<<"Nombre del libro "<<i+1<<" :";
        fflush(stdin);
        gets(libro[i]);
        cout<<endl;
    }
    for(int i=0;i<lib;i++){
        cout<<"Nombre del libro "<<i+1<<" : "<<libro[i]<<endl;
    }
    
        
    answered by 03.09.2018 в 03:23