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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:48:50+00:00 2026-05-27T16:48:50+00:00

I am using C++ and writing a program that is supposed to do a

  • 0

I am using C++ and writing a program that is supposed to do a bunch of stuff with primes. However the main issue is that I am having trouble converting in between ints and strings. I believe the following is the relevant code:

for(int j=0;j<size-1;j++){
  num=primes[j];
  ss<<num;
  ss>>temp;
  ss.str("");
  for (int count=0; count < temp.size(); count++) {
      cout<<temp<<endl;
  }

I know that I could Google and figure out how to convert from an integer another way. However, I have a feeling that the reason I can’t figure out what is going wrong is because I’m lacking some fundamental knowledge about stringstreams which I’m not aware of which I’m hoping can be fixed. num is an int and ss is a stringstream and cout temp is printing out 2 every single time, which is the value of primes[0]. I think the stringstream might be not reading after the first trial because of something to do with a newline character but I don’t really know.

  • 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-05-27T16:48:50+00:00Added an answer on May 27, 2026 at 4:48 pm

    The reason for what you are experiencing is that the EOF_BIT will be set in ss after reading the first value into temp, after that no read/writes can be made to the std::stringstream and therefore temp is not updated with a new value.

    A more human readable way of explaining the above; the std::stringstream ss will think that it has reached the end (which it has, at one point). You’ll need to tell it to "start all over again" (reset all error-flags) for it to be usable in another iteration.


    How do I solve this issue?

    There are a few methods available, to me the most clear (in code readability) is to use a new std::stringstream for each iterator in your loop (see "Example solution #2).

    Check out the snippets below that all will output:

    2
    3
    5
    7
    11
    13
    17
    

    Example solution #1

      int const PRIMES_SIZE         = 7;
      int const primes[PRIMES_SIZE] = {2,3,5,7,11,13,17};
    
      std::stringstream ss; 
      std::string temp;
    
      for (int i =0; i < PRIMES_SIZE; ++i) {
        ss << primes[i];
        ss >> temp;
    
        std::cout << temp << std::endl;
    
        ss.clear (); // unset error flags
      }
    

    Example solution #2

      int const PRIMES_SIZE         = 7;
      int const primes[PRIMES_SIZE] = {2,3,5,7,11,13,17};
    
      for (int i =0; i < PRIMES_SIZE; ++i) {
        std::stringstream ss;
        ss << primes[i];
    
        std::cout << ss.str () << std::endl;
      }
    

    Example solution #3

      #include <iterator>
    
      ...
    
      int const PRIMES_SIZE         = 7;
      int const primes[PRIMES_SIZE] = {2,3,5,7,11,13,17};
    
      std::stringstream ss;
    
      std::copy (primes, primes+PRIMES_SIZE, std::ostream_iterator<int> (ss, "\n"));
    
      std::cout << ss.str ();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program that is supposed to just calculate the months between
I am writing a program that generates excel reports, currently using the Microsoft.Interop.Excel reference.
I am writing a python program that lauches a subprocess (using Popen). I am
I'm writing a Cocoa OS X (Leopard 10.5+) end-user program that's using timestamps to
I am writing a program that creates a stack using linked lists. I have
I'm writing a program which is supposed to read two strings that can contain
I am writing a program that is supposed to help me learn about enumeration
I'm writing a program that fills in a pdf template (using fpdf) then captures
I'm writing a program using JOGL/openCL to utilize the GPU. I have code that
I tried writing a program in Java using regex to match a pattern and

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.