-6

I have a little problem with compression of two characters.

for(int i=0; i<initializer.size(); i++)
{
    char letter;
    letter = initializer[stringIter];
    if(letter == '+')
    {
        std::cout << "+";
    }
    else if(letter == '-')
    {
        std::cout << "-";
    }
    else if(letter == 'F')
    {
        std::cout << "F";
    }
    else
    {
        std::cout << letter << " UNKNOWN";}
    }
    initializer = F + F - F - F + F
}

And when I'm trying to print the character, I have a little question mark instead of the real character, and almost every character is unknown.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Menos
  • 361
  • 2
  • 7
  • 5
    shouldn't it be `initializer[i]`? Or add the declaration of `stringIter` – WorldSEnder Jul 10 '15 at 16:57
  • 3
    Perfect time to learn [how to use a debugger](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and step through the code so you can see exactly what's going on. – Captain Obvlious Jul 10 '15 at 17:04
  • stringIter is i;) Its the same value. – Menos Jul 10 '15 at 17:19
  • 2
    Where is `stringIter` incremented in the loop? I don't see it declared as a reference to `i`, so how can it be the same? – Thomas Matthews Jul 10 '15 at 19:20
  • The code as posted will not compile, even if embedded in something else: There are 5 `{`s and 6 `}`s (the [curly bracket](https://en.wikipedia.org/wiki/Bracket) (`{`}s)) are not balanced). Something has probably been left out (and equally badly formatted). – Peter Mortensen May 23 '23 at 18:10

1 Answers1

-1

This way will work:

#include <iostream>
#include <string>

int main()
{
    std::string initializer = "F+F-F-F+F";
    for(int i=0; i<initializer.size(); i++)
    {
        char letter;
        letter = initializer[i];
        if(letter == '+')
        {
            std::cout << "+";
        }
        else if(letter == '-')
        {
            std::cout << "-";
        }
        else if(letter == 'F')
        {
            std::cout << "F";
        }
        else
        {
            std::cout << letter << " UNKNOWN";
        }
    }
}

debugging shows that he found a \342 \210 \222 char as letter. But why?...

So what’s the problem? It is really chars.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lispsil
  • 411
  • 3
  • 8
  • Lolz.................................. it's function not in main and initializer is passing to it. I have wrote like this to show its value..... – Menos Jul 10 '15 at 17:18
  • And so? I have fixed your iterator mistake. If you insert this code into notmain function it will work. – lispsil Jul 10 '15 at 17:23
  • Watch to this line: letter=initializer[i]; – lispsil Jul 10 '15 at 17:24