Write a function which allows the user to enter inventory data for items in a store.

Here is the instructions for this project https://imgur.com/a/4kfry .
I am extremely confused on were to even start any help would be appreciated.
Last edited on
Hint: You'll need a main() function.
Hello MrMongoose,

As Ihatov said start with main. It took me a couple of times reading the requirements to find the reference to a menu. I find it is easier to start with the really simple things first like a menu. Starting with a menu will give you ideas of what needs to be done.

My initial thought is that a do/while will surround most of what is in main so that you can do parts multiple times before exiting the program. It is best to work on the program one small part at a time and build off of that.

The program requirements give you most of the variables that you will need. Your note at the bottom are good, but not complete. They are a good start on the variables and functions that you will need.

That should help get you started.

Hope that helps,

Andy
Thanks for the reply. Is there any way you could show me what the pseudo code would look like or even just an outline.
Hello MrMongoose,

I do not use pseudo code that often, but I will see what I can cme up with.

Andy
I am extremely confused on were to even start any help would be appreciated.

The assignment was due 2 days ago?
Last edited on
Hello MrMongoose,

Start

Define variables
	create struct

Open Output fille
	Ask for file name
	If blank usedefault file name
	Add extension if needed
	Check for errors opening file
	If problem offer redo or return to menu

Print any opening message

Call function for input
	pass or return struct
	check for -999.00
		abandon input
	validate cost
		prompt until correct
	Empty stock number
		Abandon input

Store to disk
	one record per line
		Could be done in the input function

Close file at the end of main

End program



May not be the best, but it should give you a start. Call it a personal preference, but I like to open files in main and pass the file handle to functions that need it. The requirements call for opening and closing the file where needed. You should do it this way.

When Inputing to the variables you wil need to prompt for using "-999.00". This should be done along with a prompr for "cost".

When dealing with files I like using this code. It is useful when new to using files nd in the future it could be shortened.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	std::string oFileName{ "" };  // <--- for your use use a variable here that is given a value before you reach here.

	std::ofstream outFile;

	outFile.open(oFileName, std::ios::trunc | std::ios::ate);  // <--- When finished testing loose "trunc" and change "ate" to "app" to always append the file when opened.

	if (outFile.is_open())
	{
		std::cout << "\n File " << oFileName << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << oFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);
	}


Hope that helps,

Andy
Last edited on
Okay so this is what I have and I know it is wrong but can any of you help
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
#include<stdlib.h>
#include<string>
#include<iostream>
#include<iomanip>
#include<fstream>
#include <cstring>


void openFile(ofstream& filename);
void getFileName (char fileName[]);
void getInputs(char stockNum[], char description[], float& cost, float& price, int& quantity);
void writeToFile(ofstream& fileName, char stockNum[], char description[], float cost, float price, int quantity);

int main()
{

char stockNum[6];
char description[21];
float cost;
float price;
int quantity;
ofstream fileName;

getFileName(char fileName[]);
openFile(ofstream& filename);


while (strlen(stockNum) != 0)
{
getInputs(stockNum, description, cost, price, quantity);

if (cost != -999)
{
writeToFile(fileName, stockNum, description, cost, price, quantity);
}
cin.ignore(999,'\n');

cout << stockNum << endl;
cin.getline(stockNum, 5, '\n');

}

    return 0;
}

//============================================================================================

void writeToFile(ofstream& fileName, char stockNum[], char description[], float cost, float price, int quantity)
{
fileName.open("stock.dat");
fileName << stockNum << " " << description << " " << cost << " " << price << " " << quantity << endl;
fileName.close();
exit(1);
}

//==============================================================================================

void getFileName(char fileName[])
{
cout<<"Name the file"<<endl;
cin.getline(fileName,101,'\n');
if(strlen(fileName)==0)
{
cout<<"No user input, stock.dat created"<<endl;
strcpy(fileName,"stock.dat");
}
else if(strstr(fileName,".dat"))
{
strcat(fileName,".dat");
cout<<fileName<<"was created.";
}
}
//=================================================================================================
void getInputs(char stockNum[], char description[], float& cost, float& price, int& quantity)
{
cout << "Enter stock Number";
cin.getline(stockNum, 6 , '\n');
cout << "Enter a description" << endl;
cin.getline(description, 20, '\n');

cout << "Enter cost between 0 and 10000.00(-999 to exit)" << endl;
cin >> cost;
while ((cost < 0 || cost > 10000) && cost != -999)
{
cout << "Enter cost between 0 and 10000 typing in -999 to exit" << endl;
cin >> cost;
cin.ignore(999,'\n');
}

if (cost != -999)
{
cout << "Enter the price:" << endl;
cin >> price;

cout << "How many in stock?" << endl;
cin >> quantity;
}
}

//=================================================================================================
void openFile(ofstream& fileName)
{
char fileNameStr[101];
cout << "File name?";
cin.getline(fileNameStr,101,'\n');
fileName.open(fileNameStr);
if(fileName.fail())
{
cout<<"Error Opening File"<<fileNameStr<<endl;
fileName.clear();
cout<<"File name?";
cin.getline(fileNameStr,100,'\n');
fileName.open(fileNameStr);
}
}
Hello MrMongoose,

From the top:

Line 1. "<stdlib.h>" may not be needed and if it is I would use "<cstdlib>".

Line 6. I would loose this in favor of "<string>". "string" is a much better choice over the C style char arrays that you are using.

I noticed that you did not use "using namespace std;". Good choice, but you will need to qualify what is in the standard namespace like: "std::cout", "std::cin", "std::endl", "std::ofstream", "std::string" and others. Now is as good of a time as any to learn what is in the standard namespace.

Line 9. When I compiled the program here there are three errors with this line. One had to do with "ofstream" which should be "std::ofstream". I believe the other has to do with "openFile" which may be part of "fstream" or one of the header files included by "fstream".

Lines 17 and 18 would be better defined as std::string stockNum; and std::string description;. Yes they do work as a C character array, but the std::string is easier to work with.

Line 22. first you need "std::" in front of "ofstream" and then "fileName" is very misleading. Bette choices are "outFile", "oFile", "of" or "OF" with the first being most descriptive and I personally feel it is the best choice. The opisite is "inFile". A good example is line 25. First the function call is all wrong. The function require two parameters and you are supplying only one. Second "ofstream&" is not needed in the function call only in the prototype and function definition. Only the variable names are used in a function call. Here in the function call the use of "fileName" is misleading and confusing.

After this it looks like most of the errors are simple only needing "std::" to correct most. After that when those errors are gone the more important errors will be easier to find and work on.

That should get you started. I find the "search and replace" a handy tool to correct most of the simple errors. Or for some practice you could go through the progrm typing "std::" where need. After a short time, maybe a week or so, this will become a habit you do not even think about you just do.

There is a start.

Hope tht helps,

Andy
Ewe, gross. Paperwork programming? :p
Registered users can post here. Sign in or register to post.