我想要做的是检查用户提供的字符串是否有大写字母。 带有枚举常量的代码的第一部分就是另一种方法, 我使用分配给星期日= 7,星期一= 1,星期二= 2等字的数字来工作。我正在尝试让用户不提供数字,但实际的字(周日,周一,周二),但我希望他们用大写字母指定星期几。
我得到的问题:编译得很好,但是你输入的任何内容都会以某种方式重定向到执行路径,该执行路径始终返回“使用大写字母重新输入”。如果我输入fjdkalfda或sunday或Sunday或其他任何内容,就会发生这种情况。这是代码。对不起,如果它不是代码格式,Stack Overflow的第一次用户。
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(){
/*enum DaysOfWeek {
Monday = 1,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};*/ //I'm not using this, but I can get my code to work with this
//enumerated constant by asking the user to input a number corresponding
//to the day of the week and then using if-else statements to check, say,
// int Day;
// cin >> Day
// then use if statements to check if Day = a certain number, to give output
cout << "Enter the day of week with a capital letter: ";
string Day;
getline(cin, Day);
if (Day == ("monday") || ("tuesday"))
cout << "Retype w/ a capital letter" << endl;
else if (Day == ("wednesday") || ("thursday"))
cout << "Retype w/ a capital letter" << endl;
else if (Day == ("friday") || ("saturday"))
cout << "Retype w/ a capital letter" << endl;
else if (Day == ("sunday"))
cout << "Retype w/ a capital letter" << endl;
else
{
if (Day == "Monday")
cout << "Moon" << endl;
else if (Day == "Tuesday")
cout << "Mars" << endl; //continue with the days etc...
}
return 0;
}
答案 0 :(得分:1)
人们会发现有一个名为 toupper 的函数可以将小写字母转换为大写字母,并单独留下大写字母。也许你可以重复使用这个函数来帮助用更少的代码来解决你的问题。
我从你的代码中推断出你只想验证第一个字母是大写字母。这是对的吗?
答案 1 :(得分:1)
ctype.h中有一个isupper函数,如果char
为大写,则返回true:
#include <ctype.h>
...
isCapitalized = isupper(str[0]);
...
答案 2 :(得分:1)
如果我们能以某种方式组织enum
类型的输入选择,我假设你实际上并不关心国会大厦的字母。如果您有enum classes
,那么取代C++11
将是更好的选择。如果没有,那么为常规枚举更改此代码将非常容易
如果您使用的是gcc / g ++,则可能需要包含前缀-std=c++11
注意:您可能希望将另一个enum
元素添加到enum class Days
,例如null_day
。或者甚至更好,确保用户输入正确的日期,直到他/她做,不要继续使用程序(提示:使用while
时使用getline
循环
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using std::string;
using std::transform;
using std::cout;
using std::cin;
using std::endl;
// only in C++ 11
enum class Day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
int main () {
cout << "Enter the day of week: ";
// store day from user input, transform all to lower case so
// we don't bother with user input type (only error in spelling will be the problem now)
string get_day;
getline(cin, get_day);
transform(get_day.begin(), get_day.end(), get_day.begin(), tolower);
// define instance of enum class Day, and sort out what day user has input
// note that if you do not initialize myDay object, it will set to the first
// element in the enum class Day above (which is Monday). This will happen
// if user input is not allowed to initialize it. See the << >> section below
Day myDay;
if (get_day == "monday")
myDay = Day::Monday;
else if (get_day == "tuesday")
myDay = Day::Tuesday;
else if (get_day == "wednesday")
myDay = Day::Wednesday;
else if (get_day == "thursday")
myDay = Day::Thursday;
else if (get_day == "friday")
myDay = Day::Friday;
else if (get_day == "saturday")
myDay = Day::Saturday;
else if (get_day == "sunday")
myDay = Day::Sunday;
else
<< ERROR CODE HERE >>
<< MAYBE A WHILE LOOP UNTIL USER INPUTS CORRECT DATA >>
// perform your calculations/operations/etc separate from above
if (myDay == Day::Monday)
cout << "Moon" << endl;
else if (myDay == Day::Tuesday)
cout << "Mars" << endl; //continue with the days etc...
return 0;
}