Firstly,
strcmpi() is not a standard function.
This means that it's not officially part of the C++ standard, and therefore the code you write using
strcmpi() may not compile for other people who use other tools than you do.
For example if I don't use Visual C++ but I use MinGW, then maybe I will not be able to compile your code.
If you want to be a Windows programmer only, you may just was well use MSDN to learn C++, and use non-standard functions and extensions.
Otherwise, only use functions which appear in Standard C++ references:
http://www.cplusplus.com/reference/
http://en.cppreference.com/w/
Secondly, a little history lesson.
Before C++ there was C.
Today C and C++ exist as two different but related languages, and over the years they borrowed features from one another.
Because C++ is based on C, it inherited a part of the library of C (headers, functions).
At the same time, C++ added new libraries as a better alternative to those from C.
As a C++ programmer, you should generally avoid using the legacy libraries inherited from C.
In your case, learn to use
std::string and the
algorithm library of C++ instead of the
string.h library of C.
Thirdly, accents should be supported if you use wide character types:
std::wstring,
std::wcin,
std::wcout etc.
As for the spaces problem, you're probably using
std::cin which stops reading user input after the first space it finds. Use
std::getline() instead.
If you want to compare strings without case sensitivity, you have a lot of work to do:
http://stackoverflow.com/questions/11635/case-insensitive-string-comparison-in-c
http://www.gotw.ca/gotw/029.htm
I tried writing a program to deal with accents, but I am using only MinGW currently, and the program doesn't work correctly.
Maybe in Visual Studio 2012 it may work? And maybe someone else can help more (maybe you must use the
locale library)?
http://msdn.microsoft.com/en-us/library/39cwe7zf.aspx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// This is C++11 code.
#include <iostream>
#include <string>
int main()
{
const std::wstring expected_name(L"Cristóvão Colombo");
std::wstring given_name;
std::wcout << "Question: Who discovered America?\nYour answer: ";
std::getline(std::wcin, given_name);
if (given_name != expected_name)
std::wcout << "Your answer was wrong. It was " << expected_name << ".\n";
else
std::wcout << "Your answer was correct!\n";
}
|