简体   繁体   English

程序在读取所有结构成员之前终止

[英]Program terminates before reading all struct members

The following is the code. 以下是代码。 It contains a struct student with int rno and string name as members. 它包含一个以int rno和字符串名称为成员的struct 学生 I use a loop to read the members of all student variables. 我使用循环读取所有学生变量的成员。 But the program terminates as soon as i enter any letter. 但是,只要我输入任何字母,程序就会终止。 Also, the string entered is not displayed. 另外,输入的字符串也不会显示。

#include <iostream>
#include <string>

using namespace std;
int main() {
    struct student {
        int rno;
        string name;
    };

    student s[4];

    int i;
    for( i = 0; i < 4; ++i) {
        cin >> s[i].rno;
        getline( cin, s[i].name );
    }

    string line = "";
    for( i = 0; i < 80; ++i) line += '-';

    cout << line << "ROLL\tNAME\n" << line << '\n';

    for( i = 0; i < 4; ++i) {
        cout << s[i].rno << '\t' << s[i].name << '\n';
    }
}

Thank you. 谢谢。

Your problem is using the >> stream operator and then using getline without flushing the stream of any unwanted new line characters. 您的问题是使用>>流运算符,然后使用getline而不刷新任何不需要的换行符的流。

Then when you read in the next struct you'll have the string you wanted to be the previous elements name and your stream will likely fail to read in an integer (unless the name started with a number). 然后,当您读取下一个结构时,您将拥有想要作为先前元素名称的字符串,并且流可能无法以整数读取(除非名称以数字开头)。

So your read in loop should use cin.ignore(MAX_INT,'\\n') or instead of MAX_INT some value longer than any line you would expect. 因此,您的读入循环应使用cin.ignore(MAX_INT,'\\n')或代替MAX_INT某些值长于您期望的任何行。

cin.ignore will ignore the next X characters or until it reaches the specified character (in this case MAX_INT character or until it reaches the newline character '\\n' ). cin.ignore将忽略接下来的X个字符,或者直到它到达指定的字符(在这种情况下为MAX_INT字符,或者直到到达换行符'\\n' )为止。

Your read in for loop would then look like: 您的for循环读操作将如下所示:

for (i = 0; i < 4; ++i){
    cin >> s[i].rno;
    cin.ignore(MAX_INT, '\n');
    getline(cin, s[i].name);
}

try to add cin.ignore() after get an int type and before get char type 尝试在获取int类型之后并在获取char类型之前添加cin.ignore()

and for getting char array it's better to use gets() 为了获得char数组,最好使用gets()

#include <iostream>
#include <string>
using namespace std;

struct student {
    int rno;
    string name;
};

int main() {
    student s[4];
    int i;
    for( i = 0 ; i < 4 ; ++i) {
        cin >> s[i].rno;
        cin.ignore();
        gets(s[i].name);
    }
    string line = "";
    for( i = 0 ; i < 80 ; ++i) line += '-';
    cout << line << "ROLL\tNAME\n" << line << '\n';
    for( i = 0 ; i < 4 ; ++i)
        cout << s[i].rno << '\t' << s[i].name << '\n';
}

cin leaves the newline character in the stream. cin将换行符留在流中。 Adding cin.ignore() to the next line clears/ignores the newline from the stream. cin.ignore()添加到下一行将清除/忽略流中的换行符。
This is used mainly with combinations of cin and getline. 这主要用于cin和getline的组合。

see documentation and this question too 参见文档和这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM