C++ Programming

Why does the program skip when the user is supposed to enter first name??

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
#include <iostream>
using namespace std;


void print_menu(void);

int main()
{
int index=0;
char choice;
string firstname, lastname, nickname, email1, email2, phone1, phone2, address, website, birthday, notes;
print_menu();
cin >> choice;

while (index = choice)
{
      if (choice == 'a' || choice == 'A')
      {
         cout<<"Please enter first name"<<endl;
         getline( cin, firstname );
         cout<<"Please enter last name"<<endl;
         getline( cin, lastname );
         cout<<"Please enter nickname"<<endl;
         getline( cin, nickname );
         cout<<"Please enter email 1"<<endl;
         getline( cin, email1 );
         cout<<"Please enter email 2"<<endl;
         getline( cin, email2 );
         cout<<"Please enter phone 1"<<endl;
         getline( cin, phone1 );
         cout<<"Please enter phone 2"<<endl;
         getline( cin, phone2 );
         cout<<"Please enter address"<<endl;
         getline( cin, address );
         cout<<"Please enter website"<<endl;
         getline( cin, website );
         cout<<"Please enter birthday"<<endl;
         getline( cin, birthday );
         cout<<"Please enter notes"<<endl;
         getline( cin, notes );

         cout<<"--------------------DETAILED VIEW--------------------"<<endl;
         cout<<"Last Name:       "<<firstname<<endl;
         cout<<"First Name:      "<<lastname<<endl;
         cout<<"Nickname:        "<<nickname<<endl;
         cout<<"Email 1:         "<<email1<<endl;
         cout<<"Email 2:         "<<email2<<endl;
         cout<<"Phone 1:         "<<phone1<<endl;
         cout<<"Phone 2:         "<<phone2<<endl;
         cout<<"Address:         "<<address<<endl;
         cout<<"Website:         "<<website<<endl;
         cout<<"Birthday:        "<<birthday<<endl;
         cout<<"Notes:           "<<notes<<endl;
         cout<<"-------------------------------------------------"<<endl;
      print_menu();
      cin>>choice;
      }
}     
index++;
system ("PAUSE");
return 0;
}

void print_menu (void)
{
cout<<"--------------------MAIN MENU--------------------"<<endl;
cout<<"[A] add a contact to the address book"<<endl;
cout<<"[D] display all contact sorted by ...."<<endl;
cout<<"[C] display details of contact ...."<<endl;
cout<<"[R] remove contact"<<endl;
cout<<"[F] find contacts matching ...."<<endl;
cout<<"[M] modify contact"<<endl;
cout<<"[E] export to csv format"<<endl;
cout<<"[Q] quit the program"<<endl;
cout<<"-------------------------------------------------"<<endl;
}
Last edited on
closed account (oN3AqMoL)
Index is not initialized so its value is automatically set to 0. As soon as you fix that i'll try to scan the rest of the page.
-dean :)
Last edited on
I think I fixed it. It still doesn't work.
Please Help!!!!
The problem is that after you ask for the choice, the enter is not consumed or ignored, and gets taken by getline next. I would just replace all your get line's with cin >> first name; ...
If I do that, when I put space (two word) it messes up the programs
closed account (oN3AqMoL)
4 things:
1. Index MUST = choice or else the while loop wont execute.Since index is an integer, this is impossible. You either need to make index a char value or type cast.
2. When using while loops, do == instead of =. = means that index now equals choice.
3. Instead of using getline functions, use cin>> functions. They're the standard use.
4. You messed up with the cout<< functions in the last part of main.
4: 1.You accidentally said that the firstname was the lastname, and vice
versa.
4: 2.You also should know that leaving the gap means there are spaces
INBETWEEN the strings, and that <<stringhere<< means that it goes
AFTER whatever is in front of it.
Thanks for listening!
-dean :)
Last edited on
The program executes, but the problem is just that the first name doesn't ask the user for input. It couts the line only, not ask the user for input. Then moves on to second line, last name which works, and so on it works.
The things you mentioned doesn't fix the problems.
Last edited on
closed account (oN3AqMoL)
Change your code in the reply. I need to see the new code so I can fix it.
Also: I while(true) statement might help, I think the program would be better suited for break; statements.
Last edited on
Topic archived. No new replies allowed.