简体   繁体   English

Getline无法正常工作

[英]Getline doesn't work as expected

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

int main()
{
    int order[5];
    string carorder[5];
    int smallest=999999, where;
    string carname[5];
    float carprice[5];
    cout << "Enter car names then prices: ";
        cout << endl;
    for(int i=0; i < 5; i++){
        cin >> carname[i];
        //getline(cin, carname[i]);     can't do this -- why?
        cout << endl;
        cin >> carprice[i];
        cout << endl;
    }
    //BAD ALGORITHM//
       for(int m=0; m<5; m++){
    for(int j=0; j < 5; j++){

        if(carprice[j] < smallest){
            smallest = carprice[j];
            where = j;
        }

    }
    order[m] = smallest;
    carorder[m] = carname[where];
    carprice[where] = 999999;
    smallest = 999999;

   }
   //////////////////////////
    for(int w=0;  w<5; w++){
        cout << endl << "The car: " << carname[w] << " and price: " << order[w];
    }
    //////////////////////////
    return 0;
}

I'm doing an exercise in c++ and it's supposed to take a car and its price, then return the prices in order from lowest to highest. 我正在用C ++进行练习,它应该乘汽车及其价格,然后按从最低到最高的顺序返回价格。 The challenge is to use the algorithm given by the professor, so please don't mind that part (I think it's bad algorithm). 挑战在于使用教授给出的算法,所以请不要介意这部分(我认为这是不好的算法)。 I need to know why can't I use getline(cin, carname[i]); 我需要知道为什么不能使用getline(cin,carname [i]); and cin >> carname[i]; 和cin >> carname [i]; works fine. 工作正常。 I also tried using cin.clear(); 我也尝试使用cin.clear(); and cin.ignore(); 和cin.ignore(); before the getline, still doesn't work. 在热线之前,仍然无法正常工作。 Any help is appreciated. 任何帮助表示赞赏。

Formatted input, ie, using operator>>() , will skip leading whitespace. 格式化输入,即使用operator>>() ,将跳过前导空格。 In addition, it will stop when a character is received which doesn't fit the format. 另外,当接收到不适合格式的字符时,它将停止。 For example, when reading a float , the input will read a number and stop upon the first whitespace received. 例如,当读取float ,输入将读取一个数字并在接收到第一个空格时停止。 For example, it will stop right before a newline used to enter the current value. 例如,它将在用于输入当前值的换行符之前停止。

Unformatted input, eg, using std::getline() , won't skip leading whitespace. 未格式化的输入(例如,使用std::getline()不会跳过前导空格。 Instead, it will happily read whatever character is waiting to be read. 取而代之的是,它将愉快地读取任何等待读取的字符。 For example, if the next character is a newline, std::getline() will happily stop reading right there! 例如,如果下一个字符是换行符,则std::getline()会很乐意在该处停止阅读!

Generally, when switching from formatted to unformatted input, you want to get rid of some of the whitespace. 通常,当从格式化输入切换为非格式化输入时,您希望摆脱一些空白。 For example, you can skip all leading whitespace with the std::ws manipulator: 例如,您可以使用std::ws操纵器跳过所有前导空格:

std::getline(std::cin >> std::ws, carname[i]);

Your inputs go entirely unchecked: not checking the result before using it is generally a bad idea! 您的输入完全未经检查:使用前不检查结果通常是个坏主意! You should probably test the stream state at some point and possible restore it to good state, asking for correctly formatted data. 您可能应该在某个时刻测试流状态,并可能将其恢复到良好状态,以请求正确格式化的数据。

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

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