Program stops after cin

the program is of an address book. the syntax is all fine, and the initial menu of options does show up. but once you punch in the cin, the program just stops. any help would be really appreciated, thanks!
Please post some code that exhibits the problem and we'll try to help.
alright here's the code:
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <cstring>
#include <cstdio>
#include <fstream>
using namespace std;

char Continue();
char Exit();

struct ab
{

    char Fname[15];
    char Lname[15];
    char Number[10];
    char Email[30];
    char Age[3];
    char Fax[10];

};

class AddressBook
{

    ab var;

public:
    void AddContact();
    void ContactInfo();
    int Total();
    void Show1();
    void SearchContact();
    void All();
    void Delete();

};

void AddressBook::AddContact()
{
    ofstream outFile;
    outFile.open("Book1.txt", ios::app);
    ContactInfo();
    outFile.write((char*)&var,sizeof(var));
    outFile.close();
    Continue();
}

void AddressBook::ContactInfo()
{
    char info;
    char type;
    cout << "Will you be adding a personal or business contact? (p/b)\n";
    cin >> type;
    cout << "Enter your new contact's first name: ";
    cin >> var.Fname;
    cout << "\nLast name: ";
    cin >> var.Lname;
    cout << "\nNumber (without spaces): ";
    cin >> var.Number;
    cout << "\nE-mail: ";
    cin >> var.Email;
    cin.get(info);

    if (type == 'p'||type == 'P')
	{
	    cout << "\nAnd the age: ";
	    cin >> var.Age;
	    cout << "Your contact has been created!\n";
	    var.Fax == "personal contact: no fax number stored\n";
	}

    else if (type == 'b'||type == 'B')
	{
	    cout << "\nAnd the fax number: ";
	    cin >> var.Fax;
	    cout << "Your contact has been created!" << endl;
	    var.Age == "business contact: no age stored\n";
	}
}

int AddressBook::Total()
{
    int i = 0;
    int n = (i/5);
    string line;
    ifstream inFile;
    inFile.open("Book1.txt");
    while (getline (inFile, line))
	{
	    ++i;
	}
    cout << "There are " << n << " contact(s) in your address book.";
    inFile.close();
    return n;
}

void AddressBook::Show1()
{
    cout << "\nContact name: " << var.Fname << " " << var.Lname << "\n";
    cout << "\nContact phone number: " << var.Number << "\n";
    cout << "\nContact e-mail: " << var.Email << "\n";
    cout << "\nContact age: " << var.Age << "\n";
    cout << "\nContact fax number: " << var.Fax << "\n";
}

void AddressBook::SearchContact()
{
    char find[15];
    ifstream inFile;
    inFile.open("Book1.txt");
    cin >> find;
    int n = Total();
    for (int i = 0; i < n; i++)
	{
	    inFile.read((char*)&var,sizeof(var));
	    if (find == var.Lname)
		{
		    Show1();
		    return;
		}
	    else
		{
		    cout << "Contact was not in the address book.";
		}
	}
    inFile.close();
    Continue();
}

void AddressBook::All()
{
    string line;
    ifstream inFile("Book1.txt");
    if (inFile.is_open())
	{
	    while (getline (inFile, line))
		{
		    cout << line << endl;
		}
	    inFile.close();
	}
    else
	{
	    cout << "Unable to open file.";
	}
    Continue();
}

void AddressBook::Delete()
{
    char find[15];
    int n,i;
    ifstream inFile;
    ofstream outFile;
    inFile.open("Book1.txt");
    outFile.open("Book2.txt");
    n = Total();
    for (i = 0; i < n; i++)
	{
	    inFile.read((char*)&var,sizeof(var));
	    outFile.write((char*)&var,sizeof(var));
	}
    inFile.close();
    outFile.close();
    outFile.open("Book1.txt",ios::trunc);
    inFile.open("Book2.txt");
    cout << "\nPlease enter the last name of the contact to be deleted:\n";
    cin >> find;
    for (i = 0; i < n; i++)
	{
	    inFile.read((char*)&var,sizeof(var));
	    if (find != var.Lname)
		outFile.write((char*)&var,sizeof(var));
	}
    outFile.close();
    inFile.close();
}


