新手求教strlwr()
这个可以直接用在字符串比较中吗?下面是一个输入学生姓名查学号的小程序。然后有一个功能是输入学生姓名(英文名),无论大小写都可以。所以我在构造姓名字符串的时候都用小写,然后在isValidName()里面用strlwr()把输入的字符串都变成小写(红字的部分),再一一比较。这样可行吗?其他部分都没有问题了,就红字部分。求各位大神帮帮忙//源码
#include<iostream>
#include<cmath>
#include <string>
#include <string.h> #include<iostream>
#include<cmath>
#include <string>
#include <string.h> //将大写字母转换成小写字母用strlwr
#include<ctime>
using namespace std;
string sarr[] = {"carl", "elle", "keith", "prad", "molly", "bunmi", "kim" };
bool isValidName(string pname);
string converNumber(int x);
string convertName(string pname);
int main(){
string pname;
int arr[10];
int i = 0;
int answer;
int correctAnswer = 0;
int incorrectAnswer = 0;
char m;
cout << "Please enter your name:";
cin >> pname;
cout << endl;
if (isValidName(pname) == true){
cout << "C-number" << ":" << convertName(pname) << " starting the test..." << endl;
do{
unsigned seed = time(0);
srand(seed);
int a = rand() % 10 + 1;
int b = rand() % 10 + 1;
cout << pname << ",what is the remainder after " << converNumber(a) << " is divided by " << converNumber(b) << endl;
cin >> answer;
if (answer == a%b)
{
cout << "Well down "<<pname<<" you are correct." << endl;
correctAnswer++;
}
else
{
cout << "That's not right,"<<pname<<" the correct answer is " << a%b << endl;
incorrectAnswer++;
}
cout << "Do you want to continue(y/n):";
cin >> m;
} while (m == 'y' || m == 'Y');
cout << "Correct answer:" << correctAnswer << endl;
cout << "Incorrect answer:" << incorrectAnswer << endl;
cout << "Percentage answered correctly:" << (double)correctAnswer / (correctAnswer + incorrectAnswer)*100 << "%" << endl;
}
else{
cout << pname << ",you are not required to take this test";
}
return 0;
}
bool isValidName(string pname){
for (int i = 0; i<6; i++){
if (strlwr(pname) == sarr[i]) //把pname变成小写字母再进行比较
return true;
}
return false;
}
string convertName(string pname){
string narr[7] = { "C531006","C531007", "C531010", "C532011", "C532015", "C531178", "C532196" };
string result = "";
for (int i = 0;i<7; i++){
if (pname == sarr[i])
result = narr[i];
}
return result;
}
string converNumber(int x){
int num[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
string str[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
string result = "";
for (int i = 0; i < 10; i++){
if (x == num[i])
result = str[i];
}
return result;
}
[此贴子已经被作者于2017-3-21 11:22编辑过]