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?

Stef
  • 446
  • 1
  • 4
  • 19
  • 2
    You can simplify your code by removing `mystring3` and any mentions to it, it adds nothing. Also, you're missing `#include ` – Manuel Feb 14 '10 at 11:22
  • What system are you running? How are you entering (i.e. what key sequence) the é? – outis Feb 14 '10 at 11:31
  • Related: http://stackoverflow.com/questions/1371012/how-do-i-print-utf-8-from-c-console-application-on-windows – Manuel Feb 14 '10 at 11:34

2 Answers2

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.
Sofahamster
  • 761
  • 4
  • 6
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

den bardadym
  • 2,747
  • 3
  • 25
  • 27