My program basically ask user for inputs and it saves data like login data, username in a text file and uses same file to retrieve data and displays in the output console. When program executes users can choose between options 1 to 6, option 6 is for existing out of the application and rest of the options from 1 to 5 are for users to input data and to view stored data in the file.
When program displays data in the console, I see lots of unnecessary ASCii code. Why is it showing up and how do I make them go ? Thanks!!
int main() {
//Considering the max length of data entered (name) to be 15.
char data[15];
int n = 0, option = 0, count_n = 0;
//This is the initial mark alloted to a subject.
string empty = "00";
string proctor = "";
//Name of the file in which DB is stored.
ifstream f("Example.txt");
string line;
//The following for loop counts the total number of lines in the file.
for (int i = 0; std::getline(f, line); ++i) {
count_n++;
}
while (option != 6) {
//This prints out all the available options in the DB
cout << "\nAvailable operations: \n1. Add New Students\n2."
<< "Student Login\n3. Faculty Login\n4. Proctor Login\n5. Admin View\n"
<< "6. Exit\nEnter option: ";
cin >> option;
if (option == 1) {
cout << "Enter the number of students: ";
cin >> n;
count_n = count_n + n;
for (int i = 0; i < n; i++) {
ofstream outfile;
outfile.open("Example.txt", ios::app);
//The entire data of a single student is stored line-by-line.
cout << "Enter your registration number: ";
cin >> data;
outfile << data << "\t";
cout << "Enter your name: ";
cin >> data;
int len = strlen(data);
while (len < 15) {
data[len] = ' ';
len = len + 1;
}
outfile << data << "\t";
//Inserting empty data initially into the file
outfile << empty << "\t";
outfile << empty << "\t";
cout << "\b \b";
cout << "Enter your proctor ID: ";
cin >> proctor;
outfile << proctor << endl;
}
}
else if (option == 2) {
char regno[9];
cout << "Enter your registration number: ";
cin >> regno;
ifstream infile;
int check = 0;
infile.open("Example.txt", ios::in);
//This loop prints out the data according to the registration number specified.
while (infile >> data) {
if (strcmp(data, regno) == 0) {
cout << "\nRegistration Number: " << data << endl;
infile >> data;
cout << "Name: " << data << endl;
infile >> data;
cout << "CSE1001 mark: " << data << endl;
infile >> data;
cout << "CSE1002 mark: " << data << endl;
infile >> data;
cout << "Proctor ID: " << data << endl;
infile.close();
check = 1;
}
}
if (check == 0) {
cout << "No such registration number found!" << endl;
}
}
//This loop is used to view and add marks to the database of a student.
else if (option == 3) {
char subcode[7];
cout << "Enter your subject code: ";
cin >> subcode;
string code1 = "CSE1001", code2 = "CSE1002", mark = "";
ifstream infile;
int check = 0;
cout << "\nAvailable operations: \n1. Add data about marks\n"
<< "2. View data\nEnter option: ";
cin >> option;
if (option == 1) {
cout << "Warning! You would need to add mark"
<< "details for all the students!" << endl;
for (int i = 0; i < count_n; i++) {
fstream file("Example.txt");
//The seek in file has been done according to the length
//of the data being inserted. It needs to adjusted accordingly
//for diffferent lengths of data.
if (strcmp(subcode, code1.c_str()) == 0) {
file.seekp(26 + 37 * i, std::ios_base::beg);
cout << "Enter the mark of student#" << (i + 1) << " : ";
cin >> mark;
file.write(mark.c_str(), 2);
}
if (strcmp(subcode, code2.c_str()) == 0) {
file.seekp(29 + 37 * i, std::ios_base::beg);
cout << "Enter the mark of student#" << (i + 1) << " : ";
cin >> mark;
file.write(mark.c_str(), 2);
}
}
}
//This loop is used to view marks of a student.
//The extra infile commands have been used to get a specific mark
//only since the data has been seperated by a tabspace.
else if (option == 2) {
infile.open("Example.txt", ios::in);
if (strcmp(subcode, code1.c_str()) == 0) {
cout << "Registration number - Marks\n" << endl;
while (infile >> data) {
cout << data;
infile >> data;
infile >> data;
cout << " - " << data << endl;
infile >> data;
infile >> data;
check = 1;
}
}
infile.close();
infile.open("Example.txt", ios::in);
if (strcmp(subcode, code2.c_str()) == 0) {
cout << "Registration number - Marks\n" << endl;
while (infile >> data) {
cout << data;
infile >> data;
infile >> data;
infile >> data;
cout << " - " << data << endl;
infile >> data;
check = 1;
}
}
}
infile.close();
if (check == 0) {
cout << "No such subject code found!" << endl;
}
}
//This loop displays all the details of students under the same proctor ID.
else if (option == 4) {
char procid[7];
cout << "Enter your proctor ID: ";
cin >> procid;
int check = 1;
char temp1[100], temp2[100], temp3[100];
char temp4[100], id[100];
ifstream infile;
infile.open("Example.txt", ios::in);
while (infile >> temp1) {
infile >> temp2;
infile >> temp3;
infile >> temp4;
infile >> id;
if (strcmp(id, procid) == 0) {
cout << "\nRegistration Number: " << temp1 << endl;
cout << "Name: " << temp2 << endl;
cout << "CSE1001 Mark: " << temp3 << endl;
cout << "CSE1002 Mark: " << temp4 << endl;
check = 1;
}
}
if (check == 0) {
cout << "No such proctor ID found!" << endl;
}
}
//This loop acts as an admin view to see all the data in the file.
else if (option == 5) {
char password[25];
cout << "Enter the admin password: ";
cin >> password;
//This variable value can be changed according to your requirement
//of the administrator password.
string admin_pass = "admin";
if (strcmp(password, admin_pass.c_str()) == 0) {
cout << "Reg No. \tName\tCSE1001\tCSE1002\tProctor ID" << endl;
ifstream infile;
infile.open("Example.txt", ios::in);
char data[20];
while (infile >> data) {
cout << data << "\t";
infile >> data;
cout << data << "\t";
infile >> data;
cout << data << "\t";
infile >> data;
cout << data << "\t";
infile >> data;
cout << data << endl;
}
}
}
}
}
Here is the output:
The ╠ character is a box drawing character. In code page 437 it is represented by hex value 0xCC.
In this case it is a magic hex value that means it is uninitialised stack memory.
The data array is 20 characters long, but it degrades into a pointer and the operator<< outputs characters until it gets a null character. So in this case lots of uninitialized memory ╠ characters until a \0 character is found in memory.
It seems that the uninitialized data variable was written to the file on a previous run and now it is read back as-is. If the read was unsuccessful it would probably have placed a \0 in the first position of the data char array and there would have been no output in that case.
I ran the program with an empty input file and added a single student. This was stored in the text file (viewed with Visual Studio's text editor):
REGNUM123 Pete ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ 00 00 PROC432
In this case Ì, or latin capital letter i with grave character is the 0xCC hex value in Unicode and multiple other encodings. When adding the student the number of characters written does not necessarily equal the number of characters in the data array (15 in that case). They are written until a \0 is found.
Most probably you have multiple buffer overruns:
char password[25];
cout << "Enter the admin password: ";
cin >> password;
If someone inputs more than 24 bytes (characters) as a password, you will have buffer overrun of password. You should do it this way:
std::string password;
cout << "Enter the admin password: ";
cin >> password;
The same thing is valid for output, you're potentially buffer overrunning char data[20];. Furthermore, you can't use infile >> data like that for reading from file. You have to use infile.getline():
int col_count = 4;
while (true) {
infile.getline(data, 20); // 20 is the size of the buffer.
if(infile.good()) {
cout << data;
if(--col_count == 0) {
col_count = 4;
cout << '\n';
}
else cout << '\t';
}
else break;
}
Related
I have to code, build, and execute a program that requires sequential files to create an address database. The program works but When I re-run the program the old data is still in the database when I chose to display records. How do I get the program to erase old data and start fresh when I start the program from the beginning. Can you please help identify my errors. Thank you in advance.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);
const char FileName[] = "TestAddress.txt"; //File name from where to read the characters
int main()
{
menu();
return 0;
}
void menu(void)
{
//Display main menu and call relevant functions
char input;
while (1)
{
//allow user to choose to append records, display records or exit the program
cout << "Select program\n(A)ppend Records, (D)isplay Records, (E)xit: ";
cin >> input;
cout << endl;
if (input == 'A' || input == 'a')
writeData();//Append data to the file
else if (input == 'D' || input == 'd')
readData(); //Read records from the file and display
else if (input == 'E' || input == 'e')
exit(0);// Exit application
else
cout << endl << "Invalid Input!!! Please enter A, D or E:" << endl; //In case of invalid input, menu will be displayed again
}
}//end menu
void writeData(void) {//Append data to the file
string fullName, Street, City, State, ZipCode, input;
fstream outputFile;
outputFile.open(FileName, fstream::app); //Open the file with append mode
do {
cout << endl;
cout << "Please enter Name: ";
cin.ignore();
getline(cin, fullName); //Input Name
cout << "Please enter Street: ";
cin.ignore();
getline(cin, Street); //Input Street Address
cout << "Please enter City: ";
cin >> City; //Input City
cout << "Please enter State: ";
cin >> State;// Input State
cout << "Please enter ZipCode: ";
cin >> ZipCode; //Input Zipcode
cout << endl;
outputFile << fullName << "," << Street << "," << City << "," << State << "," << ZipCode << endl; //Write all data to the file
do
{
cout << "Enter Another Record?(Y/N): ";
cin >> input;//In case if user wants to enter another rcord
}
while (input != "Y"&& input != "y" && input != "N"&& input != "n"); //Until the user presses 'y' or 'n'
} while (input == "Y" || input == "y"); //If user wants to add another record continue or else display menu again
outputFile.close(); //Close file
}//end write data
void readData(void) {//Read records from the file and display
string Name, Street, City, State, ZipCode;
char* line = new char[2000]; //pointing to a vriable that will be having a record detail
ifstream inputFile;
int RecordNumber = 1;
inputFile.open(FileName, fstream::in);//Open the file with read mode
while (!inputFile.eof())
{
inputFile.getline(line, 2000); //Read one record from the file
string * fields = split(line, ',');//Split the records based on the prsence of ','
if (fields[0] == "") //In case of '\n' has encountered
continue; //Stop processing and continue the loop again
cout << "" << endl;
cout << "Record #" << RecordNumber << endl; //Displaying record with style
cout << "Name.........." << fields[0] << endl;
cout << "Street........" << fields[1] << endl;
cout << "City.........." << fields[2] << endl;
cout << "State........." << fields[3] << endl;
cout << "Zip Code......" << fields[4] << endl;
cout << endl << "-------------------------------------------------------" << endl << endl;
RecordNumber++; //Increment record number
}
}//end read data
string * split(string theLine, char theDeliminator) {
//determine how many splits there will be so we can size our array
int splitCount = 0;
for (int i = 0; i < theLine.size(); i++)
{
//Read the whole string (theLine) and count for all ',' encountered
if (theLine[i] == theDeliminator)
splitCount++;
}
splitCount++; //add one more to the count because there is not an ending comma
//create an array to hold the fields
string* theFieldArray;
theFieldArray = new string[splitCount];
//split the string into seperate fields
string theField = "";
int commaCount = 0;
for (int i = 0; i < theLine.size(); i++)
{
//read each character and look for the deliminator
if (theLine[i] != theDeliminator) {
theField += theLine[i]; //build the field
}
else { //the deliminator was hit so save to the field to the array
theFieldArray[commaCount] = theField; //save the field to the array
theField = "";
commaCount++;
}
}
theFieldArray[commaCount] = theField; //the last field is not marked with a comma...
return theFieldArray;
} //end split
Hello I am having issues with a final project. The objective is to have the user create a form that allows a user to view data, edit data, add data, and save their password data in an encoded format. The program starts with an input file made by the user. The delimiter is ';', and the first char is a "code", then the, site, username, password, and notes follow.
I am very new to vectors, and I am not allowed to use a 2d array, or map.
Thank you for your time.
#include <iomanip>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
const int MAX_SIZE = 30;
const int BUFFER_SIZE = 200;
struct dataRow {
string site, user, pass, notes;
string code;
};
string inFileName; //name of inputfile
string outFileName;
ifstream pathStream;
ofstream outFile;
char inputChar;
int extPosition(1), count = 0,lineNum;
void displayVector(vector<dataRow> inputData);
void viewLineData(vector<dataRow> inputData, int lineNum);
void addNewRow(vector<dataRow> inputData,int& lastRowNum, string site, string user, string pass, string notes);
void editLineData(vector<dataRow> inputData, int& lineNum, string site, string user, string pass, string notes);
void warning();
void cleanBuffer();
bool displayMenu();
int exit();
dataRow descripLine;
int main()
{
vector<dataRow> inputData;
string rowDescripin,userin,passin,notesin,sitein;
string codein,errorMsg;
int lastRow;
//add a .txt extension to the file if the user didn't provide an extension
if (inFileName.size() > 4)
// If there's a valid extension, it will be in the last 4 positions of the string; Adjust by 1 for 0 offset
extPosition = inFileName.size() - 4;
int ext = inFileName.find_last_of(".txt");
if (!(inFileName.find_last_of(".") == extPosition))
{
inFileName += ".txt";
}
cout << "\nPlease enter the filename of input file: ";
cin >> inFileName;
pathStream.open(inFileName.c_str());
if (pathStream.fail())
{
cerr << inFileName << " failed to open.\n";
system("pause");
exit(1);
}
else
{
cout << "startup success" << endl;
}
getline(pathStream, codein, ';');
descripLine.code = codein;
while (!pathStream.eof())
{
getline(pathStream, sitein, ';');
descripLine.site = sitein;
getline(pathStream, userin, ';');
descripLine.user = userin;
getline(pathStream, passin, ';');
descripLine.pass = passin;
getline(pathStream, notesin, ';');
descripLine.notes = notesin;
inputData.push_back(descripLine);
}
displayVector(inputData);
displayMenu();
while (cin){
cin >> inputChar;
inputChar = toupper(inputChar);
//Adjust calculations based on inputCHar
if (inputChar == 'D') // Display line descriptions
{
displayVector(inputData);
}
else if (inputChar == 'V') //View line data
{
cout << "Enter line number you wish to view: ";
cin >> lineNum;
viewLineData(inputData, lineNum);
}
/* else if (inputChar == 'E') //Edit line Data
{
cout << "Enter line number you wish to edit: ";
cin >> lineNum;
editLineData(inputData, lineNum);
}*/
else if (inputChar == 'A') //Add line data
{
warning();
lastRow = inputData.size();
cout << "Enter a line description: ";
cin >> rowDescripin;
cout << "Enter a line username: ";
cin >> userin;
cout << "Enter a line password: ";
cin>>passin;
cout << "Enter notes: ";
cin>>notesin;
addNewRow(inputData, lastRow, rowDescripin, userin, passin, notesin);
}
/*else if (inputChar == 'S') //Save and encode file
{
}*/
else if (inputChar == 'X') //exit program
{
exit();
}
}
system("pause");
}
bool displayMenu()
{
cout << endl << " AVAILABLE OPTIONS " << endl << endl <<
"D - DISPLAY LINE DESCRIPTIONS" << endl <<
"V - VIEW LINE DATA" << endl <<
"E - EDIT LINE DATA" << endl <<
"A - ADD LINE DATA" << endl <<
"S - SAVE AND ENCODE FILE" << endl <<
"X - EXIT PROGRAM" << endl;
return 0;
}
void viewLineData(vector<dataRow> inputData,int lineNum)
{
cout << inputData[lineNum].site << endl << inputData[lineNum].user<<endl<<inputData[lineNum].pass <<endl<<inputData[lineNum].notes;
}
void displayVector(vector<dataRow> inputData)
{
cout << fixed << setprecision(3);
for (unsigned int i = 0; i < inputData.size(); i++)
{
cout << left << setw(20) << inputData[i].site ;
}
}
void addNewRow(vector<dataRow> inputData, int& lastRowNum, string site, string user, string pass, string notes)
{
char ans;
cout << "You have entered:" << endl << site << endl << user << endl << pass << endl << notes<<endl;
cout << "Is this the data you wish to add (Y/N)? ";
cin >> ans;
ans = toupper(ans);
if (ans == 'Y')
{
dataRow tempRow = { site, user, pass, notes };
inputData.push_back(tempRow);
cout << inputData.size();
int num = inputData.size()-1;
for (unsigned int i = 0; i < inputData.size(); i++)
{
cout << inputData[i].site << endl << inputData[i].user << endl << inputData[i].pass << endl << inputData[i].notes;
}
}
else if (ans == 'N')
{
cout << "ok enter of no";
}
}
void warning()
{
cout<< "WARNING: You cannot use semi-colons in these fields. Any semi-colons entered will be removed." << endl;
return;
}
int exit()
{
pathStream.close();
system("pause");
return 0;
}
void cleanBuffer()
{
cin.clear();
cin.ignore(BUFFER_SIZE, '\n');
}
The purpose of the following code is to take user input of 6 movie titles and realease dates, write that data to a file using fstream, then read the data into 2 string characters (line1 and line2) in a loop, such that the first loop will assign line1 and line2 the first movie title and year, and the second loop will assign line1 and 2 the 2nd movie title and year, and so on, until eof.
// array of structures
#include <iostream>
#include <string>
#include <sstream>
#include <istream>
#include <stdio.h>
#include <cctype>
#include <fstream>
#include <cassert>
#include <assert.h>
using namespace std;
#define NUM_MOVIES 6
struct movies_iit
{
string title;
int year;
}
films[NUM_MOVIES];
// global variables
char title[20], y, n;
int year;
string search;
// function 1
void sort_on_title(movies_iit films[], int n)
{
// Local struct variable used to swap records
movies_iit temp;
for (int i = 0; i < n - 1; i++)
{
for (int i = 0; i < n - 1; i++)
{
/* If s[i].title is later in alphabet than
s[i+1].title, swap the two records */
if (films[i].title > films[i + 1].title)
{
temp = films[i];
films[i] = films[i + 1];
films[i + 1] = temp;
}
}
}
}
//end function 1
//function query1 prototype
void query1(movies_iit movie);
// function query2 prototype
void query2(movies_iit movie);
// function 2 prototype
void printmovie(movies_iit movie);
int main()
{
// login
// username: user
// password: word
string mystr, pass, name, line1, line2;
int n;
char response;
// output object
ofstream fout("data.dat");
// input object
ifstream fin("data.dat");
assert(fin.is_open());
cout << "enter your username " << endl;
cin >> name;
cout << "enter your password " << endl;
cin >> pass;
cout << "\n" << endl;
if (name == "user" && pass == "word")
cout << "Welcome, user." << endl;
else
{
cout << "###" << "unrecognized username/password combination" << "\t" << "please try again" << "###" << endl;
system("PAUSE");
return 0;
}
cin.ignore(std::numeric_limits < std::streamsize > ::max(), '\n');
cout << "\n" << endl;
for (n = 0; n < NUM_MOVIES; n++)
{
cout << "Enter title: ";
getline(cin, films[n].title);
cout << "Enter year: ";
getline(cin, mystr);
stringstream(mystr) >> films[n].year;
}
cout << "\n" << endl;
for (int i = 0; i < NUM_MOVIES; ++i)
{
fout << films[i].title << "\n";
fout << films[i].year << "\n";
}
// sort records, function 1 call
sort_on_title(films, NUM_MOVIES);
cout << "\nYou have entered these movies:\n";
for (n = 0; n < NUM_MOVIES; n++)
printmovie(films[n]); // function 2 call
cout << "Perform an alphabetical search? (y/n)" << endl;
cin >> response;
if (response == 'y')
{
cout << "Please enter title" << endl;
cin >> title;
if (fin)
{
getline(fin, line1); // read first 2 recs
getline(fin, line2);
while (fin) // keep reading till the eof
{
if (line1 == "title")
{
cout << " >> " << line1 << endl;
cout << line2 << endl;
}
else
{
cout << line1 << endl;
cout << line2 << endl;
}
}
}
fin.close(); //close input file
response == n;
}
else if (response == 'n')
cout << "\n" << endl;
else
cout << "invalid entry" << endl;
cout << "\n" << endl;
}
// function 2 definition
void printmovie(movies_iit movie)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
// function query1 defintion
void query1(movies_iit movie)
{
if (movie.title == "title")
{
cout << " >> " << movie.title;
cout << " (" << movie.year << ")\n";
}
else
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
}
// function query2 definition
void query2(movies_iit movie)
{
if (movie.year >= year)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
}
In the loop where I read values from the file into my strings, in my output it does not display the data stored in the strings. Why is that, and how can I fix the issue?
I realized that the code I originally posted didn't work; here is a functional one.
You need to close the 'fout' filestream before you try to read from it using 'fin'.
fout.close(); // <== Close the output file to flush the buffered I/O
The data you are writing to the file is likely buffered (not written to the file immediately). Your data.dat file is empty when you try to read it during your search.
Close the file in which data is being stored.
You are matching for string "title" and not input which user has provided.
getline() should be inside while loop ( as you want to match all the lines and not just 1st 2 records)
int main (){
//login
//username: user
//password: word
string mystr, pass, name, line1, line2;
int n;
char response;
//output object
ofstream fout ("data.dat");
//input object
cout << "enter your username "<<endl;
cin >> name;
cout << "enter your password "<<endl;
cin >> pass;
cout << "\n" << endl;
if (name == "user" && pass == "word")
cout << "Welcome, user." << endl;
else
{cout << "###" << "unrecognized username/password combination" << "\t" << "please try again" << "###" << endl;
//system("PAUSE");
return 0;
}
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "\n" << endl;
for (n=0; n<NUM_MOVIES; n++)
{
cout << "Enter title: ";
getline (cin,films[n].title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> films[n].year;
}
cout << "\n" << endl;
//###################################################################################################################
//write to file
//###################################################################################################################
for (int i = 0; i < NUM_MOVIES; ++i)
{
fout << films[i].title << "\n";
fout << films[i].year << "\n";
}
fout.close();
//sort records, function 1 call
sort_on_title(films, NUM_MOVIES);
cout << "\nYou have entered these movies:\n";
for (n=0; n<NUM_MOVIES; n++)
printmovie (films[n]); //function 2 call
ifstream fin("data.dat");
assert(fin.is_open());
//###################################################################################################################
//query 1
//###################################################################################################################
cout << "Perform an alphabetical search? (y/n)" << endl;
cin >> response;
if (response == 'y')
{cout << "Please enter title" << endl;
cin >> title;
if (fin.good())
{
while(!fin.eof()) //keep reading till the eof
{
getline(fin,line1); // read first 2 recs
if(!fin.eof())
getline(fin,line2);
if (line1 == title)
{
cout << " >> " << line1 << endl;
cout << line2 << endl;
}
// else
// {
// cout << line1 << endl;
// cout << line2 << endl;
// }
}
}
fin.close(); //close input file
response == n;
}
else if (response == 'n')
cout << "\n" << endl;
else
cout << "invalid entry" << endl;
cout << "\n" << endl;
}
thanks for your anticipated help. I have been looking at this code for hours and days but I just cant seem to find out why my delRecFunc() isn't deleting just one record at a time. It appears it is deleting the whole file.
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iomanip>
using namespace std;
const int NAME_SIZE = 40, ADDR_SIZE = 50, PHONE_SIZE = 14;
char y, Y;
struct Info
{
char name[NAME_SIZE];
int age;
char address1[ADDR_SIZE];
char phone[PHONE_SIZE];
}people;
void popFunc();
void dispFunc();
void appFunc();
void delRecFunc();
void searchFunc();
void modRecFunc();
int getNumRec();
int validateRecord (int);
void wipeFunc();
int main()
{
Info person;
char choice, option;
cout << " The file we are working with is named stuff.dat,\n"
<< "I will let you know if it unavailable " << endl;
cout << " "<< endl;
do
{
cout << " \n \n Simple People Records... \n \n \n "
<< " Please choose from the following options: \n \n \n"
<< " 1. POPULATE THE BINARY FILE \n"
<< " 2. DISPLAY THE CONTENTS OF THE FILE\n"
<< " 3. APPEND A RECORD TO THE FILE\n"
<< " 4. DELETE A RECORD FROM THE FILE\n"
<< " 5. SEARCH FOR A CERTAIN RECORD\n"
<< " 6. MODIFY A CERTAIN RECORD. \n"
<< " 7. WIPE OUT THE ENTIRE CONTENTS OF THE FILE. \n" <<endl;
cin >> option;
switch(option)
{
case '1': popFunc();
system("pause");
break;
case '2': dispFunc();
break;
case '3': appFunc();
break;
case '4': delRecFunc();
break;
case '5':searchFunc();
break;
case '6':modRecFunc();
break;
case '7':wipeFunc();
break;
}
}
while(choice!= 1000);
system("pause");
return(0);
}
void popFunc()
{ fstream file("stuff.dat", ios::out | ios::binary);
if (file.fail())
{
cout << "the file does not exist so we are creating it now... ";
system("pause");
file.open("stuff.dat", ios::out);
}
cout << "enter the following data about a person: "<<endl;
cout << "Name: " <<endl;
cin >> ws;
cin.getline(people.name,NAME_SIZE);
cout << "Age (integers only or the program will be corrupted): " <<endl;
cin >> people.age;
cin.ignore(); //skip over the remaining newline.
cout<< "Address line 1: ";
cin >> ws;
cin.getline(people.address1,ADDR_SIZE);
cout << "Phone: in the following format ie: 201.123.1234 (no hyphens)";
cin >> ws;
cin.getline(people.phone,PHONE_SIZE);
file.write(reinterpret_cast<char *>(&people), sizeof(people));
file.close();
}
void dispFunc()
{
ifstream file;
file.open("stuff.dat", ios::binary);
if (file.fail())
{
cout << "the file does not exist so we are creating it now... ";
system("pause");
file.open("stuff.dat", ios::out);
}
while(file.read(reinterpret_cast<char *> (&people), sizeof(people)))
{
cout << "Name: "<< people.name <<endl;
cout << "Age: "<< people.age <<endl;
cout << "Address: " <<people.address1 <<endl;
cout << "Phone #: " <<people.phone << " \n\n"<<endl;
}
file.close();
}
void appFunc()
{ ofstream file;
file.open("stuff.dat", ios::binary | ios::app);
if (file.fail())
{
cout << "the file does not exist so we are creating it now... ";
system("pause");
file.open("stuff.dat", ios::out);
}
cout << "Name: " <<endl;
cin >> ws;
cin.getline(people.name,NAME_SIZE);
cout << "Age (integers only or the program will be corrupted): " <<endl;
cin >> people.age;
cin.ignore(); //skip over the remaining newline.
cout<< "Address line 1: ";
cin >> ws;
cin.getline(people.address1,ADDR_SIZE);
cout << "Phone: Phone: in the following format ie: 201.123.1234 (no hyphens)";
cin >> ws;
cin.getline(people.phone,PHONE_SIZE);
file.write(reinterpret_cast<char *>(&people), sizeof(people));
file.close();
}
void searchFunc()
{
int count =1;
int answer;
long recNum;
int recCount = getNumRec();
fstream search;
search.open("stuff.dat", ios::in | ios:: binary);
cout << " Please enter 1 - " <<recCount ;
cout << " and I will display the data /n"<<endl;
cin >>recNum;
while(recNum<1 || recNum > recCount)
{
cout<<"Please enter a number between 1 and "<<recCount<<": ";
cin>>recNum;
}
answer = validateRecord(recNum);
}
void modRecFunc()
{
int count=1;
int answer;//to hold choice from the user
long recNum;//to hold a record number
int recCount = getNumRec();//variable to hold how many record the file has
fstream search;
//open file
search.open("stuff.dat",ios::in|ios::out|ios::binary);
cout<<"Which record do you wish to edit? ";
cin>>recNum;//variable to store the record the user wish to delete
//validation so the user is not allow to enter more numbers than the actual
//size of the file
while(recNum<1||recNum>recCount)
{
cout<<"Please enter a number between 1 and "<<recCount<<": ";
cin>>recNum;
}
//move pointer to desire position
cout<<endl;
answer= validateRecord(recNum);//make sure the record is the right one
cout<<endl;
if(answer==1)
{
//get the new data
cout << "Name: " <<endl;
cin >> ws;
cin.getline(people.name,NAME_SIZE);
cout << "Age (integers only or the program will be corrupted): " <<endl;
cin >> people.age;
cin.ignore(); //skip over the remaining newline.
cout<< "Address line 1: ";
cin >> ws;
cin.getline(people.address1,ADDR_SIZE);
cout << "Phone: Phone: in the following format ie: 201.123.1234 (no hyphens)";
cin >> ws;
cin.getline(people.phone,PHONE_SIZE);
search.seekp((recNum-1)*sizeof(people), ios::beg);
search.write(reinterpret_cast<char *>(&people), sizeof(people));
}
cout<<endl;
search.close();
}
void wipeFunc()
{ char option;
ofstream file;
cout << "This option will delete all the contents of the file "<<endl;
cout << " Are you sure you want to delete the file? Y or N " <<endl;
cin >> option;
if (option == 'y' || option == 'Y')
{
cout << " Warning, if you hit Y, you will lose everything " <<endl;
cout << " Y to confirm, N to return to main menu" <<endl;
cin >> option;
if(option == 'y' || option == 'Y')
{
file.open("stuff.dat", ios::out | ios::binary);
file.close();
}
}
}
int validateRecord (int recNum)
{
int answer;// variable to hold the answer from the user
fstream validate;
//open file
validate.open("stuff.dat",ios::in|ios::out|ios::binary);
validate.seekg((recNum-1)*sizeof(people));
//read record from file
validate.read(reinterpret_cast<char*>(&people),sizeof(people));
cout<<endl;
cout << "the name is "<< people.name <<endl;
cout << "the age is "<< people.age <<endl;
cout << "the address is" <<people.address1 <<endl;
cout << "the phone is Phone: " <<people.phone <<endl;
cout<<"Is this the file you want?\n"
"1. yes\n"
"2. no\n"
"Choice: ";
cin>>answer;
// validate answer to 1 or 2
while(answer<1||answer>2)
{
cout<<"enter only 1 or 2: ";
cin>>answer;
}
cout<<endl;
validate.close();
return answer;
}
int getNumRec()
{
int count =0;
fstream file;
file.open("stuff.dat", ios::in|ios::binary);
if (file.fail())
{
cout << "Error opening file. Program aborting.\n";
}
file.read(reinterpret_cast<char *>(&people),sizeof(people));
while (!file.eof())
{
count++;
file.read(reinterpret_cast<char *>(&people),
sizeof(people));
}
file.close();//close the file
return count;//return count the erase function
}
void delRecFunc()
{
int count=1;
int answer; //to hold choice from the user
double recNum;//to hold the record number the user wishes to delete
int recCount = getNumRec();//variable to hold how many record the file has
fstream erase;
erase.open("stuff.dat", ios::in|ios::out|ios::binary);
Info Temp; //*****NEW TEMP STRUCT VAR*****//
cout << "Please enter the phone number of the person you want to delete in this
format 201.123.1111)";
cin >> recNum;
//move pointer to desire position
erase.seekg((recNum-1)*sizeof(people));
//read record from file
erase.read(reinterpret_cast<char*>(&people),sizeof(people)); //read record from file
//get answer from the validateRecord function
answer = validateRecord (recNum);
if(answer == 1)
{
ofstream copy;
copy.open("stuff2.dat", ios::out | ios::binary);
erase.seekg(0*sizeof(people)); // Move pointer to beginning of the file
erase.read(reinterpret_cast<char*>(&people),sizeof(people));
// BEGIN NEW CODE //
// Keep looping till there are no more records in the customer.dat file
while (!erase.eof())
{
// If the id that the user wants deleted doesnt match the current
// records id, copy it to the temp directory
if(people.phone != Temp.phone)
{
// Write the record to copy.dat
copy.write(reinterpret_cast<char*>(&people),sizeof(Info));
}
// Read the next line from customer.dat
erase.read(reinterpret_cast<char*>(&people),sizeof(Info));
}
// Close both files
erase.close();
copy.close();
}
// Delete the Old customer.dat file
remove("stuff.dat");
// Rename the temporary holder to "customer.dat"
rename("stuff2.dat","stuff.dat");
}
You're taking an improperly formatted double (inputted from the cin >> recNum), seeking that far (*sizeof(people) into the file, and going from there. Seems like you need to start there.
Looks like you are entering the phone number in recNum (in the format 201.123.1111) and doing a seekg, which will give incorrect results
I have been trying to figure out why it doesn't read my second getline() function. The first getline(cin, input) works fine for getting the input data for partition. I did the same for getting the job datas, and it doesn't work. No errors or anything. it just skips that line. Any help will be greatly appreciated. Thank you.
#include <iostream>
#include <string>
#include "memory.h"
#define show(a) std::cout << #a << ": " << (a) << std::endl
using std::cin;
using std::cout;
using std::endl;
int main(void)
{
//Variables
int MEM_SIZE = 0;
vector<int> partition_input;
vector<int> job_input;
while(true)
{
//INPUT PARTITION DATA
while(true)
{
char confirm;
string input;
int temp; //Temporary variable to store extracted data
int num_part = 0; //Iterator to check partition size and memory, and partition limits
int check_size = 0;
cout << "Please enter the memory size and partitions in the following format: \n";
cout << "1000 250 250 250 250 \n";
cout << "> ";
getline(cin, input); //The whole of user input is stored in variable "line"
istringstream os(input); //"line" is passed to stringstream to be ready to be distributed
while(os >> temp)
{
partition_input.push_back(temp);
check_size += partition_input[num_part];
num_part++;
}
//sets the first input to MEM_SIZE
//AND deletes it from the partition vector
MEM_SIZE = partition_input[0];
partition_input.erase(partition_input.begin(), partition_input.begin()+1);
check_size -= MEM_SIZE;
cout << "Memory size: " << MEM_SIZE << endl;
cout << "# of Partitions: " << partition_input.size() << endl;
cout << "Partition Sizes: ";
for(int i = 0; i < partition_input.size(); i++)
cout << partition_input[i] << ' ';
cout << endl;
cout << "Correct? (y/n) ";
cin >> confirm;
cout << endl << endl;
if(confirm == 'n')
continue;
//Checks the total partition size and the total memory size
if(!(MEM_SIZE >= check_size))
{
cout << "ERROR: Total partition size is less than total memory size." << endl;
system("pause");
continue;
}
//Check if it exceeded the max number of partition limit
if((num_part-1) >=5)
{
cout << "ERROR: You have entered more than the allowed partition(5). Please try again. \n";
system("pause");
continue;
}
break;
}
//START INPUT JOB DATA
do
{
string input2;
cout << "Please enter the size of each job in the following format." << endl;
cout << "300 100 200 400" << endl;
cout << "> ";
//*******************
//IT SKIPS THESE TWO LINES
getline(cin, input2);
istringstream oss(input2);
//*******************
int j;
while(oss >> j)
job_input.push_back(j);
for(int i = 0; i < job_input.size(); i++)
cout << job_input[i] << ' ';
break;
}while(false);
//END OF INPUT JOB DATA
break;
}
Memory A1(MEM_SIZE, partition_input.size(), partition_input, job_input.size(), job_input);
//A1.FirstFit();
// A1.BestFit();
// A1.NextFit();
// A1.WorstFit();
//system("pause");
return 1;
}
The problem is here:
cout << "Correct? (y/n) ";
cin >> confirm;
This reads the confirm character from the input. But not the '\n' character. Thus the next readline will read just that newline character.
It is best not to mix std::getline() and operator>> when reading from the input.
I would change the code to this:
cout << "Correct? (y/n) ";
std::string confirmLine;
std::getline(cin, confirmLine);
cout << endl << endl;
if(confirmLine == "n")
continue;