displaying certain data from the text file

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;
void input();
void displayall();
void update();
void deletedata();
void bye();
void searchdata();
int main()
{
system("title Inventory Database Milestone1");
cout << "Please enter choice" << endl;
cout << endl;
cout << "1 - Insert new record into the database" << endl;
cout << "2 - Display record of the database" << endl;
cout << "3 - Update existing record in the database" << endl;
cout << "4 - Delete existing record in the database" << endl;
cout << "5 - Search for a specific data" << endl;
cout << "6 - Exit " << endl;
cout << endl;

int choice;
cin >> choice;
cin.ignore();


switch(choice)
{
case 1:
input();
system("cls");
main();
break;

case 2:
displayall();
system("pause");
main();
break;

case 3:
update();
system("pause");
main();
break;

case 4:
deletedata();
system("cls");
main();
break;

case 5:
searchdata();
system("pause");
main();
break;

case 6:
system("cls");
bye();
break;
}

}


void input(){

string item_id;
string item_name;
string item_des;
string item_categ;
string item_manufac;
double item_sellpri;
double item_costpri;
string item_unitin;
string item_unitsold;
string item_yearofdateintro;
string item_monthofdateintro;
string item_dayofdateintro;

cout << "Please input Item ID" << endl;
cin >> item_id ;
cin.ignore();
cout << "Please input Item Name" << endl;
getline(cin,item_name);
cout << "Please input Item Description" << endl;
getline(cin,item_des) ;
cout << "Please input Item Category" << endl;
getline(cin,item_categ) ;
cout << "Please input Item Manufacturer" << endl;
getline(cin,item_manufac) ;
cout << "Please input Item Sellprice" << endl;
cin >> item_sellpri ;
cout << "Please input Item Costprice" << endl;
cin >> item_costpri ;
cout << "Please input Item Unit in Store" << endl;
cin >> item_unitin ;
cout << "Please input Item unit Sold" << endl;
cin >> item_unitsold;
cout << "Please input Year of Date First Introduced" << endl;
cin >> item_yearofdateintro;
cout << "Please input Month of date First Introduced" << endl;
cin >> item_monthofdateintro ;
cout << "Please input Day of Date First Introduced" << endl;
cin >> item_dayofdateintro ;

std::ofstream outfile ("Inventory Database.txt", fstream::app);
std::ofstream infile ("temp.txt", fstream::app);

outfile << "ID : " << item_id << endl;
outfile << "Name : " << item_name << endl;
outfile << "Description : " << item_des << endl;
outfile << "Category : " << item_categ << endl;
outfile << "Manufacturer : " << item_manufac << endl;
outfile << "Sell price : " << item_sellpri << endl;
outfile << "Cost price : " << item_costpri << endl;
outfile << "Unit In : " << item_unitin << endl;
outfile << "Unit Sold : " << item_unitsold << endl;
outfile << "Year : " << item_yearofdateintro << endl;
outfile << "Month : " << item_monthofdateintro << endl;
outfile << "Day : " << item_dayofdateintro << std::endl;
outfile << endl;

infile << "ID : " << item_id << endl;
infile << "Name : " << item_name << endl;
infile << "Description : " << item_des << endl;
infile << "Category : " << item_categ << endl;
infile << "Manufacturer : " << item_manufac << endl;
infile << "Sell price : " << item_sellpri << endl;
infile << "Cost price : " << item_costpri << endl;
infile << "Unit In : " << item_unitin << endl;
infile << "Unit Sold : " << item_unitsold << endl;
infile << "Year : " << item_yearofdateintro << endl;
infile << "Month : " << item_monthofdateintro << endl;
infile << "Day : " << item_dayofdateintro << std::endl;
infile << endl;

outfile.close();
infile.close();

}
void displayall()
{

string item;

ifstream outfile;

outfile.open("Inventory Database.txt");

while(getline(outfile,item))
{
cout << item << endl;
}

outfile.close();
}
void update()
{
string temp;
string st_txt1;
string st_txt2;
ofstream seek1;
ifstream temp1;
seek1.open("Inventory Database.txt");
temp1.open( "temp.txt");
cout<<"Enter the text that you want to edit\n";
getline(cin, st_txt1);
cout<<"Enter the text that you want to replace\n";
getline(cin, st_txt2);
while (getline(temp1, temp))
{
if (temp.find(st_txt1) != string::npos)
{
temp.replace(temp.find(st_txt1), st_txt1.length(), st_txt2);
cout << temp << endl;
}
seek1 << temp << endl;
}
cout<<endl<<"Edit success.\n";
}
void deletedata()
{
string temp;
string st_txt1;
string st_txt2;
ofstream seek1;
ifstream temp1;
seek1.open("Inventory Database.txt");
temp1.open( "temp.txt");
cout<<"Enter the text that you want to delete\n";
getline(cin, st_txt1);
while (getline(temp1, temp))
{
if (temp.find(st_txt1) != string::npos)
{
temp.replace(temp.find(st_txt1), st_txt1.length(), "-1");
cout << temp << endl;
}
seek1 << temp << endl;
}
cout<<endl<<"Edit success.\n";
}
void searchdata()
{
ifstream outfile("Inventory Database.txt");
string answer1;
string item_id;
string item_name;
string item_des;
string item_categ;
string item_manufac;
string item_sellpri;
string item_costpri;
string item_unitin;
string item_unitsold;
string item_yearofdateintro;
string item_monthofdateintro;
string item_dayofdateintro;


cout << "Which data would you like to see ?" << endl;
getline(cin,answer1);
cout << endl;

if (answer1 == "item id")
{
cout << "ID : " << item_id << endl;
}
else if (answer1 == "item name")
{
cout << "Item Name : " << item_name << endl;
}
else if (answer1 == "item description")
{
cout << "Item Description : " << item_des << endl;
}
else if (answer1 == "item category")
{
cout << "Item Category : " << item_categ << endl;
}
else if (answer1 == "item manufacturer")
{
cout << "Item Manufacturer : " << item_manufac << endl;
}
else if (answer1 == "item sell price|item sellprice")
{
cout << "Item Sellprice : " << item_sellpri << endl;
}
else if (answer1 == "item costprice|item cost price")
{
cout << "Item Costprice : " << item_costpri << endl;
}
else if (answer1 == "item unitin|item unit in")
{
cout << "Item UnitIn : " << item_unitin << endl;
}
else if (answer1 == "item unit sold|item unitsold")
{
cout << "Item unit sold : " << item_unitsold << endl;
}
else if (answer1 == "item year")
{
cout << "Item year of date introduced : " << item_yearofdateintro << endl;
}
else if (answer1 == "item month")
{
cout << "Item month of date introduced : " << item_monthofdateintro << endl;
}
else if (answer1 == "item day")
{
cout << "Item day of date introduced : " << item_dayofdateintro << endl;
}
else
cout << "No record found in database" << endl;
}
void bye()
{
cout << "THANK YOU FOR USING US" << endl;
cout << "FEEL FREE TO COME BACK " << endl;


So my searchdata function are supposed to be a simple if else statement to display data on which the user choose to see. But somehow the value did not show on the screen.

It only print

Item ID :

no value. pls help me
In your searchdata function, you create lots of empty strings. You never fill them, You never put any data in the strings. That's why the output is blank. Because you have empty strings.
else if (answer1 == "item unitin|item unit in")

Pretty sure this does not even remotely do what you want.
Even if I already input the value in the main function ? So how do I read the existing data from inside the text file ?
Even if I already input the value in the main function ?


If you create a variable in one function, and then create another variable with the same name in a different function, they're different variables. Putting some data into one of them doesn't do anything to the other.

So how do I read the existing data from inside the text file ?


1
2
std::ifstream infile ("someFile.txt");
infile >> item_id;


You need to learn about:

functions
passing parameters to functions
reading from files

You're not ready yet for what you're trying to do; you need to learn about variables and functions first, before you start writing them.
Last edited on
yeah I just started learning about the language for like a month since. I guess I still have a lot to do so btw thanks for helping and I'll be sure to learn more
Topic archived. No new replies allowed.