简体   繁体   English

字符串反转错误?

[英]Reverse String Error?

I am creating this revese string App but i get a error if i include a space in the string ! 我正在创建此revese字符串App,但如果在字符串中包含空格,则会出现错误!

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

int main()
{
    int inputa;
    cout<<"%%%%%%%%%%%%%%%%%%String Reversing App%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl;

    cout<<"\nEnter 1 to continue and 0 to exit"<<endl<<endl;
    cin>>inputa;
    if(inputa!=0)
    {

        do
        {

            string a,c="";

            cout<<"\nEnter the string you want to Reverse : ";
            cin>>a;
            for(int x=a.length()-1; x>=0; x--)
            {
                c=c+a.substr(x,1);
            }

            cout<<"\nThe Reverse String is : "<<c<<endl;

            cout<<"\nEnter 1 to continue and 0 to exit"<<endl<<endl;
            cin>>inputa;



        }
        while(inputa!=0);
    }
//not my  home work
}

If I type the following string like "abc def" there i get an error . 如果我输入以下字符串,例如“ abc def”,则会出现错误。 But otherwise it works perfectly ! 但是,否则它完美地工作! Is there some mistake with the codes ! 代码有误吗! I am new to CPP so it would be helpful if you could help me ! 我是CPP的新手,所以如果您可以帮助我,将对您有所帮助!

operator>> will stop reading at the first space (as David pointed out) - use getline instead 运算符>>将在第一个空格处停止读取(如David指出的那样)-使用getline

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

Full edit of your code 完整编辑您的代码

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

int main()
{
    std::cout << "%%%%%%%%%%%%%%%%%%String Reversing App%%%%%%%%%%%%%%%%%%%%%%%%\n\n";

    std::cout << "\nEnter 1 to continue and 0 to exit" << std::endl;
    int inputa;
    std::cin >> inputa;
    if(std::cin && inputa!=0)
    {
        std::cin.ignore(std::numeric_limits<int>::max( ), '\n');
        do
        {
            std::string a,c;

            std::cout<<"\nEnter the string you want to Reverse : ";
            getline(std::cin, a);
            for(int x=a.length()-1; x>=0; --x)
            {
                c+=a[x];
            }

            std::cout<<"\nThe Reverse String is : " << c << std::endl;

            std::cout << "\nEnter 1 to continue and 0 to exit" << std::endl << std::endl;
            std::cin >> inputa;
            std::cin.ignore(std::numeric_limits<int>::max( ), '\n');
        }
        while(std::cin && inputa!=0);
    }
}

Including David's verbatim answer because he answered with much more detail (David Rodríguez - dribeas) - please +1 him before he deletes it. 包括David的逐字回答,因为他的回答更为详尽(DavidRodríguez-dribeas)-请先+1他,然后再将其删除。 His answer adds much more information that I did not mention so we are merging this into a single reply at Davids request, 他的回答增加了我没有提到的更多信息,因此我们应Davids的要求将其合并为一个答复,

The answer by Adrian is correct, deals with the immediate issue and provides a solution. Adrian的答案是正确的,可以解决当前的问题并提供解决方案。 As to why it enters an infinite loop, the reason is that after reading the first word, you are trying to read an integer std::cin >> inputa , which will fail as cde cannot be parsed as an integer. 至于为什么它进入无限循环,原因是在读取第一个单词之后,您尝试读取整数std::cin >> inputa ,因为cde无法解析为整数,该整数将失败。 At this point the stream enters a fail state and subsequent reads will fail without doing anything (until you clear the error state). 此时,流进入失败状态,后续读取将不做任何事情而失败(直到您清除错误状态)。

What should you do? 你该怎么办?

If you want to process whole lines, then you should use std::getline , rather than operator>> . 如果要处理整行,则应使用std::getline ,而不要使用operator>> Beware on mixing both, as operator>> won't consume the spaces after the read (including new lines) and you might just read an empty line with the next std::getline . 当心混合使用两者,因为运算符>>在读取后不会占用空格(包括换行),并且您可能只在下一个std::getline读取空行。 You can either always read with std::getline and then parse the line, or use ignore to clear up to the newline. 您可以始终使用std::getline阅读,然后解析该行,也可以使用ignore清除换行符。 Finally, whenever you perform IO operations, don't expect the operation to succeed: check the state of the stream. 最后,无论何时执行IO操作,都不要指望该操作成功:检查流的状态。 If you don't and your loop depends on IO to complete, it is quite easy to enter this sort of infinite loop, where the stream is marked as failed, no later reads succeed and you never break out of the loop. 如果不这样做,则循环取决于IO来完成,则很容易进入这种无限循环,在该循环中,流被标记为失败,以后不会读取成功,并且您也不会退出循环。

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

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