#include <iostream> #include <iomanip> #include <string> #include <fstream> usin
ID: 3641676 • Letter: #
Question
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int MAX = 50;
enum {ADD = 1, SEARCH, PRINT, EXIT};
int getMenuChoice();
void displayMenu();
void processUser(int ids[], string names[], double balances[], int& count, int choice);
void addUser(int ids[], string names[], double balances[], int count);
void getId(int ids[], int count);
void getName(string ids[], int count);
void getBalance(double balances[], int count);
void search_ID(int ids[], string names[], double balances[], int count);
void printAll(int ids[], string names[], double balances[], int count);
void printLine();
void printSpace();
void printDoubleLine();
void saveFile(int ids[], string names[], double balances[], int count);
void printFileLine(ofstream outFile);
void PrintFileDoubleLine(ofstream outFile);
void PrintFileSpace(ofstream outFile);
void loadFile(int ids[], string names[], double balances[]);
//Function name: main()
//purpose: to loop processUser menu
//Return type: int
//Input parameters: none
//Output parameters: none
//User inputs: none
//User outputs: none
//Functions Called: processUser(), saveFile()
//Error messages: none
int main()
{
int count = 0;
int target = 0;
int ids[MAX];
int choice;
double balances[MAX];
string names[MAX];
loadFile(ids, names, balances);
while((choice = getMenuChoice()) != EXIT)
{
processUser(ids, names, balances, count, choice);
}
saveFile(ids, names, balances, count);
return 0;
}
void loadFile(int ids[], string names[], double balances[])
{
ifstream inFile;
int index = 0;
char next = ' ';
inFile.open("customer.txt");
inFile.get(next);
while(! inFile.eof())
{
if (next == ' ') // <<<<-------------------- This Fucntion is what i am having trouble with??
{ index++; }
inFile.get(next);
}
cout << index;
while (! inFile.eof())
{
for (int i = 0 ; i < index ; i++)
{
inFile >> ids[i] >> names[i] >> balances[i];
}
}
}
//Function: saveFile()
//Purpose: To save data to file after exiting program
//Return: none
//Input parameters: ids[], names[], balance[], count
//Output parameters: none
//User inputs: none
//User outputs: none
//Function called: none
//Error messages: none
void saveFile(int ids[], string names[], double balances[], int count)
{
ofstream outFile;
outFile.open("customer.txt");
outFile << fixed << setprecision(2) << showpoint;
outFile << setfill(' ');
for(int i = 0 ; i < count ; i++)
{
outFile << left << setw(10) << ids[i] << left << setw(29) << names[i] << right << setw(1) << right << setw(10) << balances[i] << endl;
}
}
//Function: getMenuChoice()
//Purpose: To get user choice from menu
//Return: 1-4
//Input parameters: none
//Output parameters: none
//User inputs: 1-4
//User outputs: none
//Function called: none
//Error messages: !!Wrong input, please input 1-4
int getMenuChoice()
{
int choice;
bool valid;
do
{
displayMenu();
cin >> choice;
if( cin.fail() || choice < 1|| choice > 4)
{
cin.clear();
valid = false;
cout << " !! Wrong input, please input 1-4 ";
printLine();
}
else
{
valid = true;
}
cin.ignore(100, ' ');
} while (!valid);
return choice;
}
//Function: displayMenu()
//Purpose: to display menu
//Return: none
//Input parameters: none
//Output parameters: none
//User inputs: none
//User outputs: none
//Function called: none
//Error messages: none
void displayMenu()
{
printSpace();
cout << "MENU ";
printLine();
printSpace();
cout << "1. Add Customer "
<< "2. Search by ID "
<< "3. Print All "
<< "4. Exit ";
printLine();
printSpace();
cout << "Enter 1-4: ";
}
//Function: processUser()
//Purpose: to process the choice given
//Return: none
//Input parameters: choice, count
//Output parameters: id[], names[], balance[], count
//User inputs: none
//User outputs: none
//Function called: addUser(), printAll()
//Error messages: none
void processUser(int ids[], string names[], double balances[], int& count, int choice)
{
if (choice == ADD)
{
printSpace();
printDoubleLine();
printSpace();
cout << "ADD USER ";
printSpace();
printLine();
printSpace();
addUser(ids, names, balances, count);
count++;
}
if (choice == SEARCH)
{
printSpace();
printDoubleLine();
printSpace();
cout << "Search by Id ";
printSpace();
search_ID(ids, names, balances, count);
}
if (choice == PRINT)
{
cout << " PRINT ALL "
<< endl;
printAll(ids, names, balances, count);
}
}
//Function: addUser
//Purpose: to gather user info
//Return: none
//Input parameters: count
//Output parameters: ids[], names[], getBalance[]
//User inputs: none
//User outputs: none
//Function called: getId(), getName(), getBalance()
//Error messages: none
void addUser(int ids[], string names[], double balances[], int count)
{
getId(ids, count);
getName(names, count);
getBalance(balances, count);
}
//Function:getId()
//Purpose: get id number
//Return: none
//Input parameters: count
//Output parameters: ids[]
//User inputs: id number
//User outputs: none
//Function called: printSpace()
//Error messages: !! Please put a # greater than zero.
void getId(int ids[], int count)
{
bool valid;
do
{
cout << "ID: ";
cin >> ids[count];
if (cin.fail() || ids[count] < 0)
{
cin.clear();
cout << " !! Please put a # greater than zero. ";
valid = false;
}
else
{
valid = true;
}
cin.ignore(100, ' ');
} while (!valid);
printSpace();
}
//Function: getName()
//Purpose: to get name
//Return: none
//Input parameters: count
//Output parameters: names[]
//User inputs: name
//User outputs: none
//Function called: printSpace(), printDoubleLine()
//Error messages: !! Please put a name.
void getName(string names[], int count)
{
bool valid;
do
{
cout << "Name: ";
getline(cin, names[count]);
if (names[count].length() != 0)
{
valid = true;
}
else
{
cout << " !! Please put a name. ";
valid = false;
}
} while (!valid);
printSpace();
}
//Function: getBalance()
//Purpose: to get Balance
//Return: none
//Input parameters: count
//Output parameters: balance[]
//User inputs: balance
//User outputs: none
//Function called: none
//Error messages: Please put a balance greater than zero
void getBalance(double balances[], int count)
{
bool valid;
do
{
cout << "Balance: ";
cin >> balances[count];
if (cin.fail() || balances[count] <= 0)
{
cin.clear();
valid = false;
cout << "**Please put a balance greater than zero ";
}
else
{
valid = true;
}
cin.ignore(100, ' ');
} while (!valid);
}
//Function: search_ID()
//Purpose: search for ID
//Return: none
//Input parameters: ids[], names[], balance[], count
//Output parameters: none
//User inputs: none
//User outputs: none
//Function called: printline(), printDoubleline()
//Error messages: !! Please put a number greater than zero!
void search_ID(int ids[], string names[], double balances[], int count)
{
int target;
int index = 0;
bool found = false;
bool valid;
do
{
cout << " Id? ";
cin >> target;
if (cin.fail() || target <= 0)
{
cin.clear();
cout << "!! Please put a number greater than zero!";
valid = false;
}
else
{
valid = true;
}
cin.ignore(1000, ' ');
} while (!valid);
while ((!found) && (index < count))
{
if (target == ids[index])
{
found = true;
}
else
{
index++;
}
}
if (found)
{
printDoubleLine();
cout << left << setw(10) << "ID" << left << setw(30) << "Name" << right << setw(11) << "Balance ";
printLine();
cout << left << setw(10) << ids[index] << left << setw(29) << names[index] << right << setw(1) << "$" << right << setw(10) << balances[index] << endl;
printDoubleLine();
}
else
{
cout << " Customer ID " << target << " not found" << endl;
}
}
//Function: printAll()
//Purpose: to print and save in fornat
//Return: none
//Input parameters: ids[], names[], balance[], count
//Output parameters: none
//User inputs: none
//User outputs: none
//Function called: none
//Error messages: none
void printAll(int ids[], string names[], double balances[], int count)
{
cout << fixed << setprecision(2) << showpoint;
cout << setfill(' ');
printDoubleLine();
cout << left << setw(10) << "ID" << left << setw(30) << "Name" << right << setw(11) << "Balance ";
printLine();
for(int i = 0 ; i < count ; i++)
{
cout << left << setw(10) << ids[i] << left << setw(29) << names[i] << right << setw(1) << "$" << right << setw(10) << balances[i] << endl;
}
printDoubleLine();
}
//Function: printDoubleLine()
//Purpose: to print doubleline
//Return: none
//Input parameters: none
//Output parameters: none
//User inputs: none
//User outputs: none
//Function called: none
//Error messages: none
void printDoubleLine()
{
for (int i = 0 ; i < 50 ; i++)
{
cout << "=";
}
cout << endl;
}
//Function: printLine
//Purpose: to print line
//Return: none
//Input parameters: none
//Output parameters: none
//User inputs: none
//User outputs: none
//Function called: none
//Error messages: none
void printLine()
{
for (int i = 0 ; i < 50 ; i++)
{
cout << "-";
}
cout << endl;
}
//Function: printSpace()
//Purpose: to print a line of space
//Return: none
//Input parameters: none
//Output parameters: none
//User inputs: none
//User outputs: none
//Function called: none
//Error messages: none
void printSpace()
{
for (int i = 0 ; i < 50 ; i++)
{
cout << " ";
}
cout << endl;
}
Explanation / Answer
The problem is that you opened the file only once. You already reached the end of the file when you were calculating the number of records. So, what you need to do is close and then reopen the file once more. Before attempting to read data from file to the arrays.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.