More answer than you asked for, but here goes:
(1) using namespace std;
You should learn to write programs without the
using namespace std;
line. Your program does so few things using standard objects it would be ever so easy to just change every
cout
→
std::cout
, etc.
1 2 3 4 5 6 7
|
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
//using namespace std;
|
(2) Globals & constants
It is
my opinion that globals, constant or otherwise, should be listed first thing in your source code. In your case, however, the only objects you should have as globals are the input and output filenames. Everything else should be a local variable in
main()
.
9 10
|
const string FILE_INPUT = "inputInventory.txt";
const string FILE_OUTPUT = "outputInventory.txt";
|
19 20 21 22 23 24
|
int main()
{
const int MAX = 10;
string itemNames[MAX];
double itemCost[MAX];
int itemNoShip[MAX][2];
|
(3) Naming issues
You have three arrays:
•
itemNames
•
itemCost
(no ‘s’?)
•
itemNoShip
(???)
The first problem is semantic: you have an array of multiple
names, and a matching array of multiple
cost... er,
costs? Pick one or the other, but mixing and matching looks sloppy.
23 24 25
|
string itemNames[MAX];
double itemCosts[MAX];
int itemNoShips[MAX][2];
|
23 24 25
|
string itemName[MAX];
double itemCost[MAX];
int itemNoShip[MAX][2];
|
The second issue is clarity. I have no idea what a “NoShip” tuple is. If it is:
• number in inventory
• number shipped
then that is two separate things, and again we have a weird semantic problem. If an item is composed of:
• name
• cost
• inventory
• number shipped
...then why are the last two grouped while the first two are not also?
Either use a
strut
to manage a complete item:
12 13 14 15 16 17 18
|
struct Item
{
string name;
double cost;
int inventory;
int shipped;
};
|
19 20 21 22
|
int main()
{
const int MAX = 10;
Item items[MAX];
|
...or use four separate arrays:
19 20 21 22 23 24 25
|
int main()
{
const int MAX = 10;
string itemName[MAX];
double itemCost[MAX];
int itemNumber[MAX];
int itemShipped[MAX];
|
(4) Array size VS array used
this is the cause of the problem you are asking about
An array has a
size (which you have declared with
MAX
) but the problem is that you have a variable number of items. Your code, however, assumes that there are always
MAX
items. Given your two-item example, you then have 8 unused-but-still-displayed items, each named "". See why you get a bunch of zeros?
The way to fix it is to keep track of the number of items you read from file.
19 20 21 22 23
|
int main()
{
const int MAX = 10;
Item items[MAX];
int itemCount = 0; // start with zero items
|
Every time you successfully read an item from file you increment the item count. This leads into to the next problem:
(5) file reading issues
You have a function to read inventory, but you do not use it. You will need to provide a proper prototype and matching function below which modifies both your item array and your item count:
|
bool readInventory( Items items[], int & itemCount, int MAX );
|
You then need to call it properly
at the right time:
1 2 3 4
|
if (!readInventory( items, itemCount, MAX ))
{
std::cerr << "Failure to read inventory!\n";
}
|
At the moment you have code on lines 27–33 to read the file instead of using the
readInventory()
function. If you wish to start your program by reading the file automatically you should replace it with a call to
readInventory()
. Otherwise you should just get rid of it.
─────
The next trick is to properly implement a read loop. (
This is the other reason you are getting zeros.) However, you have made life extra difficult by allowing spaces in your item names AND use spaces to separate fields in your item record. Also, setting field widths when
writing the file does you more harm than good (adding an additional difficulty with reading it properly).
Is the file format up to you? That is, can you change it?
Answer that and I (or someone else) can give you more help with reading it properly. (Because as-is it is really kind of beyond your current skill level to read very easily.)
(6) The menu
Your current menu and switch statement do not match. For example, your printed menu says "1" means read the inventory file. But your switch statement instead displays the current items.
That is all to get you started. Please answer the question about your file format.
Oh, and by the way, it looks like Mr. Polanco has done nothing more than print a couple of strings while you are doing all the work (
logo()
and
menu()
). You should try to even things out a bit if you want to be graded fairly.