Hi,
I have some problems with this code:
Here I have the code for a function that enables the user to add name and numbers in my phonebook. However when i execute the function the program skipps the first name. Let's say that I choose val3 to be 2 and thus i get to input 2 names and 2 numbers etc. it will look like this:
People to add, input number between 1 and 100: 2
1*Name:
2*Telephone number: 073445263
3*Name: adampqmaba
4*Telephone number: 07331337
1* --- Just a blank here nothing to add or input
2* --- here i can insert any telephone number.
3* --- here the program acts as it should do.
4* --- same here the second time.
I have tried using cin.ignore(1000, '\t'); after my cin phrase. The program then goes into an infinite loop where i can input any numbers or letters just like a notepad. nothing like name: or number: is shown.
Code:
void addP(){
telefon Phone_data[100];
int val3;
ofstream utfil;
utfil.open("Phonebook.txt", ios::app);
main:
cout << "People to add, input number between 1 and 100: ";
cin >> val3;
if (val3>100 && val3<0){
goto main;
}
for (int i=0; val3>i; i++){
cout << "Name: ";
getline(cin, Phone_data[i].namn);
cout << endl << "Telephone number: ";
getline(cin, Phone_data[i].nummer);
utfil << Phone_data[i].namn << '\t' << '\t' << Phone_data[i].nummer << endl;
}
utfil.close();
}