vendredi 27 novembre 2015

problems when read file in CLion (JetBrain)

I just install CLion and try to play around with it. But I meet some problems when reading a file and the console doesn't display as I expected, this is my code.

#include <iostream>
#include <fstream>

using namespace std;

void readFile(const char *fileName);

int main() {
    readFile("test.txt");
}

void readFile(const char *fileName) {
    ifstream file(fileName);
    string s1;
    file >> s1;
    cout << "s1 = " << s1;
}

and here is my file: test.txt

hello

this is the result:

C:\Users\hongn\.CLion12\system\cmake\generated\85e35c99\85e35c99\Debug\Test.exe
s1 =
Process finished with exit code 0

When I try:

int a;
cout << "a = ";
cin >> a;
cout << "a is: " << a;

this is the result:

C:\Users\hongn\.CLion12\system\cmake\generated\85e35c99\85e35c99\Debug\Test.exe
a =3
a = 3
a is: 3
Process finished with exit code 0

After I typed 3, the row "a = 3" appear. It is not the expected result, so redundant, why the line "a = 3" appear 2 times? How can I fix this? Thank you for your help!

Aucun commentaire:

Enregistrer un commentaire