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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:06:54+00:00 2026-06-06T00:06:54+00:00

I am not sure what the issue is with my code. I would like

  • 0

I am not sure what the issue is with my code. I would like for the character to be updated and inserted into the stringstream on each iteration of the for-loop and extracted to a string that I can later use to append to a char[] variable. The output I wish to receive for the variable content is:
What does CPU stand for?
A. Central Processing Unit
B. Control Programming Unit
C. Central Proramming Unit
D. Control Processing Unit
Instead I get all A’s down the line. How do I update the value in the stream so that temp takes on the values “A.”, “B.”, “C.”, and “D.”. I am not new to C++ but I am new to using stringstream. Can anyone explain what is happening and how I may be able to work around it? I am using a g++ compiler in a Unix environment.

    char content[1096];
    int numOfChoices;
    char letterChoice = 'A';
    string choice, temp;
    stringstream ss;

    strcpy ( content, "What does CPU stand for?");
    cout << "How many multiple choice options are there? ";
    cin >> numOfChoices;
    cin.ignore(8, '\n'); 
    for (int i = numOfChoices; i > 0; i--)
    {
       strcat (content, "\n");
       ss << letterChoice << ".";
       ss >> temp;
       strcat(content, temp.c_str());
       ss.str("");
       cout << "Enter answer for multiple choice option " 
            << letterChoice++ <<":\n--> ";
       getline (cin, choice);
       strcat(content, " ");
       strcat(content, choice.c_str());
     }
       cout << content << endl;
  • 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-06T00:06:55+00:00Added an answer on June 6, 2026 at 12:06 am

    When you perform insertion and extraction, you should always check whether it was successful or not:

    Example

    if (!(ss << letterChoice << "."))
    {
        cout << "Insertion failed!" << endl;
    }
    

    That way, you can know right away that something went wrong.
    In the first loop, when you do ss >> temp; it extracts all the characters in the stream and puts them into temp. However, the end of file is reached so the eofbit gets set. So, on the next loop when you do ss << letterChoice << ".";, the operation fails because the eofbit is set. If you add an ss.clear(); after ss >> temp; the code will work because you reset the stream state after eofbit is set.

    However, you don’t need stringstream or all those old C functions in your code. You could do everything with std::string like so:

    string content = "";
    int numOfChoices;
    char letterChoice = 'A';
    string choice;
    
    content += "What does CPU stand for?";
    cout << "How many multiple choice options are there? ";
    cin >> numOfChoices;
    cin.ignore(8, '\n'); 
    for (int i = numOfChoices; i > 0; i--)
    {
       content += "\n";
       content += letterChoice;
       content += ".";
       cout << "Enter answer for multiple choice option " 
            << letterChoice++ <<":\n--> ";
       getline (cin, choice);
       content += " ";
       content += choice;
     }
     cout << content << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing one issue and I am not sure if what i would
I'm not sure if this is a known issue that I am running into,
I'm having the craziest issue with Firefox. I'm not sure if this issue is
I'm not sure if this is a simulator issue, but I don't remeber having
I'm not sure if I have a threading issue here or not. On the
We are getting a weird issue on which we are not sure what exactly
I am getting an error message, and I not quite sure where the issue
I need help with LINQ syntax or methodology, not sure which. Here's my issue:
I'm not quite sure how to approach this issue: I am creating a web
I'm not sure what I should be searching to figure out this issue, so

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.