Can a string be a function? Or only chars?

I'm trying to do this

1
2
3
4
5
6
7
8
9
10
string getname();
{
    string name;
    ifstream getTheName("name.txt");

        getTheName >> name; //using pointer from L23
        return name;


}


But it doesn't seem to let me? Is there a work around for this? Or can I only use char? And if I have to use char, can you show me an example if you can?

Thank you!
try getline instead.

 
getline(getTheName, name);
Thanks William. I've found another way too, if anyone's interested.

void dialogue(string a, string b);

void dialogue(string name, string text)
{
string Name;
Name = name;
cout << endl << Name << ": " << text << endl;
}

IT WORKS ! :D
On line 1 of your original post there's wrongfully a semicolon after 'getname()'. Remove it.

And you need to #include <string> in order to make the stream work.

What has your 'dialogue' to do with your original post?
Hey coder.

Yeah, that must of been the source of my errors! Little errors like that still get me sometimes.

And, since you mentioned my dialogue and the OP, the reason for the OP was because I wanted my program to always open the .txt file in order to obtain information for the existing program.

Since I couldn't do that at that time, I just gave the information from file.txt to a local variable within int main(); (but I would of preferred having it the other way just for personal practice of input and output files).

So now, since I have the user's name in a local variable, I just included it inside this dialogue function I made to manipulate the talking of two people faster. Here's my code.

Become a Beta tester! haha jk.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* In this game, there will be a ninja.
He will kill the dark evil Ninja. He will go to
New York, L.A, or San Francisco 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>
#include <windows.h>
#include <Winuser.h>


using namespace std;

void name(); //function for the name of the user
char getName(); // gets the name of the person.
void dialogue(string name, string text); // use for talking
void keystroke(); // any key to continue
int main()
{
    string sPlayAgain;
    string *pPlayAgain;
    pPlayAgain = &sPlayAgain;
    string yes;

        Sleep(1000);
        cout <<"\nWelcome to Ninja!\n" << endl;
        Sleep(2000);






    do{

        do{

        name(); //remember to leave out "void".
        ifstream nameFile("name.txt"); //reads file
        string name;
        nameFile >> name; //using pointer from L23
        cout << "\nYour name is.. " << name << endl;
        Sleep(1500);
        cout << "\n Is that correct..?";
        cout << "\n Type Yes or No\n\n";
        cin >> yes;
        }while(yes != "Yes");

/* ************ Start with the game! ************/
string beginName;
    ifstream getTheName("name.txt");
    getTheName >> beginName;

    Sleep(1500);
    dialogue("Narrator","The city is burning. There are dead corpse everywhere...");

    dialogue(beginName,"Oh my god. I just left to go buy some groceries\n and this is what happened?");

    dialogue("Unknown","...");
    dialogue(beginName, "Who's there?!");
    dialogue("Pirate", beginName + "..cough..cough..");

    dialogue(beginName, "Oh my.. who did this to you? Was it, the dark evil ninja?");


    dialogue("Pirate", "yes..");
    dialogue(beginName, "please.. don't speak.");
    dialogue("Pirate", "I'm sorry...");
    dialogue(beginName, "Why are you sorry for?");
    dialogue("Pirate", "I couldn't stop him..");
    dialogue(beginName, "Don't worry... I'll.. i'll..!!");
    dialogue("Pirate", "*Head drops down*");
    keystroke();
    dialogue(beginName, "Nooooooooooo! PIRATE!");
    dialogue(beginName, "I must avenge my friend..time to meditate..");











/* ***********BELOW END GAME SEQUENCE************** */
    cout << "\nWould you like to play again? Please type Yes if you do. \n";
    cin >> sPlayAgain;
    if (sPlayAgain == "No")
    {
        cout <<"\n THANK YOU FOR PLAYING! \n";
    }
    }while(*pPlayAgain == "Yes");
    return 0;
}
 void name()
 {
        Sleep(1000);
        ofstream nameFile("name.txt");
        if(nameFile.is_open())
        {
            cout <<"\nWhat is your name? Press Ctrl+Z to quit\n" << endl;
            string name;
            cin >> name;
            nameFile << name;
            nameFile.close();

        }

}
void dialogue(string name, string text)
{
    string Name;
    Name = name;
    cout << endl << Name << ":\n " << text << endl;
    Sleep(3000);
}

void keystroke()
{
    int i;
    while((GetAsyncKeyState(i!=13) == -32767))
        {
            cout<< "Please press enter";
            cin.get();

        }
}


Do you have any tips on what I should fix? All i'm trying to do with this program is to implement everything I know about C++ so far



Just skimming your skeleton program here I noticed that your do / while statement for requesting to play again is flawed. You enter anything but "Yes" and it quits but if you don't enter "No" you don't get the leaving message. A better way would be to accept one character as input and say press Y or N and then uppercase it to compare to Big Y or N so it won't be case sensitive. That part should be in a while statement in itself and exited only if Y or N is pressed.

1
2
3
4
5
6
do
{
     char c;
     c = getch();
     // Code Here
}while(c != 'N' && c != 'Y')


This will help at least that part of the code run efficiently. As for the other part of the code, what in it are we looking for to help you fix?
Hi William. Thanks for the reply. Oh, I'll definetely keep that in consideration.

I've actually stopped working on that project for now because I read something

http://cplusplus.com/forum/articles/28558/

But thanks William. :)

But, I do have a problem though if you could help.

http://www.cplusplus.com/forum/beginner/52926/

I really need to figure that out :(
Last edited on
Topic archived. No new replies allowed.