int main()
{

    AddressBook Book;
    ab var;    
    int choice;
    cout << "=========================ADDRESS BOOK=========================\n";
    cout << "What would you like to do?\n\n";
    cout << "1. Create New Contact\n\n";
    cout << "2. Search for an Existing Contact\n\n";
    cout << "3. View All Contacts\n\n";
    cout << "4. Delete a Contact\n\n";
    cout << "5. Exit\n\n";
    cout << "Please enter a choice from 1 - 5 : \n\n" << endl;
    cin >> choice;

    if (choice == '1')
	Book.AddContact();
    else if (choice == '2')
	Book.SearchContact();
    else if (choice == '3')
	Book.All();
    else if (choice == '4')
	Book.Delete();
    else if (choice == '5')
	Exit();

    return 0;
}

char Continue()
{
    char answer;
    cout << "\nDo you want to continue using the program? (y/n)\n";
    cin >> answer;
    if (answer == 'y'||answer == 'Y')
	{
	    return main();
	}
}

char Exit()
{
    char answer;
    cout << "Are you sure you want to exit? (y/n) ";
    cin >> answer;
    if (answer == 'n'||answer == 'N')
	{
	    return main();
	}
}
change line 185 to char choice;

'1' is not equal to 1.

http://ideone.com/L4DLpC

Also, calling main from Continue and Exit is not legal C++. The standard forbids you from calling main in your code.
@cire hit the nail on the head, but just a side note tip for you: You should almost always have an else case in your if-else blocks to catch any erroneous input.

Right now, I could type in like 7 or 12 or TreskyIsAwesome and the program would just shut down without telling me what happened. So it is always good to have an else case to catch bad inputs, so it can tell me "Invalid Input" and let me try again.

Friendly advice. :)
alright thanks a bunch guys!

btw cire, do you have any advice on how I would bring the program back to the main menu at the end of a function?
Well...

1
2
3
4
5
6
7
8
9
10
int main()
{
    while(1)
    {
        // write your code here
        if(quit_program)
            break;
    }
    return 0;
}


As obvious, to quit the program, you have an example right there.
Last edited on
oh, ok. yeah that makes sense. thanks!

but what about returning it to the beginning? would this work? :
1
2
3
4
if (choice == '1'){
	Book.AddContact();
        return main();
}


sorry, I kinda suck at programming right now...tryna get better tho.
The while loop will take care of going back from the beginning.
Basically, when an expression inside the while loop is evaluated as true (1 IS evaluated to always true), some code will be executed.

1
2
3
4
5
6
7
int main()
{
    while(1)
    {
        cout << "xo";
    }
}


Will write an endless line of "xoxoxoxoxoxoxoxo".
Now, how will it ever stop?
Well, either the value to be evaluated becomes false (or 0), or you have a break statement inside the loop.

For your example:

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
int main()
{
    while(1)
    {
        AddressBook Book;
        ab var;    
        int choice;
        cout << "=========================ADDRESS BOOK=========================\n";
        cout << "What would you like to do?\n\n";
        cout << "1. Create New Contact\n\n";
        cout << "2. Search for an Existing Contact\n\n";
        cout << "3. View All Contacts\n\n";
        cout << "4. Delete a Contact\n\n";
        cout << "5. Exit\n\n";
        cout << "Please enter a choice from 1 - 5 : \n\n" << endl;
        cin >> choice;

        if (choice == '1')
            Book.AddContact();
        else if (choice == '2')
            Book.SearchContact();
        else if (choice == '3')
            Book.All();
        else if (choice == '4')
            Book.Delete();
        else if (choice == '5')
            break;
    }
    return 0;
}
ohhh i see, i didnt know that before. wow thats great to know. thanks man!
Topic archived. No new replies allowed.