Class: Setting name in console

I have this code here but I would like to know how to set the name,lastname and phone in the console itself instead of in the code.

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
#include <string>
#include <iostream>
using namespace std;

class kontakt {
	string name, lastname, phone;
public:
	void set_values(string, string, string);
	string returnname() { return name; }
	string returnlastname() { return lastname; }
	string returnphone() { return phone; }
};

void kontakt::set_values(string x, string y, string z) {
	name = x;
	lastname = y;
	phone = z;
}


int main() {

	kontakt kontakt;
	kontakt.set_values("sven", "svensson", "070-0000000");
	cout << "Name: " << kontakt.returnname() << endl;
	cout << "Lastname: " << kontakt.returnlastname() << endl;
	cout << "Phonenumber: " << kontakt.returnphone() << endl;

	return 0;
}
Last edited on
Use std::cin to get input from the console.
1
2
3
std::string first_name;
std::cout << "Enter your first name: ";
std::cin >> first_name;


You may want to consider using std::getline(), in case any of the names have a space in them.
http://www.cplusplus.com/reference/string/string/getline/
Last edited on
Topic archived. No new replies allowed.