-3

I have written the code below. The matter is this: after I read the x and y from input stream there is not matter - mean x and y are the exact value of input - but later they change to some other value.

what's the matter? I can not understand it!!

int count(char s[], char ss[] , long long int posF, long long int posE){}
int main()
{
    char s[]{};
    int q = 0;
    cin >> s;
    cin >> q;
    int choise = 0;
    while(q--)
    {
        cin>>choise;
        if(choise == 1)
        {
            int x = 0;
            cin>>x;
            char c;
            cin>>c;
            s[x-1] = c;
        }
        else if(choise == 2)
        {
            int x = 0;
            int y = 0;
            cin>>x>>y;
            //Fist LOG
            cout<<"First log x and y are correct    "<<x<<"  "<<y<<endl;
            char ss[]{};
            cin>>ss;
            //Second LOG
            cout<<"Second log x and y are  wrong?Why?"<<x<<"  "<<y;
            cout<<count(s, ss, x-1, y-1)<<endl;
        }

    }
    return 0;
}
Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
  • 11
    `char s[]{};` and `char ss[]{};` are not legal c++. Even if your compiler offers extensions allowing it, the lines `cin >> s;` and `cin>>ss;` will almost certainly be undefined behavior. – François Andrieux Jul 12 '17 at 17:16
  • Answer is simple: Undefined behavior. Most likely due to referencing items out of bounds of your arrays. – Algirdas Preidžius Jul 12 '17 at 17:17
  • 4
    I dont know what you're trying to do with `char ss[]{}; cin>>ss;` but im pretty sure its wrong. – Borgleader Jul 12 '17 at 17:17
  • 2
    `>>` to a `char` array is risky at the best of times, and a zero length array is about the worst of times.There is no overflow protection. – user4581301 Jul 12 '17 at 17:19
  • @Ron when i delete {} it says me that (local incomplete array must be initialize ) – Mohammad Abdollahzadeh Jul 12 '17 at 17:21
  • 5
    @m1350 Sounds like you could benefit from reading one of [these C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Jul 12 '17 at 17:22
  • @Borgleader when I comment cin>>ss; it becomes ok what should i do? – Mohammad Abdollahzadeh Jul 12 '17 at 17:24
  • 7
    @m1350: Programming by guessing doesn't work. You are going to have to take a break for learning the language from one of [these C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Lightness Races in Orbit Jul 12 '17 at 17:25
  • 1
    @LeonardoAlvesMachado [You sure about that?](http://ideone.com/BblXXD) The OP is confused enough as it is. – WhozCraig Jul 12 '17 at 17:26
  • 1
    @LeonardoAlvesMachado Even if your suggest's syntax was valid, the proposed solution is overly complex and not robust. For c++ questions, it would be better to stick with idiomatic solutions, such as using `std::string` to store strings. – François Andrieux Jul 12 '17 at 17:31
  • Replace `char []` with `std::string`. Replace `cin >> ss` with `std::getline(cin, string_variable)`. You could also read single words with `cin >> string_variable;`. – Thomas Matthews Jul 12 '17 at 17:57
  • Use a `switch` instead of an `if-else` ladder. For example, your ladder is missing a final `else` clause. What happens when the User enters `4` as a *choise*? – Thomas Matthews Jul 12 '17 at 17:59

1 Answers1

1

As the other said the problem of your code is char ss[]{};cin>>ss; so if you comment if you will understand it

so my suggestion is to use string instead of char[] and you can use cin<< for string and you can use [] operator e.x

string s = "Code";
cout<<s[0];

and M.r @Ron is correct it's better to read This Books