What am I doing wrong- struct/for loop/array project

This is my program so far for a school project. Everything works as it should, it compiles and will run (in command prompt), but once it gets to the last set of data inputs for how ever many books, it will crash randomly with the microsoft error report thing.

Sorry if this is confusing, I am not yet good at describing the program or know the correct terminology.

And, Thanks for any input/help, it is greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;

int howmany() {    //gets the number of books being entered
    int num;
    cout << "How many books do you have?\n";
    cin >> num;
    cout << endl;
    return num;
}

string stringit(double num) {           //converts number to string
       std::ostringstream conv;
       conv << num;
       return conv.str();
}


int main() {    


int amnt = howmany();          //variables and structs are declared in this area  
struct bookinfo {
       string title;       
       string author;
       string publisher;       
       double isbn;     
       };
bookinfo books[amnt];
string together[amnt];


for (int count=1; count <= amnt; count++) { //gets the book info from user for specified amount of books
cin >> books[count].title;
cin >> books[count].author;
cin >> books[count].publisher;
cin >> books[count].isbn;

//following line converts the struct into 1 string
together[count] = books[count].title + "\n" + books[count].author + "\n" + books[count].publisher + "\n" + stringit(books[count].isbn);
cout << endl << endl << together[count] << endl << endl;
}


return 0;
}
You can't use a non-const int to specify the size of arrays. You likely have a compiler extension enabled that allows it, but it's not legal C++.

for (int count=1; count <= amnt; count++)

Valid indices into an array are 0 to size-1.

You're using 1 to size.
Hi, thank for the help. I fixed that but it didn't make a difference. Fortunately, after about an hour of picking apart the program and taking it apart I realized that I had simply forgotten that an array will start at array[0] for its first member, and I was going beyond the array. Ironically we did an example in class showing this exact thing. Oh well, I guess it's part of learning. Thanks again for the help.
Topic archived. No new replies allowed.