what is wrong with my code

when i enter donald it returns duck but when i enter mickey it returns duckmouse and so with goofy..... what is wrong ???




int len;
string t;
string text;
string x[3]={"donald","mickey","goofy"};
string y[3]={"duck","mouse","dog"};
cin>>text;

for(int i=0;i<3;i++){
for(int k=0;k<=i;k++){
if(text==x[i]){
x[i]==y[k];
cout<<y[k];

}
}
}
}
closed account (E0p9LyTq)
Declare your x and y string arrays const and you will see what is wrong, you are changing the values in your x array in your loops. You don't need to do that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

int main()
{
   std::string text;
   
   const std::string x[3] = { "donald", "mickey", "goofy" };
   const std::string y[3] = { "duck", "mouse", "dog" };

   std::cin >> text;

   for (int i = 0; i < 3; i++)
   {
      if (text == x[i])
      {
         std::cout << y[i] << '\n';
      }
   }
}

And PLEASE, learn to use code tags, it makes reading your source much easier.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Thx alot XD.
Topic archived. No new replies allowed.