cin.getline()存储了错误的数据

时间:2014-12-22 17:50:17

标签: c++ input cin cstring

我已经搜索过已经问过的问题,但我还没找到我正在寻找的东西。 我正在使用结构,我存储一个人的详细信息(名字,姓氏,出生日期等等)。然后,目标是创建一个指向这些结构的指针数组,并创建一个包含一组人员信息的列表。到目前为止,主要功能基本不同。

#include <iostream>
#include "methods.h"
using namespace std;

void printMenu() {
    cout << "\n Welcome to the Person program!" << endl
             << " Choose what you would like to do: " << endl
             << "  1. Insert person" << endl
             << "  2. Show person" << endl
             << "  3. Exit" << endl;
}

int main() {
    char choice;
    person p;

    do {
        printMenu();

        cout << "\n Your choice: ";
        cin >> choice;
        cin.ignore();

        switch(choice) {
            case '1': setPerson(p); break;
            case '2': getPerson(p); break;
            default: cout << "\n Exiting..." << endl;
        }
    } while(choice == '1' || choice == '2');

    return 0;
}

这是methods.cc文件

// methods.cc
#include <iostream>
#include "methods.h"
using namespace std;

static char dummy; // catches the '\n' left in the stream after the usage of cin >>

void setFirstName(char s[]) {
    cout << "\n First name: ";
    cin.getline(s, 25);
}

void getFirstName(const char s[]) {
    cout << "\n First name: " << s << endl;
}

void setLastName(char s[]) {
    cout << "\n Last name: ";
    cin.getline(s, 25);
}

void getLastName(const char s[]) {
    cout << "\n Last name: " << s << endl;
}

void setAddress(address & a) {
    cout << "\n Street: ";
    cin.getline(a.street, 25);
    cout << "\n Number: ";
    cin.getline(a.number, 6);
    cout << "\n Town: ";
    cin.getline(a.town, 25);
    cout << "\n Zip code: ";
    cin.getline(a.zip, 25);
    cout << "\n Province: ";
    cin.getline(a.province, 25);
}

void getAddress(const address & a) {
    cout << "\n Street: " << a.street << endl;
    cout << "\n Number: " << a.number << endl;
    cout << "\n Town: " << a.town << endl;
    cout << "\n Zip code: " << a.zip << endl;
    cout << "\n Province: " << a.province << endl;
}

void setBirthDate(birthdate & bd) {
    cout << "\n Day: ";
    cin >> bd.day;
    cin.get(dummy);
    cout << "\n Month: ";
    cin >> bd.month;
    cin.get(dummy);
    cout << "\n Year: ";
    cin >> bd.year;
    cin.get(dummy);
}

void getBirthDate(const birthdate & bd) {
    cout << "\n Day: " << bd.day << endl;
    cout << "\n Month: " << bd.month << endl;
    cout << "\n Year: " << bd.year << endl;
}

void setGender(char & g) {
    cout << "\n Gender: ";
    cin >> g;
    cin.get(dummy);
}

void getGender(const char & g) {
    cout << "\n Gender: " << g << endl;
}

void setPerson(person & p) {
    setFirstName(p.firstName);
    setLastName(p.lastName);
    setAddress(p.location);
    setBirthDate(p.bd);
    setGender(p.gender);
}

void getPerson(const person & p) {
    getFirstName(p.firstName);
    getLastName(p.lastName);
    getAddress(p.location);
    getBirthDate(p.bd);
    getGender(p.gender);
}

该程序运行正常,但是,在输入所有信息并提示程序显示输入的数据后,前三个字段(名字,姓氏和街道)都显示相同的输出,即街道的名称......不知何故,名字和姓氏都没有保存到各自的char数组......

例如,假设我输入A和B作为名字和姓氏,然后输入C作为街道名称,输出将是

First name: C
Last name: C
Street: C

...然后从这里开始输出正确......

编辑:顺便说一句,我没有故意使用字符串类型。因为它是面向对象的,所以它不在编程课程中,所以我坚持使用c字符串类型(字符数组或指向char的指针)

编辑2:这是methods.h文件。我自己发现了这个错误。请参阅struct person

中代码中的注释
// methods.h

#ifndef METHODS_H
#define METHODS_H

struct address {
    char street[15];
    char number[6];
    char town[15];
    char zip[6];
    char province[3];
};

struct birthdate {
    int day;
    int month;
    int year;
};

struct person {
    char firstName[25]; // error due to me omitting the array length.
    char lastName[25];  // same here, I had written char firstName[] & char lastName[]
    address location;
    birthdate bd;
    char gender;
};

// base methods
void setFirstName(char s[]);
void getFirstName(const char s[]);

void setLastName(char s[]);
void getLastName(const char s[]);

void setAddress(address & a);
void getAddress(const address & a);

void setBirthDate(birthdate & bd);
void getBirthDate(const birthdate & bd);

void setGender(char & g);
void getGender(const char & g);

void setPerson(person & p);
void getPerson(const person & p);

#endif

感谢您的所有建议!

0 个答案:

没有答案