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;
user1457479
  • 3
  • 1
  • 2
  • 3
    Is there any reason why you have all those ugly c-string operations in there? You hardly _try_ to use the `stringstream`. – leftaroundabout Jun 15 '12 at 00:36
  • I just needed the stringstream in order to build a string that included a char that I can increment depending on how many multiple choice options the user wanted to enter. I started out using c-strings then discovered the stringstream after. – user1457479 Jun 17 '12 at 23:01
  • I did try using the stringstream throughout, but for some reason it wasn't appending the information I wanted it to on each iteration of the for loop, while strcat gave me no problems – user1457479 Jun 17 '12 at 23:08

1 Answers1

1

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;
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • Good explanation as well. I understand! – user1457479 Jun 17 '12 at 23:26
  • @user1457479: The most important thing to learn is to always perform "error checking/handling". Many people omit checking out of laziness, etc., but when something goes wrong, it can save you a lot of pain in the future (also, don't forget to accept the answer if you think it answers the question). – Jesse Good Jun 17 '12 at 23:35
  • I actually wasn't aware that you could concatenate with strings with chars – user1457479 Jun 17 '12 at 23:38