Hello adeel zamann,
I will start with your code is very hard to read. To that end it takes more time to straighten out your code and see what is happening.
So far I hae started with this:
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
|
#include <iostream>
#include <windows.h>
#undef min
#undef max
#include <limits>
#include <fstream>
#include <string> // <--- Added. Used for "std::string" and "std::getline". Also need for the insertation and extraction operators.
using namespace std;
constexpr DWORD SLEEP_TIME{ 2000 };
void SkipLine(std::istream &in)
{
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
int main()
{
Main:
system("cls");
cout << "Wellcome to the Mega Stor";
cout << "\nEnter 1 for add the thing\n\nEnter 2 for check or change in The price\n\n";
string selection; // <--- A "char" or "int" would be better.
cin >> selection;
if (selection == "1")
{
add:
system("cls");
cout << "\nWellcome to the Mega Stor";
cout << "\nAdd New Things";
ofstream things("data.txt", std::ios::app);
string name, id; // <--- Changed.
float price; // <--- Changed.
cout << "\nEnter Id: ";
cin >> id; // <--- "std::getline" may work better.
//SkipLine(); // <--- Call function before using "std::getline".
cout << "\nEnter Name: ";
cin >> name; // <--- Should use "std::getline" because the name may contain a space
cout << "\nEnter price: ";
cin >> price;
things << id << ',' << name << ',' << price << '\n';
//things << "Id Is " << id << '\n';
//things << "Name Is " << name << '\n';
//things << "Price Is " << price << '\n';
//things << '\n';
things.close();
system("cls");
cout << "\t" << id << " This Product is added in the data";
Sleep(SLEEP_TIME);
// Code removed for brevity.
}
else if (selection == "2")
{
// Code removed for brevity.
}
else
{
system("cls");
cout << "\nInvalid Selection";
Sleep(SLEEP_TIME);
goto Main;
}
}
|
The space between "include" and the "<" is not really needed, but it does make it easier to read.
When you include the header files "window.h" and "limits" you need to follow the "windows.h" with the 2 "#undef"s, so that "limits" can define "min" and "max" in the way it needs to. I have not found any better way to do this, mostly because I so not use "windows.h" that often. I usually use
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
In this case the (3) means 3 seconds and only whole numbers are used. Another option is
std::this_thread::sleep_for(std::chrono::milliseconds(3000)); // <--- Needs header files chrono" and "thread".
Where 1000 equals 1 second. This is also more portable than using "Sleep()".
A problem with defining "selection" as a string is that you could accidently or on purpose enter more than just 1 or 2 then your if statements would never be equal.
Line 54 would be a better way to write to the file which would make it much easier to read the file. Also writing to the file
"Id Is "
is more a waste of time and storage space for something that is not needed.
Line 71. Although there is no "else if" in C++ and there is nothing wrong with the way you did it. Most often line 71 is written this way. It works just the same and makes the code easier to read and follow.
Then if "selection" is not 1 or 2 you reach the "else" at line 75.
In the code not shown the "else if" statements should look like what I did.
A note: The "goto" statements should be replaced with either a "do/while" or "while" loop. The "goto" statement is useful in rare cases, but should not be part of normal programming.
While working I did have a thought. You could define a struct:
1 2 3 4 5 6
|
struct Product
{
std::string s_id;
std::string s_name;
float price{};
};
|
Read the file into the struct and store it in a vector or array. The vector would be better if you have studied that. This way it is easier to use the vector or array to deal with any changes and just write the vector or array to the input file overwriting what is there with the new information. This would be easier than trying to move the file pointer around to change the price, unless you have to read and write to 1 file.
Andy