当我运行此代码时,我输入一个名称,然后打印其余的cout
语句。当我有多个输入时,它不允许我输入除一个输入之外的任何其他内容。如何解决这个错误?
#include "stdafx.h"
#include <iostream>
using namespace std;
struct info {
char Firstname;
char Lastname;
};
int main()
{
int month;
int date;
info person1, person2, person3, person4, person5;
cout << "Enter the First Name of 1 friend." << endl;
cin >> person1.Firstname;
cout << "Now enter their last name." << endl;
cin >> person1.Lastname;
cout << "Enter the number of the month the person was born in: " << endl;
cin >> month;
cout << "Now Enter your birth date:" << endl;
cin >> date;
if ((month == 1 && date >= 20) || (month == 2 && date <= 18))
{
cout << " Your zodiac sign is AQUARIUS\n";
}
else if ((month == 2 && date >= 19) || (month == 3 && date <= 20))
{
cout << " Your zodiac sign is PISCES\n";
}
else if ((month == 3 && date >= 21) || (month == 4 && date <= 19))
{
cout << " Your zodiac sign is ARIES\n";
}
else if ((month == 4 && date >= 20) || (month == 5 && date <= 20))
{
cout << " Your zodiac sign is TAURUS\n";
}
else if ((month == 5 && date >= 21) || (month == 6 && date <= 20))
{
cout << " Your zodiac sign is GEMINI\n";
}
else if ((month == 6 && date >= 21) || (month == 7 && date <= 22))
{
cout << " Your zodiac sign is CANCER\n";
}
else if ((month == 7 && date <= 23) || (month == 8 && date <= 22))
{
cout << " Your zodiac sign is LEO\n";
}
else if ((month == 8 && date >= 23) || (month == 9 && date <= 22))
{
cout << " Your zodiac sign is VIRGO\n";
}
else if ((month == 9 && date >= 23) || (month == 10 && date <= 22))
{
cout << " Your zodiac sign is LIBRA\n";
}
else if ((month == 10 && date >= 23) || (month == 11 && date <= 21))
{
cout << " Your zodiac sign is SCORPIO\n";
}
else if ((month == 11 && date >= 22) || (month == 12 && date <= 21))
{
cout << " Your zodiac sign is SAGUITTARIUS\n";
}
else if ((month == 12 && date >= 22) || (month == 1 && date <= 19))
{
cout << " Your zodiac sign is CAPRICORN\n";
}
else
{
cout << " INVALID INPUT";
}
cout << "Enter the First Name of 1 friend." << endl;
cin >> person2.Firstname;
cout << "Now enter their last name." << endl;
cin >> person2.Lastname;
cout << "Enter the number of the month the person was born in: " << endl;
cin >> month;
cout << "Now Enter your birth date:" << endl;
cin >> date;
if ((month == 1 && date >= 20) || (month == 2 && date <= 18))
{
cout << " Your zodiac sign is AQUARIUS\n";
}
else if ((month == 2 && date >= 19) || (month == 3 && date <= 20))
{
cout << " Your zodiac sign is PISCES\n";
}
else if ((month == 3 && date >= 21) || (month == 4 && date <= 19))
{
cout << " Your zodiac sign is ARIES\n";
}
else if ((month == 4 && date >= 20) || (month == 5 && date <= 20))
{
cout << " Your zodiac sign is TAURUS\n";
}
else if ((month == 5 && date >= 21) || (month == 6 && date <= 20))
{
cout << " Your zodiac sign is GEMINI\n";
}
else if ((month == 6 && date >= 21) || (month == 7 && date <= 22))
{
cout << " Your zodiac sign is CANCER\n";
}
else if ((month == 7 && date <= 23) || (month == 8 && date <= 22))
{
cout << " Your zodiac sign is LEO\n";
}
else if ((month == 8 && date >= 23) || (month == 9 && date <= 22))
{
cout << " Your zodiac sign is VIRGO\n";
}
else if ((month == 9 && date >= 23) || (month == 10 && date <= 22))
{
cout << " Your zodiac sign is LIBRA\n";
}
else if ((month == 10 && date >= 23) || (month == 11 && date <= 21))
{
cout << " Your zodiac sign is SCORPIO\n";
}
else if ((month == 11 && date >= 22) || (month == 12 && date <= 21))
{
cout << " Your zodiac sign is SAGUITTARIUS\n";
}
else if ((month == 12 && date >= 22) || (month == 1 && date <= 19))
{
cout << " Your zodiac sign is CAPRICORN\n";
}
else
{
cout << " INVALID INPUT";
}
cout << "Enter the First Name of 1 friend." << endl;
cin >> person3.Firstname;
cout << "Now enter their last name." << endl;
cin >> person3.Lastname;
cout << "Enter the number of the month the person was born in: " << endl;
cin >> month;
cout << "Now Enter your birth date:" << endl;
cin >> date;
if ((month == 1 && date >= 20) || (month == 2 && date <= 18))
{
cout << " Your zodiac sign is AQUARIUS\n";
}
else if ((month == 2 && date >= 19) || (month == 3 && date <= 20))
{
cout << " Your zodiac sign is PISCES\n";
}
else if ((month == 3 && date >= 21) || (month == 4 && date <= 19))
{
cout << " Your zodiac sign is ARIES\n";
}
else if ((month == 4 && date >= 20) || (month == 5 && date <= 20))
{
cout << " Your zodiac sign is TAURUS\n";
}
else if ((month == 5 && date >= 21) || (month == 6 && date <= 20))
{
cout << " Your zodiac sign is GEMINI\n";
}
else if ((month == 6 && date >= 21) || (month == 7 && date <= 22))
{
cout << " Your zodiac sign is CANCER\n";
}
else if ((month == 7 && date <= 23) || (month == 8 && date <= 22))
{
cout << " Your zodiac sign is LEO\n";
}
else if ((month == 8 && date >= 23) || (month == 9 && date <= 22))
{
cout << " Your zodiac sign is VIRGO\n";
}
else if ((month == 9 && date >= 23) || (month == 10 && date <= 22))
{
cout << " Your zodiac sign is LIBRA\n";
}
else if ((month == 10 && date >= 23) || (month == 11 && date <= 21))
{
cout << " Your zodiac sign is SCORPIO\n";
}
else if ((month == 11 && date >= 22) || (month == 12 && date <= 21))
{
cout << " Your zodiac sign is SAGUITTARIUS\n";
}
else if ((month == 12 && date >= 22) || (month == 1 && date <= 19))
{
cout << " Your zodiac sign is CAPRICORN\n";
}
else
{
cout << " INVALID INPUT";
}
cout << "Enter the First Name of 1 friend." << endl;
cin >> person4.Firstname;
cout << "Now enter their last name." << endl;
cin >> person4.Lastname;
cout << "Enter the number of the month the person was born in: " << endl;
cin >> month;
cout << "Now Enter your birth date:" << endl;
cin >> date;
if ((month == 1 && date >= 20) || (month == 2 && date <= 18))
{
cout << " Your zodiac sign is AQUARIUS\n";
}
else if ((month == 2 && date >= 19) || (month == 3 && date <= 20))
{
cout << " Your zodiac sign is PISCES\n";
}
else if ((month == 3 && date >= 21) || (month == 4 && date <= 19))
{
cout << " Your zodiac sign is ARIES\n";
}
else if ((month == 4 && date >= 20) || (month == 5 && date <= 20))
{
cout << " Your zodiac sign is TAURUS\n";
}
else if ((month == 5 && date >= 21) || (month == 6 && date <= 20))
{
cout << " Your zodiac sign is GEMINI\n";
}
else if ((month == 6 && date >= 21) || (month == 7 && date <= 22))
{
cout << " Your zodiac sign is CANCER\n";
}
else if ((month == 7 && date <= 23) || (month == 8 && date <= 22))
{
cout << " Your zodiac sign is LEO\n";
}
else if ((month == 8 && date >= 23) || (month == 9 && date <= 22))
{
cout << " Your zodiac sign is VIRGO\n";
}
else if ((month == 9 && date >= 23) || (month == 10 && date <= 22))
{
cout << " Your zodiac sign is LIBRA\n";
}
else if ((month == 10 && date >= 23) || (month == 11 && date <= 21))
{
cout << " Your zodiac sign is SCORPIO\n";
}
else if ((month == 11 && date >= 22) || (month == 12 && date <= 21))
{
cout << " Your zodiac sign is SAGUITTARIUS\n";
}
else if ((month == 12 && date >= 22) || (month == 1 && date <= 19))
{
cout << " Your zodiac sign is CAPRICORN\n";
}
else
{
cout << " INVALID INPUT";
}
cout << "Enter the First Name of 1 friend." << endl;
cin >> person5.Firstname;
cout << "Now enter their last name." << endl;
cin >> person5.Lastname;
cout << "Enter the number of the month the person was born in: " << endl;
cin >> month;
cout << "Now Enter your birth date:" << endl;
cin >> date;
if ((month == 1 && date >= 20) || (month == 2 && date <= 18))
{
cout << " Your zodiac sign is AQUARIUS\n";
}
else if ((month == 2 && date >= 19) || (month == 3 && date <= 20))
{
cout << " Your zodiac sign is PISCES\n";
}
else if ((month == 3 && date >= 21) || (month == 4 && date <= 19))
{
cout << " Your zodiac sign is ARIES\n";
}
else if ((month == 4 && date >= 20) || (month == 5 && date <= 20))
{
cout << " Your zodiac sign is TAURUS\n";
}
else if ((month == 5 && date >= 21) || (month == 6 && date <= 20))
{
cout << " Your zodiac sign is GEMINI\n";
}
else if ((month == 6 && date >= 21) || (month == 7 && date <= 22))
{
cout << " Your zodiac sign is CANCER\n";
}
else if ((month == 7 && date <= 23) || (month == 8 && date <= 22))
{
cout << " Your zodiac sign is LEO\n";
}
else if ((month == 8 && date >= 23) || (month == 9 && date <= 22))
{
cout << " Your zodiac sign is VIRGO\n";
}
else if ((month == 9 && date >= 23) || (month == 10 && date <= 22))
{
cout << " Your zodiac sign is LIBRA\n";
}
else if ((month == 10 && date >= 23) || (month == 11 && date <= 21))
{
cout << " Your zodiac sign is SCORPIO\n";
}
else if ((month == 11 && date >= 22) || (month == 12 && date <= 21))
{
cout << " Your zodiac sign is SAGUITTARIUS\n";
}
else if ((month == 12 && date >= 22) || (month == 1 && date <= 19))
{
cout << " Your zodiac sign is CAPRICORN\n";
}
else
{
cout << " INVALID INPUT";
}
return 0;
}
答案 0 :(得分:0)
正如@Igor Tandetnik指出你在你的结构中使用了一个字符。
因此,如果您输入了一个包含3个或更多字符的名称,那么它将绕过下一个cin
函数调用。
更改您的结构以使用char数组并使用getline
的{{1}}函数。
cin
然后在你的主要功能中:
struct info {
char Firstname[50];
char Lastname[50];
};