简体   繁体   English

在弦上使用cin和getline

[英]Use of cin and getline for strings

I was recently doing a problem in C++ : 我最近在C++遇到问题:

Write a program to work out if a series of 5 digits are consecutive numbers. 如果一系列的5位数字是连续的数字,请编写程序进行计算。 To make this easier, assumes the digits are a string: 为了简化此过程,假设数字是一个字符串:

string numbers = "10-9-8-7-6"; 字符串数字=“ 10-9-8-7-6”;

Make sure your code works for the following sequence, as well: 确保您的代码也按以下顺序工作:

string numbers = "1-2-3-4-5"; 字符串编号=“ 1-2-3-4-5”;

I solved it, however I saw when I used cin for the string the console window threw some exception & didn't execute the program but on replacing it with getline , it worked perfectly. 我解决了它,但是我看到当我使用cin作为字符串时,控制台窗口引发了一些异常并且没有执行该程序,但是在用getline替换它时,它可以正常工作。

Could anyone explain me the reason behind it because logically both should work properly. 任何人都可以向我解释其背后的原因,因为从逻辑上讲两者都应该正常工作。

The program is: 该程序是:

#include<iostream>
#include<string>

using namespace std;

void change(int x, int y, int &inc, int &dec)
{
    if (x - y == 1)
        ++dec;
    else if (y - x == 1)
        ++inc;
}

int main()

{
    string s = "", snum = "";
    cout << "enter 5 nos and use \'-\' to separate them: ";
    cin >> s;
    int i = 0, x = 0, y = 0, inc = 0, dec = 0;
    for (char &ch : s)
    {
        if (ch == '-')
        {
            ++i;
            if (i == 1)
            {
                y = stoi(snum);
                cout << y << endl;
            }
            else
            {
                x = y;
                y = stoi(snum);
                cout << x << " " << y << endl;
                change(x, y, inc, dec);
            }
            snum = "";
        }
        else
            snum += ch;
    }
    x = y;
    y = stoi(snum);
    cout << x << " " << y << endl;
    change(x, y, inc, dec);
    if (inc == 4 || dec == 4)
        cout << "ORDERED";
    else
        cout << "UNORDERED";
    return 0;

}

If you have to enter everything at the same time such as: 如果您必须同时输入所有内容,例如:

10 9 8 7 6 10 9 8 7 6

All on one line then cin does not record all that at the same time. cin不会同时记录所有内容。 Concerning cin it only takes the characters before a space (" ") for example. 关于cin它仅以空格(“”)为例。 Getline however takes that entire line and uses it. 但是, Getline会占用整个行并使用它。 Another way to do the same thing would be to use the cstdio library and have it set up with either using printf or puts to prompt, and then use gets to gather all the information from the puts prompt. 另一种执行相同操作的方法是使用cstdio library并使用printfputs提示设置它,然后使用gets从puts提示收集所有信息。 This is what I assume why it works. 这就是我认为它起作用的原因。

Example: 例:

cstdio library cstdio库

char string[50];
printf("Enter a string of text");
gets(string);
cout << string << endl;

*EDIT *编辑

After the comment below I realized what you are asking, and if you are assuming the numbers are strings, and they are separated with hyphens and no spaces then it should work fine. 在下面的评论之后,我意识到了您的要求,并且如果您假设数字是字符串,并且将它们用连字符分隔并且没有空格,那么它应该可以正常工作。 It shouldn't be the problem of cin by maybe something else? cin可能不是其他问题吗?

If there are spaces involved in your code then what I wrote above EDIT will be a simple solution to THAT problem. 如果您的代码中包含空格,那么我在EDIT上面写的内容将是该问题的简单解决方案。

If you need to get a formatted string, I recommend you scanf like this: 如果需要获取格式化的字符串,我建议您使用scanf如下所示:

if( 5 == scanf("%d-%d-%d-%d-%d", &a, &b, &c, &d, &e) )
      //welldone
      // work with above 5 int easily :)
else
      // Please enter again 

This way you don't have to work with string at all and life would be easier. 这样,您根本就不需要使用字符串,生活会更轻松。 You can easily check if these 5 are consecutive or not . 您可以轻松地检查这5个是否连续。

If you need not a new solution and want to get your code fixed, tell me in comment. 如果您不需要新的解决方案并希望修复您的代码,请在注释中告诉我。

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

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