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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// I'm writing a program that gives the user the option to display the first
// five names (one name on each line) on an existing names.txt file with a
// few dozen names, as well as display the last five names on the names.txt
// file. I'm having a hard time with the addNewNames() functions as when I go
// to enter a name and press enter, an infinite loop of !!! Choice out of
// range (1 - 5 only) comes up. I am writing a first and last name to the
// string. I would appreciate any help here :)
#include <iostream>
#include <fstream>
#include <limits>
#include <string>
void handleMenu(const std::string& filename);
int getChoice();
void displayFirstFive(std::fstream & fin);
void displayLastFive(std::fstream & fin);
int count(std::fstream & fin);
void addNewNames(std::fstream & fin);
int main()
{
std::string filename { "names.txt" };
handleMenu(filename);
std::cout << "Over!\n";
// system("PAUSE"); // http://www.cplusplus.com/forum/beginner/1988/
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
void handleMenu(const std::string& filename)
{
std::string line(45, '*');
int choice = 0;
do {
std::cout << line << '\n';
std::cout << "1. Display first five names\n"
"2. Display last five names\n"
"3. Count the number of names\n"
"4. Add a new name\n"
"5. Quit\n";
std::cout << line << '\n';
choice = getChoice();
std::fstream fin(filename);
if(!fin) {
std::cout << "can't open " << filename << ".\n";
return;
}
// switch?
if (choice == 1) { displayFirstFive(fin); }
else if (choice == 2) { displayLastFive(fin); }
else if (choice == 3) { std::cout << "There are " << count(fin) << " names\n\n"; }
else if (choice == 4) { addNewNames(fin); }
} while (choice != 5);
}
int getChoice()
{
std::cout << "\nEnter your choice: ";
int choice = 0;
std::cin >> choice;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while (choice < 1 || choice > 5)
{
std::cout << "!!! Choice out of range (1 - 5 only). Try again: ";
std::cin >> choice;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return choice;
}
void displayFirstFive(std::fstream & fin)
{
fin.clear();
fin.seekg(0, std::ios::beg);
std::string line;
int count = 0;
while (std::getline(fin, line) && count < 5)
{
std::cout << count + 1 << ": " << line << '\n';
count++;
}
std::cout << '\n';
}
void displayLastFive(std::fstream & fin)
{
int total = count(fin), counter = 0;
fin.clear();
fin.seekg(0, std::ios::beg);
std::string line;
while (std::getline(fin, line))
{
counter++;
if (counter > total - 5) { std::cout << counter << ": " << line << '\n'; }
}
std::cout << '\n';
}
int count(std::fstream & fin)
{
fin.clear();
fin.seekg(0, std::ios::beg);
int numOfLines = 0;
std::string line;
while (std::getline(fin, line)) { numOfLines++; }
return numOfLines;
}
void addNewNames(std::fstream& fin)
{
fin.clear();
fin.seekg(0, std::ios::end);
std::cout << "Adding a name\nEnter a name: ";
std::string input;
getline(std::cin, input);
fin << input << '\n';
}
|