Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8457845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:54:59+00:00 2026-06-10T12:54:59+00:00

I am creating this revese string App but i get a error if i

  • 0

I am creating this revese string App but i get a error if i include a space in the string !

#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 . 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 !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:55:00+00:00Added an answer on June 10, 2026 at 12:55 pm

    operator>> will stop reading at the first space (as David pointed out) – use getline instead

    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. His answer adds much more information that I did not mention so we are merging this into a single reply at Davids request,

    The answer by Adrian is correct, deals with the immediate issue and provides a solution. 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. 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>>. 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. You can either always read with std::getline and then parse the line, or use ignore to clear up to the newline. Finally, whenever you perform IO operations, don’t expect the operation to succeed: check the state of the stream. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have referred this for creating error message tool tips, to be displayed continuously
So i have been creating this framework thing that basically puts together source code
I am creating this link tag: <link rel=canonical href=<%= request.url %> /> The problem
I had a huge file for creating this GUI and I have shortened it
Seems I've outdone myself. All the while I was creating this pretty little 'latest
Why is this creating a new row at the bottom with this data instead
nobody loved my first question about this: Creating Entity Framework objects with Unity for
I am creating table like this: CREATE TABLE foobar (id uniqueidentifier, foo text, bar
I've been reading this post Creating a login form in CodeIgniter based on Ion
I was trying to create Hibernate Validator bean, and run into this problem creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.