Why does it print the name two times?

时间:2018-03-25 19:06:08

标签: c++

I have a file containing the sentence "Hallo, my name is Max". I have tried printing it on the Command Prompt, it printed "Hallo, my name is Max Max". What I don't understand is why did it print "Max" two times and not one?

#include<iostream>
#include<string>
#include<fstream>

int main(){
    std::string str;
    std::ifstream myfile("text.txt");
    while(myfile){
    myfile >> str;
    std::cout <<str<< " ";
    }
    myfile.close();
    return 0;
}

0 个答案:

没有答案
相关问题