简体   繁体   中英

C++ pointer runtime error

I made a program using structure and pointer. But for some reason it is not working properly. The main problem is that, for-loop would no go as it would. It would be helpful if you could solve this problem

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct Book{
    string name;
    int release;
};

int main(){
    //local variable
    int i;
    string release_dte;
    int choice;
    //interface
    cout << "Welcome to Book Storage CPP" << endl;
    cout << "How many entries would you like to make: ";
    cin >> choice;
    Book* Issue = new Book[choice];
    //for handler
    for (i = 0; i < choice; i++){
        cout << "Book: ";
        getline(cin, Issue[i].name);
        cout << "Release Date: ";
        getline(cin, release_dte);
        Issue[i].release = atoi(release_dte.c_str());
    }
    cout << "These are your books" << endl;
    for ( i = 0; i < choice; i++){
        cout << "Book: " << Issue[i].name << " Release Date: " << Issue[i].release << endl;
    }
    system("pause");
    return 0;
} 

You're not checking if the input succeeded, nor are you clearing the new line left after the extraction into choice :

 if ((std::cout << "Book: "), std::getline(std::cin >> std::ws, Input[i].name) && (std::cout << "Release Date: "), std::getline(std::cin >> std::ws, release_dte)) { Input[i].release = std::stoi(release_dte); } 

You should also be using std::stoi for C++ strings as shown above.

I could not exactly infer what is the problem you are referring to. But my guess is getline() function inside the for loop is not working properly, I suggest the code before the for loop to be like the following\\

cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');

for (i = 0; i < choice; i++){
    cout << "Book: ";
    getline(cin, Issue[i].name);
    cout << "Release Date: ";
    getline(cin, release_dte);
    Issue[i].release = atoi(release_dte.c_str());
}

Your final code should be

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct Book{
  string name;
  int release;
};

int main(){
//local variable
int i;
string release_dte;
int choice;
//interface
cout << "Welcome to Book Storage CPP" << endl;
cout << "How many entries would you like to make: ";
cin >> choice;
Book* Issue = new Book[choice];

cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');

//for handler
for (i = 0; i < choice; i++){
    cout << "Book: ";
    getline(cin, Issue[i].name);
    cout << "Release Date: ";
    getline(cin, release_dte);
    Issue[i].release = atoi(release_dte.c_str());
}
cout << "These are your books" << endl;
for ( i = 0; i < choice; i++){
    cout << "Book: " << Issue[i].name << " Release Date: " << Issue[i].release << endl;
}
system("pause");
return 0;

}

This will work as you intended

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM