Input of username

I have an assignment to make a code that saves your username for a game. When I write what my username is, the only thing that is written in the .txt file is "0". Can someone help me?



#include <iostream>
#include <fstream>
using namespace std;

int main () {
long long username;
cout<<"What is your name: ";
cin>>username;
ofstream myfile;
myfile.open ("register.txt");
myfile << username;
myfile.close();
return 0;
}
You are trying to input string into integer variable so nothing saved.
So how should I do it?
First you need to add #include <string> and then change long long username; for string username;.

Your code should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	string username;
	cout << "What is your name: ";
	cin >> username;
	ofstream myfile;
	myfile.open ("register.txt");
	myfile << username;
	myfile.close();
	return 0;
}


Hope this help.
You should have declared username as a string:

1
2
3
4
5
6
7
#include <string>
//...

int main(){
string username;
//...
}
Topic archived. No new replies allowed.