Making questions where user inputs words and integers.

So I'm having some issues making a questionnaire type program

here is my 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
32
33
#include "stdafx.h"
#include <iostream>
#include <string>
#include<fstream>
using namespace std;


int main()
{
	int a, b;
	a = 2;
	b = 5;
	cout << "The variable 'a' is \n" << a<<endl;
	cout << "the variable 'b' is \n" << b;
	cout << "\nso a + b = " << a + b<<endl;
	cout << "What was your name again?\n";
	string name;
	int c;
	string state;
	getline(cin, name);
	cout << "what year were you born in?\n";
	cin >> c ;
	cout << "and what state were you born in?\n";
	getline(cin, state);
	cout << "so your name is " << name << " and you were born in the year, " << c << " and you were born in the state of "<<state<<endl;
	cout << "What year is it?";
	int d;
	cin >> d;
	cout << "you were born " << d - c << " years ago\n";
    return 0;
}



Output
1
2
3
4
5
6
7
8
9
10
11
12
13
The variable 'a' is
2
the variable 'b' is
5
so a + b = 7
What was your name again?
jemeu
what year were you born in?
1990
and what state were you born in?
so your name is jemeu and you were born in the year, 1990 and you were born in the state of
What year is it?2020
you were born 30 years ago


So I think I understand getline (cin,input) but I'm not understanding where I'm using
1
2
int c
cin>> c;
and how it's going to the next line right away. I'm hoping to get a closer age with questioning the month and day, but I'm just learning how to do with what I'm learning so I don't move too fast forward. thanks
Last edited on
Mixing std::getline and std::cin causes input problems. std::cin leaves the endline ('\n') character in the input stream, std::getline removes it.

Your second std::getline at line 24 'sees' the endline in the stream. std::cin::ignore removes the endline.

https://en.cppreference.com/w/cpp/io/basic_istream/ignore

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
#include <iostream>
#include <string>

int main()
{
	int a, b;
	a = 2;
	b = 5;

	std::cout << "The variable 'a' is " << a << '\n';
	std::cout << "the variable 'b' is " << b << '\n';
	std::cout << "so a + b = " << a + b << "\n\n";

	std::cout << "What was your name ? ";
	std::string name;
	std::getline(std::cin, name);

	std::cout << "what year were you born in? ";
	int birth_year;
	std::cin >> birth_year;
	
	// ignore all the characters in the stream until the endline, extracting that
	std::cin.ignore(32667, '\n');

	std::cout << "and what state were you born in? ";
	std::string state;

	// mixing std::getline and std::cin cause input problems
	std::getline(std::cin, state);
	std::cout << '\n';

	std::cout << "So your name is " << name << " and you were born in the year, " << birth_year
		<< "\nand you were born in the state of " << state << "\n\n";

	std::cout << "What year is it? ";
	int current_year;
	std::cin >> current_year;
	std::cout << "you were born " << current_year - birth_year << " years ago\n";

}


Topic archived. No new replies allowed.