[英]C++ Substrings Assignment For Textgame
我的问题的描述是这样的(textGame.cpp)完成一个程序,该程序为ad-lib类型的游戏输入一些字符串变量。 不完整的代码可以从这里下载 textGameBroken.cpp 下载 textGameBroken.cpp 5 点 - 修复proper_case function 使非字母字符(空格、破折号等)后的首字母和首字母大写,rest 小写5 分 - 将 firstName_first2char 修复为 firstName 的前两个字符 5 分 - 将 firstName2_minus2char 修复为通过 firstName2 变量末尾的第三个字符 10 分 - 修复 find_ordinal function 以返回正确的序数 1st、2nd、3rd、4th。 .. 等 5 分 - 修复 is_a_number bool function 以在输入的所有数字都是数字时返回 true,否则返回 false
附件:
// Prof. Touchette
// Text ad-lib game
// modified by ??your name here??
#include <iostream>
#include <string>
using namespace std;
//Prototype for functions
string proper_case(string);
string find_ordinal(string);
bool is_a_number(string);
int main()
{
//Declare variables and initialize
string lastName = "";
string firstName = "";
string firstName_first2char = "";
string lastName2 = "";
string firstName2 = "";
string firstName2_minus2char = "";
string occupation = "";
string number = "";
string ordinal = "";
string country = "";
string adjective = "";
string noun = "";
string noun2 = "";
string noun3 = "";
string noun2init = "";
// Get inputs
while (firstName.length() < 2)
{
cout << "Enter a person's first name (minimum 2 characters):";
getline(cin, firstName);
}
while (lastName.length() < 2)
{
cout << "Enter a person's last name (minimum 2 characters):";
getline(cin, lastName);
}
while (firstName2.length() < 4)
{
cout << "Enter a another person's first name (minimum 4 characters):";
getline(cin, firstName2);
}
while (lastName2.length() < 2)
{
cout << "Enter a another person's last name (minimum 2 characters):";
getline(cin, lastName2);
}
cout << "Enter an occupation:";
getline(cin, occupation);
while (! is_a_number(number))
{
cout << "Enter a number:";
cin >> number;
}
//mixing cim and getline can cause a problem
//clear the buffer before next getline or \n will carry over from cin
cin.ignore(numeric_limits<streamsize>::max(),'\n'); // could also use cin.ignore();
cout << "Enter the name of a country:";
getline(cin, country);
cout << "Enter an adjective(descriptive word):";
getline(cin, adjective);
cout << "Enter a noun(a thing):";
getline(cin, noun);
cout << "Enter another noun(a thing):";
getline(cin, noun2);
cout << "Enter another noun(a thing):";
getline(cin, noun3);
//Process text
firstName = proper_case(firstName);
lastName = proper_case(lastName);
firstName2 = proper_case(firstName2);
lastName2 = proper_case(lastName2);
country = proper_case(country);
noun2 = proper_case(noun2);
noun2init = toupper(noun2[0]);
ordinal = find_ordinal(number);
firstName_first2char = firstName; //*** Need substring (substr) with 1st 2 characters of firstName
firstName2_minus2char = firstName2; //*** Need substr for 3rd character to the end of firstName2
//Output paragraph
cout << firstName << " " << lastName << " was a lawyer and " << occupation << " who served as \n";
cout << country << "'s " << number << ordinal << " president. " << lastName << " led the nation \n";
cout << "through its greatest " << adjective << " " << noun3 << " crisis and preserved the \n";
cout << noun << ". The president worked with " << firstName2 << " " << lastName2 << " so closedly \n";
cout << "on the " << noun2 << " Alliance Movement " << "(also know as " << noun2init << "AM), the duo were \n";
cout << "sometimes referred to as " << firstName_first2char << "-" << firstName2_minus2char << ".\n";
return 0;
}
string proper_case(string input_name)
{
string proper_name = input_name;
proper_name[0] = toupper(input_name[0]);
for (int i=1; i<input_name.length(); ++i)
{
if (isalpha(input_name[i-1]))
{
proper_name[i] = tolower(input_name[i]);
}
else
{
//*** Need upper case here
}
}
return proper_name;
}
string find_ordinal(string input_number)
{
string ord;
string last_digit;
last_digit = input_number.substr(input_number.length()-1,1);
if (last_digit == "1")
{
ord = "st";
}
else if (last_digit == "2")
{
ord = "nd";
}
//*** Need to finish function for other digits.
return ord;
}
bool is_a_number(string input_number)
{
if (input_number.length() == 0)
//*** return true or false;
for (int i=0;i<input_number.length();++i)
{
if (! isdigit(input_number[i]))
{
//*** return true or false;
}
}
//*** return true or false;
}
这是我当前的代码
////////
// modified by
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
//Prototype for functions
string proper_case(string);
string find_ordinal(string);
bool is_a_number(string);
int main()
{
//Declare variables and initialize
string lastName = "";
string firstName = "";
string firstName_first2char = "";
string lastName2 = "";
string firstName2 = "";
string firstName2_minus2char = "";
string occupation = "";
string number = "";
string ordinal = "";
string country = "";
string adjective = "";
string noun = "";
string noun2 = "";
string noun3 = "";
string noun2init = "";
// Get inputs
while (firstName.length() < 2)
{
cout << "Enter a person's first name (minimum 2 characters):";
getline(cin, firstName);
}
while (lastName.length() < 2)
{
cout << "Enter a person's last name (minimum 2 characters):";
getline(cin, lastName);
}
while (firstName2.length() < 4)
{
cout << "Enter a another person's first name (minimum 4 characters):";
getline(cin, firstName2);
}
while (lastName2.length() < 2)
{
cout << "Enter a another person's last name (minimum 2 characters):";
getline(cin, lastName2);
}
cout << "Enter an occupation:";
getline(cin, occupation);
while (! is_a_number(number))
{
cout << "Enter a number:";
getline(cin, number,);
}
//mixing cim and getline can cause a problem
//clear the buffer before next getline or \n will carry over from cin
cin.ignore(); // could also use cin.ignore();
cout << "Enter the name of a country:";
getline(cin, country);
cout << "Enter an adjective(descriptive word):";
getline(cin, adjective);
cout << "Enter a noun(a thing):";
getline(cin, noun);
cout << "Enter another noun(a thing):";
getline(cin, noun2);
cout << "Enter another noun(a thing):";
getline(cin, noun3);
//Process text
firstName = proper_case(firstName);
lastName = proper_case(lastName);
firstName2 = proper_case(firstName2);
lastName2 = proper_case(lastName2);
country = proper_case(country);
noun2 = proper_case(noun2);
noun2init = toupper(noun2[0]);
ordinal = find_ordinal(number);
firstName_first2char = firstName; //*** Need substring (substr) with 1st 2 characters of firstName
firstName2_minus2char = firstName2; //*** Need substr for 3rd character to the end of firstName2
//Output paragraph
cout << firstName << " " << lastName << " was a lawyer and " << occupation << " who served as \n";
cout << country << "'s " << number << ordinal << " president. " << lastName << " led the nation \n";
cout << "through its greatest " << adjective << " " << noun3 << " crisis and preserved the \n";
cout << noun << ". The president worked with " << firstName2 << " " << lastName2 << " so closedly \n";
cout << "on the " << noun2 << " Alliance Movement " << "(also know as " << noun2init << "AM), the duo were \n";
cout << "sometimes referred to as " << firstName_first2char << "-" << firstName2_minus2char << ".\n";
return 0;
}
string proper_case(string input_name)
{
string proper_name = input_name;
proper_name[0] = toupper(input_name[0]);
for (int i=1; i<input_name.length(); ++i)
{
if (isalpha(input_name[i-1]))
{
proper_name[i] = tolower(input_name[i]);
}
else
{
proper_name[i] = toupper(input_name[i]); //*** Need upper case here
}
}
return proper_name;
}
string find_ordinal(string input_number)
{
string ord;
string last_digit;
last_digit = input_number.substr(input_number.length()-1,1);
if (last_digit == "1")
{
ord = "st";
}
else if (last_digit == "2")
{
ord = "nd";
}
//*** Need to finish function for other digits.
return ord;
}
bool is_a_number(string input_number)
{
if (input_number.length() == 0)
//*** return true or false;
for (int i=0;i<input_number.length();++i)
{
if (! isdigit(input_number[i]))
{
//*** return true or false;
}
}
//*** return true or false;
return 0;
}
当一个数字的输入弹出它永远重复时,我不知道如果有人可以更正此代码并解释他们所做的每一行会发生什么,我将非常感激我非常迷失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.