So I have this function named void name();. This function is supposed to open a file, have the user input a variable within that file, and close the file. The problem is, when I use the void name() function, it skips the part where the user will enter some information for the variable 'string name'. Please help.
/* In this game, there will be a ninja.
He will kill the dark evil Ninja. He will go to
New York, L.A, or Sanfrisco to search for the evil Ninja.
When he goes to the correct one, he will battle the evil Ninja.
*/
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
void name();
int main()
{
string sPlayAgain;
string *pPlayAgain;
pPlayAgain = &sPlayAgain;
string yes;
cout <<"\nWelcome to Ninja!\n" << endl;
cout <<"\nWhat is your name? Press Ctrl+Z to quit\n" << endl;
string *pName;
void name();
do{
do{
ifstream nameFile("name.txt");
string name;
nameFile >> name; //using pointer from L23
cout << "\nYour name is.. " << name << endl;
cout << "\n Is that correct..?";
cout << "\n Type Yes or No\n";
cin >> yes;
}while(yes != "Yes");
cout << "\nWould you like to play again? Please type Yes if you do. \n";
cin >> sPlayAgain;
}while(*pPlayAgain == "Yes");
return 0;
}
void name()
{
ofstream nameFile("name.txt");
if(nameFile.is_open())
{
string name;
cin >> name;
nameFile << name;
nameFile.close();
}
}
cout <<"\nWelcome to Ninja!\n" << endl;
cout <<"\nWhat is your name? Press Ctrl+Z to quit\n" << endl;
string *pName;
void name(); // this is a function prototype. This does not call the function.
do{
You do not put the return type when you want to call the function. The proper way to call it is:
He just said it, you put void in front which is a declaration, not the calling of the function. He even typed it in code what you need to replace it with. His answer is very accurate, you probably read it too fast and missed it.