Need help with arrays, and possibly loops.

Working on this small project right now. I'm not sure if I am storing the names into an array properly. I can't get the program to output all the previously entered names and ages properly either. Any help, criticisms, ect. would be greatly appreciated.

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
//This is a program that allows you to create a simple database and output it all to a text file
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


int main() {
	
	string name[10];
	int age[10];
	int input;
	int i;
	i = 0;

	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:
		while (i <= 9) {
		cout << endl;
		cout << "Enter the info for each person. Limit of 10 people."<< endl << "Type 'back' to return to the main menu"<< endl;
		cout << "Input Name: " << endl;
			cin >> name[i];
		cout << "Input Age: "<< endl;
			cin >> age[i];
			i++;
		}
		break;
	
	case 3:
		int a = 0;//another variable for counting
		while (a <= 9) {
		cout << name[a] << endl << age[a] << endl << endl; //should print out every name and age
		a++;
		}
		break;
	}

	goto mainmenu;
}
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;
}


Last edited on
Topic archived. No new replies allowed.