Address Book Program
Jan 7, 2015 at 9:12am UTC
I'm outputting an array of inputted entry and in the table, the first name won't pop up. the program leaves it blank. Do you know what's wrong in my code?
I just post the part of the code in which i think is where i go wrong
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
switch (option)
{
case 1:
cout << "Enter First Name: " ;
cin.getline(p.fname, 100);
newLine();
cout << "Enter Last Name: " ;
cin.getline(p.lname, 100);
cout << "Enter Address: " ;
cin >> p.address;
newLine();
cout << "Enter Contact Number: " ;
cin >> p.cnum;
newLine();
AddEntry(p);
break ;
//case 2:
// /* EditEntry();*/
// break;
//case 3:
// /*DeleteEntry();*/
// break;
case 4:
DisplayEntry();
break ;
//case 5:
// /*SearchEntry();*/
/* break;*/
case 6:
cout << "Program closed\n" ;
system("pause>0" );
exit(1);
default :
cout << "Invalid choice \n\n" ;
system("pause>0" );
}
}
return 0;
}
void AddEntry(Person p)
{
person[index] = p;
index++;
}
void DisplayEntry()
{
cout << setw(11) << "No." << setw(10)
<< " First Name" << setw(10)
<< "Last Name" << setw(18)
<< "Address" << setw(10)
<< " Contact No." << setw(10);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
int i = 0;
for (; i < index; i++){
cout << endl;
cout << i + 1 << setw(10);
cout << person[i].fname << setw(10); //this won't output...
cout << person[i].lname << setw(18);
cout << person[i].address << setw(10);
cout << person[i].cnum <<setw(10);
}
printf("\nRecords Total:%d\n" , i);
system("pause>0" );
}
Jan 7, 2015 at 10:27am UTC
The issue could be this: the name is not correctly read because of the '\n' that remains in the buffer after you read the number "option". Try cleaning the buffer before reading the name with this function:
1 2 3
void clean_buffer() {
while (getchar() != '\n' );
}
and tell us if you solve. I'm not 100% sure but from a quick look, this could be a possible answer.
Jan 7, 2015 at 10:57am UTC
More details would be helpful, for example in
1 2 3 4 5
void AddEntry(Person p)
{
person[index] = p;
index++;
}
No telling how you've manipulated "index", it's obviously been defined in a different scope so all bets are off, as you haven't shown it.
Piece of advice - programmers are notoriously bad at guessing where the problem is in their own code, so if you can, post the whole code, at least post something that can be compiled and tested to demonstrate the problem.
Jan 7, 2015 at 12:53pm UTC
This is the whole code.
@minomic: where should i put the clean_buffer()?
@tipaye: Sorry, i'm a complete noob in programming xD
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 123 124 125 126 127 128 129
#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
using namespace std;
struct Person
{
char fname[100];
char lname[100];
char cnum[15];
string address;
};
void AddEntry(Person p);
void DisplayEntry();
void line(char , int );
void newLine();
Person person[20];
Person p;
int index = 0;
int main()
{
int option;
while (1){
system("cls" );
system("COLOR D" );
cout << "-------------------Address Book" ;
line('-' , 19);
cout << "\n What would you like to do?\n" ;
line('-' , 50);
cout << " [1] Add Contact \n" ;
cout << " [2] Edit Contact \n" ;
cout << " [3] Delete Contact \n" ;
cout << " [4] View Contacts \n" ;
cout << " [5] Search Address Book \n" ;
cout << " [6] Exit \n" ;
line('-' , 50);
cout << "Please enter your choice: " ;
cin >> option;
char buff[2];
switch (option)
{
case 1:
system("cls" );
cout << "Enter First Name: " ;
cin.getline(p.fname, 100);
newLine();
cout << "Enter Last Name: " ;
cin.getline(p.lname, 100);
cout << "Enter Address: " ;
cin >> p.address;
newLine();
cout << "Enter Contact Number: " ;
cin >> p.cnum;
newLine();
AddEntry(p);
break ;
//case 2:
// /* EditEntry();*/
// break;
//case 3:
// /*DeleteEntry();*/
// break;
case 4:
DisplayEntry();
break ;
//case 5:
// /*SearchEntry();*/
/* break;*/
case 6:
exit(1);
default :
cout << "Invalid choice \n\n" ;
system("pause>0" );
}
}
return 0;
}
void AddEntry(Person p)
{
person[index] = p;
index++;
}
void DisplayEntry()
{
cout << setw(11) << "No." << setw(10)
<< " First Name" << setw(10)
<< "Last Name" << setw(18)
<< "Address" << setw(10)
<< " Contact No." << setw(10);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
int i = 0;
for (; i < index; i++){
cout << endl;
cout << i + 1 << setw(10);
cout << person[i].fname << setw(10);
cout << person[i].lname << setw(18);
cout << person[i].address << setw(10);
cout << person[i].cnum <<setw(10);
}
printf("\nRecords Total:%d\n" , i);
system("pause>0" );
}
void line(char ch, int ctr)
{
for (int i = 0; i < ctr; i++) {
cout << ch;
}
cout << endl;
}
void newLine()
{
char s; do {
cin.get(s);
} while (s != '\n' );
}
Jan 7, 2015 at 1:37pm UTC
Just trying to look at a hack for the problem you mentioned, rather than the code as a whole...
clean_buffer() is essentially what you've already done with newLine(), so you don't need it. Instead try adding a call to newLine() after line 43
cin >> option;
Also don't forget the proper includes
1 2
#include<cstdlib>
#include<cstdio>
This one is a problem:
#include<cstring>
Jan 8, 2015 at 7:32am UTC
@tipaye: Thank you! It now work!
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 123 124 125 126 127 128 129 130 131 132
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<iomanip>
using namespace std;
struct Person
{
char fname[100];
char lname[100];
char cnum[15];
string address;
};
void AddEntry(Person p);
void DisplayEntry();
void line(char , int );
void newLine();
Person person[20];
Person p;
int index = 0;
int main()
{
int option;
while (1) {
system("cls" );
system("COLOR D" );
cout << "-------------------Address Book" ;
line('-' , 19);
cout << "\n What would you like to do?\n" ;
line('-' , 50);
cout << " [1] Add Contact \n" ;
cout << " [2] Edit Contact \n" ;
cout << " [3] Delete Contact \n" ;
cout << " [4] View Contacts \n" ;
cout << " [5] Search Address Book \n" ;
cout << " [6] Exit \n" ;
line('-' , 50);
cout << "Please enter your choice: " ;
cin >> option;
newLine();
char buff[2];
switch (option)
{
case 1:
system("cls" );
system("COLOR B" );
cout << "Enter First Name: " ;
cin.getline(p.fname, 100);
cout << "Enter Last Name: " ;
cin.getline(p.lname, 100);
cout << "Enter Address: " ;
cin >> p.address;
newLine();
cout << "Enter Contact Number: " ;
cin >> p.cnum;
newLine();
AddEntry(p);
break ;
//case 2:
// /* EditEntry();*/
// break;
//case 3:
// /*DeleteEntry();*/
// break;
case 4:
DisplayEntry();
break ;
//case 5:
// /*SearchEntry();*/
/* break;*/
case 6:
exit(1);
default :
cout << "Invalid choice \n\n" ;
system("pause>0" );
}
}
return 0;
}
void AddEntry(Person p)
{
person[index] = p;
index++;
}
void DisplayEntry()
{
system("cls" );
system("COLOR E" );
cout << setw(11) << "No." << setw(10)
<< " First Name" << setw(10)
<< " Last Name" << setw(18)
<< "Address" << setw(10)
<< " Contact No." << setw(10);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
int i = 0;
for (; i < index; i++){
cout << endl;
cout << i + 1 << setw(10);
cout << person[i].fname << setw(10);
cout << person[i].lname << setw(18);
cout << person[i].address << setw(10);
cout << person[i].cnum <<setw(10);
}
printf("\nRecords Total:%d\n" , i);
system("pause>0" );
}
void line(char ch, int ctr)
{
for (int i = 0; i < ctr; i++) {
cout << ch;
}
cout << endl;
}
void newLine()
{
char s; do {
cin.get(s);
} while (s != '\n' );
}
Topic archived. No new replies allowed.