HELP! getline() confuse me

Excuse my english. I am trying to write a code that a computer will ask me to type my first name and last name then after that the computer will ask me again to print my whole name using getline() and after printing my whole name it will print the programming arithmethic. Thank you

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

int main()
{
	int x;
	string full;
	string first;
	string last;
	
	cout << "First name: "; // prints first name
	cin >> first;
	cout << "Last name: "; // prints last name
	cin >> last;

	cout << "Your name is: " << first + " " + last << endl;

	
	cout << "Enter your full name: "; 
	getline(cin, full); // prints whole line

	cout << "Your whole name is: " << full << endl;

        // prints arithmethic
	int a = 45;
	int s = 56;
	x = a + s;

	cout << "a + s = " << a + s << endl;
	cout << "a + s = " << a - s << endl;
	cout << "a + s = " << a * s << endl;
	cout << "a + s = " << a / s << endl;
	
	return 0;
	
}
i don't understand, what you're trying to do?
Here is your code fixed. Study it...

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

int main()
{
	int x;
	string full;
	string first;
	string last;
	
	cout << "First name: "; // prints first name
	cin >> first;
	cout << "Last name: "; // prints last name
	cin >> last;

	cout << "Your name is: " << first + " " + last << endl;

	cin.ignore();
	cout << "Enter your full name: ";
	
	getline(cin, full); // prints whole line

	cout << "Your whole name is: " << full << endl;

        // prints arithmethic
	int a = 45;
	int s = 56;
	x = a + s;

	cout << "a + s = " << x << endl;
	cout << "a - s = " << a - s << endl;
	cout << "a * s = " << a * s << endl;
	cout << "a / s = " << a / s << endl;
	
	cin.get(); //to pause the screen. press enter to continue.
	return 0;
	
}


The last division problem results in zero, because integers won't show a decimal value. Good luck.
Last edited on
Topic archived. No new replies allowed.