c ++中的行函数无法正常工作

问题描述 投票:1回答:1

我试图获取用户输入,并且应该包含一个空格,因为我要输入全名。因此,我使用了getline方法,但无法正确输入数据。这是代码:

#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); 

当我运行程序时,我得到的是:

Enter the name of the contact to be added:Enter the mobile number of this contact:

这是主要方法:

/*
 * 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;

}

我无法分别输入每个值。

您知道如何解决此问题吗?

谢谢您的帮助。

c++ input netbeans getline
1个回答
0
投票

@ NathanOliver如何怀疑您使用了cin。在第111和153行的主要方法中(如果采用与上述相同的格式),您可以找到它们。也尝试将它们更改为getline()。应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.