Hey, everyone, I am another student seeking help for a programming class. What I am having trouble with specifically is reading from a file.
The program itself involves entering in some store products with information such as: product name, product ID, etc. After everything is entered, the program is supposed to redisplay all of the information stored if required to do so.
My teacher taught us about the basics of using the (file).open and (file).read functions to read and write to and from notepad files, but I am having trouble reading more than one line from the file. I have saved at least 2 lines of data into the notepad file, but when displaying, I can only display one of the lines and have no idea how to display the other one. I have divided my code into 2 functions in addition to the main one, with the display code being in display_product().
213-227 -> This is where I write the data into the file using binary since my teacher recommended it.
241-313 -> This is where I try to read from the file back into the program, but have no idea how to read parts of it.
-- Also, sorry for my horrible coding and labelling, I am really bad at programming and still trying to learn.
______________________________________________________
// Mysterious Man
// April 2nd, 2008
// Menu System 2
#include "Main(1).h"
void main()
{
init_graphics();
int choice;
char waste[50];
set_color (cBLUE,cBLACK);
top:
clear_screen();
set_color (cMAGENTA,cBLACK);
cout<< "\t\t\t Grocery Products Database\t\t\t\t";
cout.flush();
cout<< "\n--------------------------------------------------------------------------------";
cout.flush();
set_color (cBLUE,cBLACK);
cout<< "\n\t\tAdd a New Product\t\t(1)";
cout.flush();
cout<< "\n\t\tList Products\t\t\t(2)";
cout.flush();
cout<< "\n\t\tUpdate Products\t\t\t(3)";
cout.flush();
cout<< "\n\t\tSearch Products\t\t\t(4)";
cout.flush();
cout<< "\n\t\tQuit\t\t\t\t(5)";
cout.flush();
set_color (cRED, cBLACK);
cout<< "\n\n\tMake your choice: ";
cout.flush();
set_color (cBLUE, cBLACK);
choice= getche();
switch (choice)
{
case '1':
add_product();
break;
case '2':
display_product();
break;
case '3':
break;
case '4':
break;
case '5':
cout<< "\n\nThankyou for using the program...\n\n";
cout.flush();
return;
break;
default:
cout<< "\n\nPlease enter a valid number...\n";
cout.flush();
wait (2000);
break;
}
goto top;
}
void add_product()
{
char waste[50];
// Add a new product to our file
fstream outfile;
char yesno;
product prod;
clear_screen();
set_color ((color)9,(color)0);
set_cursor_pos (34,1);
cout<< "Add New Product";
cout.flush();
set_color ((color)15,(color)0);
set_cursor_pos(10,3);
cout<< "Please enter sales person's first name: ";
cout.flush();
cin.width(20); // Maximum 20 characters allowed
cin>> prod.fname;
cin.ignore(50, '\n');
// Properly format first name
_strlwr (prod.fname);
if (prod.fname[0]> 96 && prod.fname[0]< 123)
{
prod.fname[0]-=32;
}
if (cin.gcount() < 20) cin.ignore (20< '\n');
set_cursor_pos(10,4);
cout<< "Please enter sales person's last name: ";
cout.flush();
cin.width(20); // Maximum 20 characters allowed
cin>> prod.lname;
cin.ignore(50, '\n');
// Properly format last name
_strlwr (prod.lname);
if (prod.lname[0]> 96 && prod.lname[0]< 123)
{
prod.lname[0]-=32;
}
if (cin.gcount() <30) cin.ignore (30< '\n');
// Entering the product name which has a max of 50 characters
set_cursor_pos (10,5);
cout<< "Please enter the product name: ";
cin.getline (prod.prodname, 50);
if (cin.gcount() <50) cin.ignore (50< '\n');
set_cursor_pos (10,6);
cout<< "Please enter the product ID number: ";
cin.width (7);
cin>> prod.prodnum;
cin.ignore (50< 'n');
// Check to see if all ID numbers are digits
bool ok;
do
{
ok= false; // false means no error
for (int i= 0; i<6; i++)
{
if (prod.prodnum[i]< 48 || prod.prodnum[i]> 57)
{
ok= true; // not a number
}
if (ok=true)
{
set_cursor_pos (10,8);
cout<< "Please enter the product ID number: ";
cin.width (7);
cin>> prod.prodnum;
cin.ignore (50, '\n');
}
}
} while (ok=true);
// Enter Regular and Sales Price into header
set_cursor_pos (10,10);
cout<< "Enter regular price of product "<< prod.prodname<< ": ";
cin.width (7);
cin>> prod.price.regprice;
cin.ignore (50, '\n');
set_cursor_pos (10,11);
cout<< "Enter sales price of product "<< prod.prodname<< ": ";
cin.width (7);
cin>> prod.price.saleprice;
cin.ignore (50, '\n');
// If the reg price is 0, set discount to 0
if (prod.price.regprice<= 0 || prod.price.regprice== prod.price.saleprice)
{
prod.price.discount= 0;
}
else
{
prod.price.discount= 100-(((prod.price.saleprice)/(prod.price.regprice))* 100);
}
// Review the data for input
clear_screen();
set_cursor_pos (34,1);
cout<< "Add a New Product";
cout.flush();
prod.active= 1; // this is an active record
// Forcing the money values to be converted to two decimal places & displaying
cout.setf (ios::fixed, ios::floatfield);
cout.precision (2);
set_cursor_pos (8,3);
cout<< "Save the following information?\n";
cout.flush();
cout<< "\nFirst Name:\t\t\t"<< prod.fname<< "\nLast Name:\t\t\t"<< prod.lname;
cout.flush();
cout<< "\nProduct Name:\t\t\t"<< prod.prodname<< "\nProduct ID:\t\t\t"<< prod.prodnum;
cout.flush();
cout<< "\nRegular Product Price:\t\t$ "<< prod.price.regprice<< "\nProduct Sales Price:\t\t$ " << prod.price.saleprice;
cout.flush();
cout<< "\nProduct Discount:\t\t"<< prod.price.discount<< "%";
cout<< "\n\nChoose yes or no (y/n)? ";
yesno= cin.get();
cin.ignore (50, '\n');
if (yesno== 'y' || yesno== 'Y')
{
outfile.open ("Product_Database.txt", ios::out | ios::app | ios::binary);
if (!outfile)
{
cout<< "*** File Error Lol ***"<< endl; // endl = endline = "\n"
wait (1000);
return;
}
// Write to file - In Binary
outfile.write ((char *) &prod, sizeof (prod));
cout<< "\nData Saved...\n";
wait (1000);
outfile.close();
}
else if (yesno!= 'Y' || yesno!= 'y')
{
cout<< "\nReturning to Main Menu...";
wait (2000);
return;
}
waste[49]= yesno;
return;
}
void display_product()
{
fstream infile;
product prod;
int count;
clear_screen();
set_cursor_pos (31,2);
cout<< "Displaying Products";
cout.flush();
// ------------------------- casting operator
infile.open ("Product_Database.txt", ios::in | ios::binary);
if (!infile.is_open())
{
cout<< "The File Does Not Exist!";
cout.flush();
wait (2000);
return;
}
set_cursor_pos (1,4);
cout<< " -------------------------------------------------------------------";
cout.flush();
set_cursor_pos (1,6);
cout<< " -------------------------------------------------------------------";
cout.flush();
set_cursor_pos (1,5);
cout<< " |First Name|";
cout.flush();
set_cursor_pos (18,5);
cout<< " |Last Name|";
cout.flush();
set_cursor_pos (36,5);
cout<< " |Product Name|";
cout.flush();
set_cursor_pos (56,5);
cout<< " |Product ID|";
cout.flush();
count= 0;
// -------------- If using binary, must use:: infile.read((char *) &prod, sizeof (prod))
do
{
infile.read ((char *) &prod, sizeof (prod));
prod.count++;
set_cursor_pos (4,8+count);
cout<< "1. "<< prod.fname;
cout.flush();
set_cursor_pos (22,8+count);
cout<< prod.lname;
cout.flush();
set_cursor_pos (40,8+count);
cout<< prod.prodname;
cout.flush();
set_cursor_pos (60,8+count);
cout<< prod.prodnum;
cout.flush();
} while (!infile.eof());
// --------------
infile.close();
cout<< "\n";
wait (5000);
}
________________________________________________________________
HEADER
// Grocery Sales List HEADER FILE
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdio.h>
#include "msoftcon.cpp"
// Create the grocery file structure
struct sales
{
double regprice;
double saleprice;
double discount;
};
struct product
{
int active; // Is this record active= 1, inactive= -1
char prodname[50]; // The product name
char fname[20]; // The sales person's first name
char lname[30]; // The sales person's last name
char prodnum[7]; // Product identification number
sales price; // Lolol from above structure!
int count; // The counter for displaying
char yesnolol; // Press any key to continue after displaying
};
void add_product();
void display_product();