-
C++ Novice needs help
Hey guys, I've run into a small problem on this program I was creating. It's supposed to be a simple database using arrays to store names in strings and ages in a separate array. Unfortunately, when I try to have the program echo back all the inputted data, it fails and just inputs the first name and age entered. Take a look:
Code:
//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; //counting
int a;//counting
a = 0;
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 < 10) {
cout << endl;
cout << "Enter the info for each person. Limit of 10 people."<< endl << "Type 'back' to return to the main menu"<< endl; //back function not implemented yet
cout << "Input Name: " << endl;
cin >> name[i];
cout << "Input Age: "<< endl;
cin >> age[i];
i++;
}
break;
case 3:
for (a=0; a<10; a++) {//tried different kinds of loops, can't get it to print the arrays correctly
cout << name[a] << endl << age[a] << endl << endl; //should print out every name and age
}
break;
}
goto mainmenu;
}
Thanks in advance for any help :)
-
You should stop using the goto statement and use a while loop. A goto should be reserved for special occasions. A simple while loop would be a good replacement in this program.
Now as to your actual problem, the iostream extraction operator>> will stop processing when it encounters a white space character. So if you want your names to have spaces you should use getline() instead of the extraction operator.
Jim
-
Ok I changed it so that it uses getline() for the strings (and switched goto to a while loop using a bool variable) , but it's the output that doesn't seem to be working. It just outputs the first name and age.
-
What is your current code?
-
Code:
//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; //counting
int a;//counting
a = 0;
i = 0;
bool done;
done = false;
while(!done) {
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 < 10) {
cout << endl;
cout << "Enter the info for each person. Limit of 10 people."<< endl << "Type 'back' to return to the main menu"<< endl; //back function not implemented yet
cout << "Input Name: " << endl;
getline(cin,name[i]);
cout << "Input Age: "<< endl;
cin >> age[i];
i++;
}
break;
case 3:
while (a<10){
cout << name[a] << endl << age[a] << endl << endl; //should print out every name and age
}
break;
}
}
}
-
You don't change a within your loop.
Btw, it's good practice to define your variables in the place you actually use them rather than at the beginning of main().