-6

Here is a sample program I wrote giving me the same problem.
I am trying to find where 'A', 'B', and 'C' are.

#include <iostream>
#include <sstream>
#include <string>
char a;
char b[256];
string str2 = "A";
string fileline1 = "ABC"
int i;
int x;
stringstream aa;

int main(){
    while ( i < 7 ){
        std::size_t found = fileline1.find(str2);
          if (found!=std::string::npos){
             cout << "first '" << str2 <<  "' found at: " << found << '\n';
             strcpy(b, str2.c_str());
               for ( int x=0; b[x] != '\0'; ++x ){
                    b[x]++;
              }}
             aa << b;
             aa >> str2;
             i++;
             }
}

The output is:
first 'A' found at: 0
first 'B' found at: 1
first 'B' found at: 1
...
The program never advances to C.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • You might want a new `stringstream` for each iteration of the loop. (Why on earth are you using `strcpy` when you know about `string`?) – Alan Stokes Aug 01 '14 at 20:58
  • I had to change the string into a char[] for the for loop. If you have an easier way to do this please inform me. – user3900624 Aug 01 '14 at 21:09
  • @user3900624 To iterate through the characters of a string, use *iterators*: [`string::begin`](http://en.cppreference.com/w/cpp/string/basic_string/begin). [This question](http://stackoverflow.com/questions/9438209/c-for-every-character-in-string) will give you an idea. – leemes Aug 01 '14 at 21:11
  • 1
    And there is so much more wrong about your code... Why use `stringstream` at all? It seems like you need it to convert from char array back to string after modification. Why not simply say `str2 = b`? Also, to me it looks like `str2` is always going to be a single character string. Then you don't need the loop to modify characters; simply do `str2[0]++`. And even better, don't use a string at all for the character you're searching for; use a `char` instead. `find` has an overload for `char`. – leemes Aug 01 '14 at 21:16
  • 1
    Your for loop could just be `for (auto &c : b) ++c;` – chris Aug 01 '14 at 21:21
  • `b[x]++` is perfectly legal if `b` is a `string `. – Alan Stokes Aug 01 '14 at 21:32
  • You'll find most things easier if you use local variables, with the smallest possible scope, rather than globals. – Alan Stokes Aug 01 '14 at 21:33
  • Sorry for the sloppiness of the code. I wrote this fast to explain my problem better but I guess it did not do that. Thank You @leemes and Alan Stokes for your help. – user3900624 Aug 01 '14 at 21:46

1 Answers1

0

Moving the line

stringstream aa;

just before the line

         aa << b;

solves the problem for me.

This is perhaps caused by use of aa both as an input stream as well as output stream. Not sure of the details.

Update

Here's your program with a bit of error checking code thrown in.

#include <iostream>
#include <sstream>
#include <string>
#include <string.h>

using namespace std;

char a;
char b[256];
string str2 = "A";
string fileline1 = "ABC";
int i;
int x;
stringstream aa;

int main()
{
   while ( i < 7 )
   {
      std::size_t found = fileline1.find(str2);
      if (found!=std::string::npos)
      {
         cout << "first '" << str2 <<  "' found at: " << found << '\n';
         strcpy(b, str2.c_str());
         for ( int x=0; b[x] != '\0'; ++x )
         {
            b[x]++;
         }
      }

      aa << b;
      if ( !aa.good() )
      {
         cout << "The stringstream is not good.\n";
      }

      aa >> str2;
      if ( aa.eof() )
      {
         cout << "The stringstream is at eof.\n";
         aa.clear();
      }
      if ( !aa.good() )
      {
         cout << "The stringstream is not good.\n";
      }
      i++;
   }
}

Output

first 'A' found at: 0
The stringstream is at eof.
first 'B' found at: 1
The stringstream is at eof.
first 'C' found at: 2
The stringstream is at eof.
The stringstream is at eof.
The stringstream is at eof.
The stringstream is at eof.
The stringstream is at eof.

Without the block

      if ( aa.eof() )
      {
         cout << "The stringstream is at eof.\n";
         aa.clear();
      }

The state aa of was such that it wasn't doing anything with the lines:

      aa << b;
      aa >> str2;

Hence, str2 was not being changed at all.

R Sahu
  • 204,454
  • 14
  • 159
  • 270