Converting an Array of Character to a String
I am a newbie and completed a phone book project in which a user could enter a single name, number and single-word note for each entry. In a follow-up, the instructor has now required that the program accept names and notes that might be multiple words. I set the program up with a first class for entries and second class for the phone book; “currentname” and “currentnumber” are variables declared in the phone book class. The “add entry to the phone book” function below is intended to receive a character array (all words and spaces between) for names and numbers. The function is within the phone book class. I am getting error messages at the lines where I hope to make conversions of character arrays to strings.
Code:
void enterNewContact(){ char tempName[40], tempNote[50];
if (num_entries > 0)
i = num_entries;
else
i = 0;
cout << "Please enter Name:";
cin.getline(tempName, 40);
currentname(tempName); //Intending that the character array be "converted" to string here. Getting error message.
entryItem[i].setName(currentname);
cout << "Please enter Number:";
cin >> currentnumber;
while (currentnumber.at(3) != '-'){
cout << "Please enter the phone number in the following format: XXX-XXXX" << endl;
cout << "Please enter Number:";
cin >> currentnumber;
}
entryItem[i].setNumber(currentnumber);
cout << "Please enter Note:" << endl;
cin.getline(tempNote, 50);
currentnote(tempNote); //Same issue here.
entryItem[i].setNote(currentnote);
num_entries++;
cout << "The new entry has been added to the Phone book." << endl;
}