I fiddled around with your code for a bit and made it more robust. Now the user can enter Back, BACK, bAcK, etc and it will go back. You actually didn't even implement that feature.
Also, if the user accidently enters a non-digit char in for age it will just prompt them again until an integer in entered.
And lastly, you were printing out ALL the names no matter what which is bad because what if they only enter 3 names? Then you are printing out the garbage in the 7 other components of the array. I simply replaced (a < 10) with (a < i) but you might want to have a separate variable to store the number of valid entries in the database if they are going to be allowed to delete and re-enter entries while the program is running.
The function ProcessString will return false if they just hit enter without typing in a name so you can use that for further validation if you'd like.
Also right now you are just using cout to display the entries to the screen. If you want to print them to a file just have them enter the filename as a string and then;
1 2 3 4 5 6 7
|
ofstream fout;
string filename;
cin >> filename;
fout.open(filename.c_str()) // filename must be c-style string
fout << //whatever
|
A class would be the best way to implement this. (Or maybe Im just biased to Object Oriented Programming. haha)
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
|
#include<iostream>
#include<fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
string ToLower(string str) {
for (int i = 0; i < str.length(); i++)
str[i] = tolower(str[i]);
return str;
}
bool IsNumber(string str) {
for (int i = 0; i < str.length(); i++)
if (!isdigit(str[i]))
return false;
return true;
}
bool ProcessString(istream& in, string& str) {
char buff;
bool valid = true;
str = "";
in >> buff;
if (buff == '\n')
valid = false;
while (buff != '\n' and in) {
str += buff;
in.get(buff);
}
return valid;
}
int main() {
string name[10];
int age[10];
int input;
int i = 0;
string ageTmp;
string nameTmp;
mainmenu:
cout << "Database Manager" << endl;
cout << "1: New Database" << endl;
cout << "2: Load Database" << endl;
cout << "3: View Database" << endl;
cout << "4: Edit Database" << endl;
cin >> input;
switch (input) {
case 1:
cout << "\nEnter the info for each person. Limit of 10 people."<< endl
<< "Type 'back' to return to the main menu"<< endl;
cout << "Input Name: " << endl;
ProcessString(cin, nameTmp);
while (i < 10 and ToLower(nameTmp) != "back") {
name[i] = nameTmp;
cout << "Input Age: "<< endl;
cin >> ageTmp;
cin.ignore(256,'\n');
while (!IsNumber(ageTmp)) {
cout << "Input Age: "<< endl;
cin >> ageTmp;
cin.ignore(256,'\n');
}
age[i] = atoi (ageTmp.c_str());
i++;
cout << "\nEnter the info for each person. Limit of 10 people."<< endl
<< "Type 'back' to return to the main menu"<< endl;
cout << "Input Name: " << endl;
ProcessString(cin, nameTmp);
}
break;
case 3:
int a = 0;//another variable for counting
while (a < i) {
cout << name[a] << endl << age[a] << endl << endl; //now it prints only ages entered
a++;
}
break;
}
goto mainmenu;
return 0;
}
|