Hello, this program is an address book where you caan add/view entries. I'm having a problem printing out entries. Can someone help me find out why the information isn't getting saved into the structure array?
Code:
#include <iostream>
#include <string>
using namespace std;
struct contactinfo
{
string name, phonenumber;
int age;
};
int menu();
void addentry(contactinfo,int);
void viewentry(contactinfo,int);
int main()
{
menu();
return 0;
}
void addentry(contactinfo contact[],int index)
{
char ch;
int size=10;
cout<<"\nEnter contact name: " ;
getline(cin, contact[index].name,'\n');
cout<<"\nEnter contact age: " ;
cin>> contact[index].age; cin.ignore();
cout<<"\nEnter contact phone number: ";
getline(cin, contact[index].phonenumber,'\n');
cout<<"\nWould you like to add another entry? [Y/N]\n";
cin>>ch; cin.ignore();
switch(ch)
{
case 'y':
{
if ((index+1)==size)
{
cout<<"\nYour address book is full.";
break;
}
else
addentry(contact, (index+1));
break;
}
case 'n':
menu();
break;
default:
cout<<"\nInvalid Input";
menu();
break;
}
return;
}
void viewentry(contactinfo contact[], int size)
{
for (int i=0;i<size;i++)
{
cout<<"\n" <<contact[i].name <<endl <<contact[i].age <<endl <<contact[i].phonenumber <<endl;
}
getchar();
}
int menu()
{
const int size=10;
int ch;
int index=0;
contactinfo contact[size];
cout<<"Address Book\n\n\t[1]Add a new entry\n\t[2]View previous entries\n\t[3]Exit Program\n\n" ;
cin>>ch; cin.ignore();
switch(ch)
{
case 1:
addentry(contact,index);
break;
case 2:
viewentry(contact,size);
break;
case 3:
return 0;
default:
cout<<"\nInvalid Input\n\n";
menu();
break;
}
return 0;
}