Hello frustratedSEstudent,
Upon Testing your program this is what I found.
#include<cstdlib> I do not believe this header file is needed. Although I could be wrong. I did not use it and the program ran.
These global variables are not needed. They can just as easily be local variables in the function "operation" and in main.
1 2 3 4 5 6 7 8
|
struct products
{
char name[1];
char description;
char ID;
int quantity;
double price;
};
|
For the struct line 3 is a two element array with element zero holding one character and element one holding a "\0". I do not think that is what you want. Line 4 will only hold one character and the same with line 5. A better approach would be:
1 2 3 4 5 6 7 8 9
|
struct products
{
string name;
string description;
string ID;
int quantity;
double price;
};
|
The function "int operation()" returns an "int", but it is never used. More on that later. The variable "x" is better defined in the function since it is being returned to main before the function ends.
In main you call the function "operation" which returns an "int", but you never use it. "x" can be defined in main and your function call would look like this:
x = operation();
. Now you can use "x" in your switch and it will work without the need for your global variable. Since the scope of the function "operation" and main are different "x" can be defined in each function without any conflict.
To eliminate the need for the goto statements I wrapped most of main in a do while loop. The idea is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
bool cont{true};
do
{
// Code here
} // End of switch
if (cont)
{
std::cout << "\n Do another: ";
std::cin >> x;
}
} while (cont);
|
The if statement is if the main menu choice was not to exit the program. I added choice 5 to the menu and case 5 to the switch to end the do/while loop and end the program. This should work, but I have not tested it yet because of other problems that need addressed first. I would also suggest a "default" case to catch any menu choice that is not correct. Although it would be better to catch that in the function "operation" before you return to main. Easier to check it in "operation" than in main.
In the switch I could only start with case 1 for now. The for loop starts out as:
int i = (1 + c);
. That works, but you are only using 99 out of 100 in your array. It would work better as:
int i = (0 + c);
. Later when "c" has a value > zero it would be useful to add to the array where you left off. And first time through you will start with element zero and use all 100 elements of the array.
In the "else" statements the lines that input "name" and "description" would work better with std::getline instead of the cin. cin will read until the first space or \n whichever comes first. And you will want to use this
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
after a std::cin >> and before a std::getline() to clear the "\n" left by the std::cin. This line of code needs the header file "limits" to work.
Other than adding a case 5 to the switch and the above mentioned if after the closing brace of the switch I have not looked at the rest if the program yet.
Hope that helps,
Andy
Edit: Your repeating loop comes from the for in case 1. There is no way out of it until "i" reaches 100. Sorry meant to mention that earlier.