user input - lowercase mixed with uppercase

okay another beginnerC++ question...

Can someone compile my code below to figure out my problem
- it runs, it compiles. it answers the questions properly (sort of)

PROBLEM:
when the user answers the questions correctly, but IMPROPERLY, the program still declares that the answer is wrong.

EXAMPLE:
Question 2 -
answer is pascal
// i want the user to be able to input PasCAL, PASCAL, pascal, PAsCaL
and still get the correct answer. as long as the characters match.
whether they input lowercase or uppercase or mixture of both.
// if the user does not write it EXACTLY the way i wrote it as in the
quotations, user gets the wrong answer.

Situation with Question 2 -
input: PASCAL
output: No, it's pascal

Situation with Question 3 -
input: Englebert
output: No, it's englebert



thanks for all the suggestions and the help. i truly appreciate this. =)
...kristine...


___________________________________________________


#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

#include <cctype>

// required for conversion to lowercase
class toLower {public: char operator()(char c) const {return toupper(c);}};


void askQuestion(string q, string a)
{


string answer; // just a local variable
cout << q << ": ";
getline(cin, answer);
if (answer == a)
cout << "Correct!\n";
else
cout << "No, it's " << a << endl;

} // askQuestion


int main()
{

askQuestion("Which decade gave birth to the electronic digital computer? [1930, 1940]", "1940");
askQuestion("Who invented the 1st mathematical adding machine? [Pascal/Schickard]", "pascal");
askQuestion("Who invented the computer mouse? [Englebart/Kemeny]", "englebart");

cin.ignore();
cin.get();
return 0;
} // main
Because according to ASCII, 'A' and 'a' are different characters.

Good to stick with one format of the correct answer (say, all lowercase).
Make a function that coverts the user input to all lowercase before comparing it with the correct answer.

[edited]
I'm not very familiar with streams...Your function can read char by char of answer, then covert to lowercase whenever it "sees" an uppercase char.

This can be a useful reference:
1
2
for (int s = 0; s < 256; s++)
    std::cout << char(s) << '\t' << s << '\n';


The "set" of lowercase letters is just a "shift" of the "set" of uppercase ones.
Last edited on
I think this matches ur req:

void toLowerCase(std::string &str)
{
const int length = str.length();
for(int i=0; i < length; ++i)
{
str[i] = std::tolower(str[i]);
}
}

void askQuestion(string q, string a)
{


string answer; // just a local variable
cout << q << ": ";
getline(cin, answer);
toLowerCase(answer);
toLowerCase(a);
if (answer == a)
cout << "Correct!\n";
else
cout << "No, it's " << a << endl;

}
hey thank you guys very much. helped me out A LOT. truly appreciate it. =)

..kRisTine...
Topic archived. No new replies allowed.