循环以使用cstring数组c ++获取用户输入

时间:2021-05-28 21:23:00

I have an assignment that requires me to write a program that prompts the user to enter the name of a student and their grade and keeps looping until they enter "quit".

我有一项任务,要求我编写一个程序,提示用户输入学生的名字和他们的成绩,并一直循环,直到他们进入“退出”。

But I can't figure out how to get the user input for the array to get the entire line (which is a first & last name so I can't just do cin >> name1[i] since theres white space) but when I use cin.getline or just getline and compile it, I get an error message saying No member function matching getline.

但我无法弄清楚如何获取数组的用户输入以获得整行(这是一个名字和姓氏所以我不能只做cin >> name1 [i],因为这是空白)但是当我使用cin.getline或只是getline并编译它,我得到一条错误消息,说没有成员函数匹配getline。

Also when I compile it without getline, its just a continuous loop and doesnt let me input any info for name or grade. I'm new to arrays and cstring so please try to dumb down where I'm messing up. Thank you.

此外,当我没有getline编译它,它只是一个连续循环,并没有让我输入任何名称或等级的信息。我是数组和cstring的新手,所以请尽量愚蠢到我弄乱的地方。谢谢。

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>

using namespace std;

int main() {

    const int CAPACITY = 50;
    string name1[CAPACITY];
    string grade[CAPACITY];
    char quit[]= "quit";
    int i;

    //for loop to get names and grades from user until quit is entered
    for (i = 0; i < CAPACITY; i++) {
        while (name1[i] != quit)
            cout << "Please input a name (or 'quit' to quit): ";
            getline(cin, name1[i]);

    //break if name1[i] = quit 
    if (name1[i].compare(quit) == 0) {
        break;
    }

    //continue loop if quit not entered and get the grade from that person
    cout << "Please input this person's grade: ";
    cin >> grade[i];
    }

    return 0;

}

2 个解决方案

#1


2  

Several issues:

  • For an array of C strings, you need char name1[50][MAXNAMESIZE];. You just declared a single string.
  • 对于C字符串数组,您需要char name1 [50] [MAXNAMESIZE] ;.你刚刚声明了一个字符串。

  • When reading into a C string, cin.getline() requires a length parameter to specify the maximum number of characters to input, so it doesn't overflow the buffer.
  • 当读入C字符串时,cin.getline()需要一个length参数来指定要输入的最大字符数,因此它不会溢出缓冲区。

  • You don't need separate loops for the names and grades. Get the grade for each student immediately after getting their name.
  • 您不需要为名称和等级分别使用循环。获得姓名后立即获得每个学生的成绩。

  • To compare C strings you have to use strcmp(), not ==.
  • 要比较C字符串,你必须使用strcmp(),而不是==。

  • When you mix >> and getline(), you need to call cin.ignore() after >> to skip past the newline. See cin and getline skipping input
  • 混合>>和getline()时,需要在>>之后调用cin.ignore()以跳过换行符。请参阅cin和getline跳过输入

Code:

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>

using namespace std;

#define MAXNAMESIZE 100

int main() {

    char name1[50][MAXNAMESIZE];
    int grade[50];

    for (int i = 0; i < 50; i++) {

        cout << "Please input a name (or 'quit' to quit): ";
        cin.getline(name1[i], sizeof name1[i]);

        if (strcmp(name1[i], "quit") == 0) {
            break;
        }
        cout << "Please input this person's grade: ";
        cin >> grade[i];
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    return 0;
}

#2


1  

Declare name1 variable as std::string, Then just use std::cin:

将name1变量声明为std :: string,然后使用std :: cin:

std::string name1;
std::cin >> name1;

But if you really need to get entire line you always can do:

但如果你真的需要获得整条生产线,你总能做到:

std::string line;
std::getline(std::cin, line);

And then use the line.

然后使用该行。

If your assignment really requires that you use cstrings you can:

如果您的作业确实要求您使用cstrings,您可以:

char line[50];
std::cin.get(line, 50);

#1


2  

Several issues:

  • For an array of C strings, you need char name1[50][MAXNAMESIZE];. You just declared a single string.
  • 对于C字符串数组,您需要char name1 [50] [MAXNAMESIZE] ;.你刚刚声明了一个字符串。

  • When reading into a C string, cin.getline() requires a length parameter to specify the maximum number of characters to input, so it doesn't overflow the buffer.
  • 当读入C字符串时,cin.getline()需要一个length参数来指定要输入的最大字符数,因此它不会溢出缓冲区。

  • You don't need separate loops for the names and grades. Get the grade for each student immediately after getting their name.
  • 您不需要为名称和等级分别使用循环。获得姓名后立即获得每个学生的成绩。

  • To compare C strings you have to use strcmp(), not ==.
  • 要比较C字符串,你必须使用strcmp(),而不是==。

  • When you mix >> and getline(), you need to call cin.ignore() after >> to skip past the newline. See cin and getline skipping input
  • 混合>>和getline()时,需要在>>之后调用cin.ignore()以跳过换行符。请参阅cin和getline跳过输入

Code:

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>

using namespace std;

#define MAXNAMESIZE 100

int main() {

    char name1[50][MAXNAMESIZE];
    int grade[50];

    for (int i = 0; i < 50; i++) {

        cout << "Please input a name (or 'quit' to quit): ";
        cin.getline(name1[i], sizeof name1[i]);

        if (strcmp(name1[i], "quit") == 0) {
            break;
        }
        cout << "Please input this person's grade: ";
        cin >> grade[i];
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    return 0;
}

#2


1  

Declare name1 variable as std::string, Then just use std::cin:

将name1变量声明为std :: string,然后使用std :: cin:

std::string name1;
std::cin >> name1;

But if you really need to get entire line you always can do:

但如果你真的需要获得整条生产线,你总能做到:

std::string line;
std::getline(std::cin, line);

And then use the line.

然后使用该行。

If your assignment really requires that you use cstrings you can:

如果您的作业确实要求您使用cstrings,您可以:

char line[50];
std::cin.get(line, 50);