c++ 我在使用getline函数[duplicate]时遇到问题

ljo96ir5  于 2023-02-01  发布在  其他
关注(0)|答案(2)|浏览(124)
    • 此问题在此处已有答案**:

cin and getline skipping input [duplicate](4个答案)
Why does std::getline() skip input after a formatted extraction?(5个答案)
昨天关闭。

#include <iostream>
using namespace std;
int main(){
    //set variables
    string name1;string name2;string name3;string n1;string n2;string n3;
    int age1; int age2; int age3;

    //get names and ages
    cout << "Who Is The First Student?"<< endl;
    getline(cin, name1); //name 1
    cout << "What is " << name1<< "'s Age?"<< endl;
    cin >> age1; // age 1
    cout << "Who Is The Second Student?"<< endl;
    getline(cin, name2); //name 2
    cout << "What is " << name2<< "'s Age?"<< endl;
    cin >> age2; //age 2
    cout << "Who Is The Third Student?"<< endl;
    getline(cin, name3); // name 3
    cout << "What is " << name3<< "'s Age?"<< endl;
    cin >> age3; //age 3

    // gets modified names
    n1 = name1.substr(2, name1.size() -3);
    n2 = name2.substr(2, name2.size() -3);
    n3 = name3.substr(2, name3.size()-3);
    // Output formatting
    cout << "Name             Age             Modified"<<endl;
    cout << name1<< "             "<<age1<<"             "<<n1<<endl;
    cout << name2<< "             "<<age2<<"             "<<n2<<endl;
    cout << name3<< "             "<<age3<<"             "<<n3<<endl;
    return 0;
}

输出询问第一个问题,即第一个学生的姓名,但输出如下:
谁是第一个学生?-约翰·多伊-约翰的年龄是多少?-19-谁是第二个学生?-约翰的年龄是多少?-
它跳过了用户输入的第二个学生的名字,并立即询问年龄,但我不知道为什么会发生这种情况,是我的代码有问题还是我有格式不正确?我相信我使用了正确的getline函数,但我可能是不正确的,并没有意识到它被跳过了一个更重要的函数。

s5a0g9ez

s5a0g9ez1#

std::string::substr()手册页是这样描述您看到的异常的:

Parameters
   pos   -  position of the first character to include
   count -  length of the substring

Exceptions
   [throws] std::out_of_range if pos > size()
vuktfyat

vuktfyat2#

该程序的主要问题是operator >>只读取一个字。
例如在这些语句中

cout << "Who Is The First Student?"<< endl;
cin >> name1; //name 1

您输入了包含两个单词"John Doe"的字符串。但operator >>只读取变量name1中的第一个单词"Jphn"
在下一个输入语句中

cout << "What is " << name1<< "'s Age?"<< endl;
cin >> age1; // age 1

由于输入缓冲器包含字"Doe"而不是数字,因此发生错误。
因此,变量name1name2name3不包含您所期望的内容。

n1 = name1.substr(2, name1.size()-3);
n2 = name2.substr(2, name2.size()-3);
n3 = name3.substr(2, name3.size()-3);

产生运行时错误。
您应该使用标准函数std::getline而不是operator >>
下面是一个基于您的程序代码的演示程序,它显示了错误的原因

#include <iostream>
#include <string>

int main()
{
    std::string name1, name2;
    int age1;

    //get names and ages
    std::cout << "Input 3 Names Below:" << std::endl;
    std::cout << "Who Is The First Student?" << std::endl;
    std::cin >> name1; //name 1
    std::cout << "What is " << name1 << "'s Age?" << std::endl;
    std::cin >> age1; // age 1
    std::cout << "Who Is The Second Student?" << std::endl;
    std::cin >> name2; //name 2

    std::cout << "name1 = " << name1 << '\n';
    std::cout << "name2 = " << name2 << '\n';
}

程序输出为

Input 3 Names Below:
Who Is The First Student?
John Doe
What is John's Age?
Who Is The Second Student?
name1 = John
name2 =

正如你所看到的,由于读取变量age1(缓冲区包含字符串"Doe")中的数据时出错,对象name2为空。

n2 = name2.substr(2, name2.size()-3);

则将发生运行时间错误。
下面是另一个演示程序,展示了如何在程序中使用标准函数std::getline

#include <iostream>
#include <string>
#include <limits>

int main()
{
    std::string name1, name2, name3;
    int age1, age2, age3;

    //get names and ages
    std::cout << "Input 3 Names Below:" << std::endl;

    std::cout << "Who Is The First Student?" << std::endl;
    std::getline( std::cin, name1 ); //name 1
    std::cout << "What is " << name1 << "'s Age?" << std::endl;
    std::cin >> age1; // age 1
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

    std::cout << "Who Is The Second Student?" << std::endl;
    std::getline( std::cin, name2 ); //name 2
    std::cout << "What is " << name2 << "'s Age?" << std::endl;
    std::cin >> age2; //age 2
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

    std::cout << "Who Is The Third Student?" << std::endl;
    std::getline( std::cin, name3 ); // name 3
    std::cout << "What is " << name3 << "'s Age?" << std::endl;
    std::cin >> age3;

    std::cout << "name1 = " << name1 << '\n';
    std::cout << "name2 = " << name2 << '\n';
    std::cout << "name3 = " << name3 << '\n';
}

程序输出为

Input 3 Names Below:
Who Is The First Student?
John Dow
What is John Dow's Age?
20
Who Is The Second Student?
Mary Poppins
What is Mary Poppins's Age?
21
Who Is The Third Student?
Bob Fisher
What is Bob Fisher's Age?
25
name1 = John Dow
name2 = Mary Poppins
name3 = Bob Fisher

相关问题