How can I make it so that the user enters the name for each sibling?
right now it just displays:
How many brothers and sisters do you have?4
A big family!
What's the name of your #1 sibling?
mark
What's the name of your #2 sibling?
What's the name of your #3 sibling?
What's the name of your #4 sibling?
//Part 2 Coding
//Ronan Sullivan
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int siblings;
int name, x;
x = 1;
cout << "How many brothers and sisters do you have?";
cin >> siblings;
if (siblings < 0)
{cout << "How could that be?\n";
cout << "Please enter a valid entry.\n";
}
elseif (siblings == 0)
cout << "Oh, you were an only child.\n";
elseif (siblings >=1 && siblings <= 3)
cout << "Sounds like a nice size family.\n";
elseif (siblings > 3)
cout << "A big family!\n";
while (siblings >= 1)
{
cout << "What's the name of your #" << x++ << " sibling?\n";
siblings--;
cin >> name;
}
return 0;
}
cin by itself is not the best for doing multiple inputs. Since there is an endline character already in the buffer after entering the first name each subsequent call to cin merely retrieves the endline.
You should use cin.get() instead of cin >>, and then "flush" cin's buffer by using cin.sync().
//Part 2 Coding
//Ronan Sullivan
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int siblings;
int name, x;
x = 1;
cout << "How many brothers and sisters do you have?";
cin >> siblings;
cin.sync();
if (siblings < 0)
{
cout << "How could that be?\n";
cout << "Please enter a valid entry.\n";
}
elseif (siblings == 0)
{
cout << "Oh, you were an only child.\n";
}
elseif (siblings >=1 && siblings <= 3)
{
cout << "Sounds like a nice size family.\n";
}
elseif (siblings > 3)
{
cout << "A big family!\n";
}
while (siblings >= 1)
{
cout << "What's the name of your #" << x++ << " sibling?\n";
siblings--;
name = cin.get();
cin.sync();
}
return 0;
}