gvatar
12-10-2007, 04:59 PM
Im a newbie in C++ having started learning it for only a few days, so pardon me for any misconceptions I may have or my ignorance.
For some queer reason, the following code does not produce the desired effect of allowing the user to input ten different words and displaying the fifth word. Instead, it allows an infinite input that does not terminate with either \n or \t. Oh, any suggestions to shorten the code would be appreciated :thumbsup:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1, str2, str3, str4, str5, str6, str7, str8, str9, str10;
cout << "Enter the phrase:" << endl;
getline(cin, str1, '\t');
getline(cin, str2, '\t');
getline(cin, str3, '\t');
getline(cin, str4, '\t');
getline(cin, str5, '\t');
getline(cin, str6, '\t');
getline(cin, str7, '\t');
getline(cin, str8, '\t');
getline(cin, str9, '\t');
getline(cin, str10);
cout << str5;
system("pause");
return 0;
}
For some queer reason, the following code does not produce the desired effect of allowing the user to input ten different words and displaying the fifth word. Instead, it allows an infinite input that does not terminate with either \n or \t. Oh, any suggestions to shorten the code would be appreciated :thumbsup:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1, str2, str3, str4, str5, str6, str7, str8, str9, str10;
cout << "Enter the phrase:" << endl;
getline(cin, str1, '\t');
getline(cin, str2, '\t');
getline(cin, str3, '\t');
getline(cin, str4, '\t');
getline(cin, str5, '\t');
getline(cin, str6, '\t');
getline(cin, str7, '\t');
getline(cin, str8, '\t');
getline(cin, str9, '\t');
getline(cin, str10);
cout << str5;
system("pause");
return 0;
}