0
#include <iostream> #include <string> using namespace std; string mystring1, mystring2, mystring3 = "grové"; int main(){ mystring1 = "grové"; getline( cin, mystring2 ); //Here I type "grové" (without "") cout << "mystring1= " << mystring1 << endl; cout << "mystring2= " << mystring2 << endl; cout << "mystring3= " << mystring3 << endl; return 0; } 

The output of above code is:

mystring1= grov8
mystring2= grové
mystring3= grov8

although when I cut and paste the code here it comes as:

mystring1= grovΘ
mystring2= grové
mystring3= grovΘ

Why does the content of mystring2 differ from mystring1 and mystring3?

3
  • 2
    You can simplify your code by removing mystring3 and any mentions to it, it adds nothing. Also, you're missing #include <string> Commented Feb 14, 2010 at 11:22
  • What system are you running? How are you entering (i.e. what key sequence) the é? Commented Feb 14, 2010 at 11:31
  • Related: stackoverflow.com/questions/1371012/… Commented Feb 14, 2010 at 11:34

2 Answers 2

5

Assuming you use Microsoft Windows: Your source code has a different encoding from that of the windows command line.

Type chcp in the command line to see the current console codepage. (Mine is 850)

You have three options:

  • Change the codepage/encoding of your source code to the codepage/encoding of your console.
  • Change the codepage/encoding of the console to the codepage/encoding of your source file.
  • Use a library (or Windows API) to change the encoding on the fly.
Sign up to request clarification or add additional context in comments.

Comments

0

Windows console has cp 866 (on may cyrilic) encoding and you file has other (UTF-8 or CP1251(my cyrilic)).

You may use Win API functions to solve this problem

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.