Inputs and conditions

I'm trying to make the conditions based on the inputs of age and vaccine status, but it doesnt seem to work. and also if the age input is two digits its skip the country input and goes to vaccine. I hope I can get some assistance with this.

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

int main()
{
  std::cout << "\n Please provide the following information:\n\n"
            << "   Name: ";

  std::string Name;
  std::cin >> Name;

  std::cout << "   Age: ";

  uint8_t Age;
  std::cin >> Age;

  std::cout << "   Country: ";

  std::string Country;
  std::cin >> Country;

  std::cout << "   Vaccinated?: ";

  std::string Vaccine;
  std::cin >> Vaccine;
  if(Age >= 12 && Vaccine == "yes") {
  	std::cout << "   Travel Pass Issued   ";
  } else {
  	std::cout << "   Travel Pass Denied   ";
  }
}
There's a couple of points re input of which you need to be aware:

1) stream extraction >> ignores any initial white space chars (space, tabs, newline) and only gets input up to the first white space char and doesn't extract the white space

2) To input a string which might contain a space (or tab) then std::getline() is used. However this doesn't ignore any initial white-space chars and does extract (and ignore) the terminating delimiter.

3) uint8_t isn't really an integer - but is unsigned char. Hence for age of say 34 it tries to read 3 as a char then the 4 - so the problem!

Perhaps:

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

int main() {
	std::string Name;

	std::cout << "\n Please provide the following information:\n\n"
		<< "   Name: ";
	std::getline(std::cin, Name);

	unsigned Age;

	std::cout << "   Age: ";
	std::cin >> Age;
	std::cin.ignore();

	std::string Country;

	std::cout << "   Country: ";
	std::getline(std::cin, Country);

	std::string Vaccine;

	std::cout << "   Vaccinated (yes, no only): ";
	std::cin >> Vaccine;

	if (Age >= 12 && Vaccine == "yes")
		std::cout << "   Travel Pass Issued   ";
	else
		std::cout << "   Travel Pass Denied   ";
}

Last edited on
it doesnt seem to work
What exactly does it mean?

if the age input is two digits its skip the country input and goes to vaccine
The problem is uint8_t which is interpreted as a single [unsigned] character. Use int instead and it should work.
change uint8_t to int.
Alright, thank you all for the help. everything worked perfectly after I changed uint_t to int.
Thank you @seeplus for the extra info too!
Topic archived. No new replies allowed.