Hey guys. I have a file with three names in it (first,middle,last) that I want to read from and display the full name. The problem though is that the word is not getting updated. For example, when I run the code it says: "Your first name is joe, you middle is joe, and you last is joe.
I want it to say: You first name is joe, you middle is andrew, and your last is smith.
How can I fix this? Here is the function. TY
static string promptForString(string filename)
{
ifstream infile ;
infile.open(filename.c_str()) ;
if(!infile.is_open())
{
exit(EXIT_FAILURE) ;
}
else
{
cout << endl << "The file has been located " << endl ;
}
char word[100] ;
infile >> word ;
while (infile.good())
{
cout << "Hello " << word << "You middle name is " << word << "and your last is " << word ;
infile >> word ;
}
return filename ;
}
You wrote one variable three times in one line of code. Of course all three times it will be the same.
Though are you sure the output wasn't
Your first name is joe, you middle is joe, and you last is joe
Your first name is andrew, you middle is andrew, and you last is andrew
?
Anyway, it would be best to do this without a loop.
1 2 3 4 5 6
file >> name;
cout << "Hello " << name;
file >> name;
cout << ", your middle name is " << name;
file >> name;
cout << " and your last name is " << name;
hey hamsterman. You are correct with what my output was saying. But is there a way where I could do something like this, without having to make three commands each updating the file word.
1 2 3
cout << "Hello " << word << ", your middle name is " << word << " and your last name is " << word
string getWord(ifstream& file){
string str;
file >> str;
return str;
}
int main(){
...
cout << "Hello " << getWord(file)
<< ", your middle name is " << getWord(file)
<< " and your last name is " << getWord(file);
...
}