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 9140459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:31:09+00:00 2026-06-17T09:31:09+00:00

I’m having a problem getting my do/while loop to function properly. This program runs

  • 0

I’m having a problem getting my do/while loop to function properly.
This program runs good the first go-around, but when i enter ‘y’ when it asks if i would like to “tell more” the program just asks the user the question (not allowing me to enter a string) and then couts the statements. What am I doing wrong? and how do i get it to function properly?

using namespace std;

int main()
{
    int i, numspaces = 0;
    char nextChar;
    string trimstring;
    string uc, rev;
    string answer;
    char temp;
    cout << "\n\n John Acosta"
    <<" Exercise 1\n"
    << "\n\n This program will take your string, count the number\n"
    << " of chars and words, UPPERCASE your string, and reverse your string.";

    string astring;
    do {
        cout << "\n\nTell me something about yourself: ";
        getline (cin, astring);

        trimstring = astring;
        uc = astring;
        rev = astring;

        for (i=0; i<int(astring.length()); i++)
        {
            nextChar = astring.at(i); // gets a character
            if (isspace(astring[i]))
                numspaces++;
        }


        trimstring.erase(remove(trimstring.begin(),trimstring.end(),' '),trimstring.end());
        transform(uc.begin(), uc.end(),uc.begin(), ::toupper);

        for (i=0; i<rev.length()/2; i++)
        {
            temp = rev[i];
            rev[i] = rev[rev.length()-i-1];
            rev[rev.length()-i-1] = temp;
        }

        cout << "\n\tYou Entered: " << astring
        << "\n\tIt has "<<trimstring.length()
        << " chars and "<<numspaces+1
        << " words."
        << "\n\tUPPERCASE: "<<uc
        << "\n\tReversed: "<<rev
        << "\n\n";


        cout<<"\n\nwant to tell me more? Enter \"y\" for YES and \"n\" for NO\n\n";
        cin>>answer;
        cout<<"\n";

    } while(answer == "y");                 //contiue loop while answer is 'y'; stop when 'n'

    {
        cout <<"\n Thanks. Goodbye!\n";     //when loop is done
    }

    return 0;
}
  • 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-17T09:31:09+00:00Added an answer on June 17, 2026 at 9:31 am

    The input operator >> works like this: First it skips whitespace, if any; Then it reads the string until it reaches the next whitespace, in your case the newline after the 'y'. This newline is then left in the stream so when you at the beginning of the loop do getline you get that newline left after the "y".

    You can remove this by using the ignore function:

    cout<<"\n\nwant to tell me more? Enter \"y\" for YES and \"n\" for NO\n\n";
    cin>>answer;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout<<"\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I want to construct a data frame in an Rcpp function, but when I
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.