Juan
Juan

Reputation: 33

Can't get french characters to work in C++

// francais projecct test1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
    char  userAnswer[10];
    char answer[] = { "Vous êtes" };

    wcout << "s'il vous plaat ecrire conjugation pour Vous etre: ";

    cin>>userAnswer;

    if (strcmp(userAnswer, answer) == 0)

        cout << endl << "correct"<<endl<<endl;
    else
        cout << endl << "wrong answer"<<endl<<endl;

    system("pause");
    return 0;
}

The accented characters aren't recognized by the compiler and I don't know how to get input of unicode characters if unicode is required.

Upvotes: 1

Views: 1134

Answers (1)

Kunal Puri
Kunal Puri

Reputation: 3427

std::getline is defined for std::basic_string (specialized cases include std::string, std::wstring). Normal character arrays don't fall in that category.

Reference: http://www.cplusplus.com/reference/string/string/getline/

Though I would highly recommend you to use std::string / std::wstring, If you want to make your code work, you must use cin.getline in your case.

You may refer to Example 2 in this: https://www.programiz.com/cpp-programming/library-function/iostream/wcin

Secondly, userAnswer == answer is wrong as it will compare two pointers, not their actual content.

For doing so, you should use strcmp().

Reference: http://www.cplusplus.com/reference/cstring/strcmp/

Something like this:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
    char userAnswer[10];
    char answer[] = "Vous etes";

    wcout <<"s'il vous plait ecrire conjugation pour Vous etre: ";
    cin.getline(userAnswer, 10);

    if (!strcmp(userAnswer, answer))
    {
        wcout <<endl<< "correct";
    }
    else
    {
        wcout <<endl<< "wrong answer";
    }

    return 0;
}

Upvotes: 1

Related Questions