cin won't work in function???

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* 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>


using namespace 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();
        }

 }


Thank you.
You're not calling the function.

Line 28:

1
2
3
4
5
6
7
8
9
        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:

 
name();  // <- no "void" 

Thanks Disch.

But Disch, actually, I prototyped the function at the beginning before int main().
I wrote that after int main. Sorry if it's kind of messy.

And I've tried taking out the void and its still the same. The function skips the part where it asks the user for input.

Do you know what the problem could be? I mean, I don't know why it doesn't work.

Thanks again :D
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.
Oh!! my bad. Yeah, I read it too fast.
After reading it too fast, I went to change that function to "void();".

My bad!

Thanks guys :)
Topic archived. No new replies allowed.