Issue
I was trying to get the user input and it should include a space because I am asking for a full name. So I used the getline method but I can't input the data correctly. Here's the code:
#include <iostream>
#include <string>
string name, mobile, landline, work;
cout << "Enter the name of the contact to be added:";
getline(cin, name);
cout << "Enter the mobile number of this contact:";
getline(cin, mobile);
cout << "Enter the landline number of this contact:";
getline(cin, landline);
cout << "Enter the work number of this contact:";
getline(cin, work);
When I run the program this is what I get:
Enter the name of the contact to be added:Enter the mobile number of this contact:
This is the main method:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: Gaby
*
* Created on March 25, 2020, 7:14 PM
*/
#include "Contact.h"
#include "DCSList.h"
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
void showMenu();
void saveChanges(ofstream& file, DCSList& list);
/*
*
*/
int main(int argc, char** argv) {
fstream file; //This is the file we read from
ofstream outputFile; //This is the file we will write on at the end
string name, numberString; //Strings to extract the data from the file
string numbers[3]; //string array of numbers
string numberExtracted; //Used to extract each number from the numberString
string numbOfContacts; //Number of Contacts we want to read from file
int number; //Integer variable of the parsed value of the number read
int counter = 1; //Number of contacts read from file
int choice = 0; //Choice of user
DCSList list;
file.open("./ContactList.txt");
if(file.is_open()){
//If file is open then read from it
//First get the number of contacts in list
getline(file, numbOfContacts);
//Convert the number in the file to an integer
stringstream count(numbOfContacts);
//Set the value of number
count >> number;
while(counter <= number){
//Extract the name of the contact
getline(file, name);
//Extract the mobile, work and home numbers
getline(file, numberString);
//We will need to split the numberString with the - as delimiter
stringstream ss(numberString);
int i = 0; //Counter for numbers
while(getline(ss, numberExtracted, '-')){
numbers[i] = numberExtracted;
i++;
}
//Create a Contact object and add the contact to the list
Contact c(name, numbers[0], numbers[1], numbers[2]);
list.addNode(c);
counter++;
}
//Write on the other file
outputFile.open("NewContactList.txt");
if (outputFile.is_open()){
list.start();
while(list.hasNext()){
outputFile << list;
list.operator ++();
}
}
else{
cout << "File not opened!" << endl;
}
outputFile.close();
}
else{
cout << "File is not opened!" << endl;
}
file.close();
do{
showMenu();
cin >> choice;
switch(choice){
case 1:{
list.start();
while(list.hasNext()){
cout << list;
list.operator ++();
}
break;
}
case 2:
{
string name, mobile, landline, work;
cout << "Enter the name of the contact to be added:";
getline(cin, name);
cout << "Enter the mobile number of this contact:";
getline(cin, mobile);
cout << "Enter the landline number of this contact:";
getline(cin, landline);
cout << "Enter the work number of this contact:";
getline(cin, work);
//Create a contact object
Contact contact(name, mobile, work, landline);
//Add contact to list
list.addNode(contact);
}
break;
case 3:
{
string contactToDelete;
cout << "Enter a contact name or "
"any mobile, work, land number :";
cin >> contactToDelete;
if (list.deleteNode(contactToDelete)){
cout << "Contact has been deleted!" << endl;
}
}
break;
case 4:
{
string searchCriteria;
cout << "Enter a contact name or "
"any mobile, work, land number :";
getline(cin, searchCriteria);
if (list.searchNode(searchCriteria)){
cout << list;
}
else{
cout << "No contact found!" << endl;
}
break;
}
case 5:
{
cout << "Thank you for using the program." << endl;
cout << "All changes applied will be "
"saved to the output file" << endl;
//Write on the other file
outputFile.open("NewContactList.txt");
if (outputFile.is_open()) {
list.start();
while (list.hasNext()) {
outputFile << list;
list.operator++();
}
} else {
cout << "File not opened!" << endl;
}
file.close();
return 0;
}
default:{
cout << "Please enter a valid value to do an operation!" <<endl;
break;
}
}
}while(choice != 1 || choice != 2 ||
choice != 3 || choice != 4 || choice != 5);
return 0;
}
void showMenu(){
cout << "-------------PHONEBOOK-------------" << endl;
cout << "Enter a number according to "
"the operation you want to do: " << endl;
cout << "1. Show phone book" << endl;
cout << "2. Add new contact" << endl;
cout << "3. Delete a contact" << endl;
cout << "4. Search for a contact" << endl;
cout << "5. Exit program" << endl;
}
I am not able to input each value individually.
Do you know how can I fix this problem?
Thank you for your help.
Solution
How @NathanOliver has suspected you used a cin. In your main method in lines 111 and 153 (if formated in the same way as above) you can find them. Try to change them to getline() too. That should work.
Answered By - Hunterplayer 100
Answer Checked By - Marie Seifert (JavaFixing Admin)