Trouble populating and displaying arrays

I'm trying to use a while loop to populate a set of arrays with data entered by a user. I am then passing the arrays to a function to be displayed when prompted by the user. The arrays have an unspecified size because I want the user to be able to enter as many items as they want.

From what I can tell, the program loops correctly, but does not store the data in the specified array on multiple passes. The data is overwritten by the next entry from the user (ie, when I tell the program to display an array, it will only display the last value it was assigned). I'm not sure if the problem is with my while loop or with the display function I used.

Any hints or help would be appreciated.

Here is my code:

Note: I removed some of the data entry fields and their variables to make the code a bit shorter and easier to read.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//Preprocessor commands
#include <iostream>
#include <string>
using namespace std;


//Declare functions
void DisplayStringArray(string[], int);
void DisplayIntArray(int[], int);

//Beginning of inventory program
int main ()
{
     //Declare arrays
     int inventNum[] = {0};
     int purchaseDate[] = {0};
     string titleOf[] = {""};
     string authorOf[] = {""};

     //Declare variables
     char addBook = ' ';
     char displayData = ' ';
     char addMore;
     char displayOption = ' 

     //Begin main loop
     bool endLoop = false;

     while (!endLoop)
     {
          //Prompt to add a book
          cout << "Add a book to the database (Y/N)?: ";
          cin >> addBook;
          while (toupper(addBook) == 'Y')
          {
               //Declare counter variables
               int counter = 1;
               int i = 0;

               //Add book information
               cout << "Inventory number: ";
               cin >> inventNum[i];
               cout << "Book title: ";
               cin >> titleOf[i];
               cout << "Book's author: ";
               cin >> authorOf[i];
               cout << "Date purchased: ";
               cin >> purchaseDate[i]
               
               //Prompt to add another book
               cout << "Add another book (Y/N)?: ";
               cin >> addMore;
               if (toupper(addMore) == 'N')
               {
                    break;
               } //end if

               //Update counter and i
               counter += 1;
               i += 1;
          } //end add while
	
          //Prompt to display the database
          cout << "Display the database (Y/N)?: ";
          cin >> displayData;
          if (toupper(displayData) == 'Y')
          {
               cout << "Please select a display option.\n";
               cout << "\nDisplay by Title - T"; 
               cout << "\nDisplay by Author - A";
               cout << "\nDisplay by Inv. Number - I";
               cout << "\nDisplay by Purchase Date - P";
               cout << "\nEnter selection: ";
               cin >> displayOption;
               if (toupper(displayOption) == 'T')
               {
                    int sizeOfArray = sizeof(titleOf)/sizeof(string);
			
                    DisplayStringArray(titleOf, sizeOfArray);
               } //end if
               else
                    if (toupper(displayOption) == 'A')
                    {
                         int sizeOfArray = sizeof(authorOf)/sizeof(string);
			
                         DisplayStringArray(authorOf, sizeOfArray);
                    } //end if
                    else
                         if (toupper(displayOption) == 'I')
                         {
                              int sizeOfArray = sizeof(inventNum)/sizeof(int);
			
                              DisplayIntArray(inventNum, sizeOfArray);
                         } //end if
                         else
                              if (toupper(displayOption) == 'P')
                              {
                                   int sizeOfArray = sizeof(purchaseDate)/sizeof(int);
			
                                   DisplayIntArray(purchaseDate, sizeOfArray);
                              } //end if
                              else
                                   cout << "Invalid selection. Please try again: ";
          } //end display if
	
          if (toupper(addBook && displayData) != 'Y')
          {
               char closeProg = ' ';

               cout << "Close the program (Y/N)?: ";
               cin >> closeProg;
               if (toupper(closeProg) == 'Y')
               {
                    endLoop = true;
               } //end if
               else 
                    endLoop = false;
          } //end if
} //end main while loop
	
	
     system ("pause");
     return 0;
} //end of main function

//****************************FUNCTIONS*******************************
void DisplayStringArray (string displayString[], int counter)
{
     for (int x = 0; x < counter; x += 1)
          cout << displayString[x] << endl;
} //end of DisplayTitle function

void DisplayIntArray (int displayInt[], int counter)
{
     for (int x = 0; x < counter; x += 1)
          cout << displayInt[x] << endl;
} //end of DisplayIntArray function 


- SlashSign
First of all, unfortunately in c++ (and c) you cannot define a variable sized array like that. When you write for example:
 
char mychar[] = {' '};

the compiler counts how many items are inside your braces and uses that for the size of the array. This will be the size of the array from now till eternity. So you can't use an array like this and keep adding elements to it. One solution that you can use is to just set the size of the array to a large value and hope that your users won't need more entries than that, or you can outright prevent them from adding more entries beyond the array size limit. So for example in your program, do something like this: int inventNum[1000];. This is not a great solution because if you try and access members beyond 1000, you will get a segmentation fault and your program will die.
Thank you for your reply, slicedpan!

I declared my arrays like you suggested ( int inventNum[10]; ) and did a test run. Now it seems my program will recognize that there are supposed to be 10 items in the array, but it doesn't display them.

For example, I run the program, telling it to add a new book and then to add another new book. However, after I exit the addBook loop and try to display the list of authors in the authorOf array, I only get the last author entered followed by several lines of blank space (which I think is supposed to be the rest of the array, but since they have no data in there, a blank is displayed).

The only thing I can think of is that there is something wrong with my counter, but I've looked online and in my textbook and I can't seem to find the problem.
Yeah inside the loop for adding books you have int i = 0, every time this loop executes it will reset it to 0, so every book you add is only overwriting the first entry. Also you should look up std::vector as a replacement for your arrays, i think they would work better. http://www.cplusplus.com/reference/stl/vector/
Thanks for the assistance slicedpan.

I fixed the array by simply moving the counter variables up top.

Also, I will look into using a vector in place of my arrays.

Thanks again for the help!
Topic archived. No new replies allowed.