Search code examples
c++stringcharcstringstringstream

How to insert a new character in a StringStream and extract to string within a loop in C++?


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;

Solution

  • 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;