Problem with an array of structures in a function; weird weird characters

So I have been trying to get this working for a while now and it works right for as much as I have done now. Its incomplete but the first two functions will work and read in information from books.dat but when you type V to view whats held in the struct array, it give me a lot of really weird characters instead of what is from the text file.

I can't figure out what I did wrong!! My brain might just be too fried after hours of writing this...

code:
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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <ctime>
#include <cstring>
using namespace std;

//Structure=======================================================
const int MAXSIZE=25;
const int MAXBOOKS=100;
struct Book
{
      char  ISBN[MAXSIZE];
      char  Title[MAXSIZE];
      char  Author[MAXSIZE];
      int   Year;
      int   Qty;
      float Price;
};

//Function Prototypes=======================================================
void Menu(Book[], int&);
void Read(Book[], int&);
void View(Book[], int&);

int main()
{
      Book Books[MAXBOOKS];
      int numBooks = 0;

      Read(Books, numBooks);
      cout << "Bookstore Menu \n\n";
      Menu(Books, numBooks);

return 0;
}

//Function Definitions======================================================
void Menu(Book Books[], int &count)
{
      char Choice;
      cout << "Select from the following: \n\n";
      cout << "V) View all books \n";
      cout << "A) Add a new book \n";
      cout << "D) Delete a book \n";
      cout << "U) Update a book price and quantity \n";
      cout << "Q) Quit the program \n";
      cout << "Choice? ";

      Choice = toupper(Choice);
      while(Choice != 'Q')
      {
          cin >> ws;
              cin.get(Choice);

              if(Choice == 'V')
                      View(Books, count);

              else
                      cout << "Please type a correct response(V, A, D, U, Q) \n";

   }
      return;
}

void Read(Book Books[], int &count)
{
ifstream Input;
Input.open("Books.dat");
count=0;
while(!Input.eof())
{
       Input.getline(Books[count].ISBN, MAXSIZE);
       Input.getline(Books[count].Title, MAXSIZE);
       Input.getline(Books[count].Author, MAXSIZE);
       Input >> Books[count].Year;
       Input >> Books[count].Qty;
       Input >> Books[count].Price;
       count++;
}
Input.close();
}

void View(Book Books[], int &count)
{
  cout << "Bookstore Inventory \n\n";
  cout << setw(5) << "ISBN NO.";
  cout << setw(5) << "Title";
  cout << setw(5) << "Author";
  cout << setw(5) << "Year";
      cout << setw(5) << "Qty";
  cout << setw(5) << "Price";

      for(int i=0; i<MAXBOOKS; i++)
      {
              cout << setw(5) << Books[i].ISBN;
          cout << setw(5) << Books[i].Title;
          cout << setw(5) << Books[i].Author;
              cout << setw(5) << Books[i].Year;
              cout << setw(5) << Books[i].Qty;
              cout << setw(5) << Books[i].Price;
              cout << endl;
      }
      Menu(Books, count);
}


books.dat file is just the same as a text file with one entry for testing purposes:
1
2
3
4
5
6
123456
Artwork
Bob
1999
15
27.50


Any help is seriously appreciated, and I thank you so much for your time.
Last edited on
[hint] Maybe you are trying to print out too many books [/hint]

(there's some other errors as well (for example you are reading one too many books from the file, and calling the menu function from the View function is not really a good idea - but I'll let you try and cure those on your own first) )
Yeah I just realized that when I changed MAXBOOKS to 1 instead of 100, then realized that I have i<MAXBOOKS which basically was doing that loop 100 times! So I caught that one...

How would you suggest doing the calling order on the menu function? The reason I put it in view function was so it would call the menu again after it is done. I need the menu to show up each time after each function has been run. What is a better approach?
Last edited on
Well...the way you are currently doing it, you will could get an infinite recursion...You already have you code basically set up to do it...go through the code again and follow it line by line and you should spot how to fix it.
Topic archived. No new replies allowed.