1
// 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.

Juan
  • 33
  • 5
  • P.S. there's a circumflex on "plaît" and an acute on "écrire". – Rup Feb 15 '19 at 12:43
  • 1
    Whilst I'd normally applaud you for trying to use Unicode IO on Windows, if you're only interested in French accents (and console output) then you don't need to: you can cover most of Europe with the default code page. – Rup Feb 15 '19 at 12:45
  • My French is really rusty, but it's just occurred to me too: you probably want "écrivez" anyway, not "écrire", as the imperative vous form. – Rup Feb 15 '19 at 12:57
  • turns out the program only answers with yes even if the answer is wrong and the accented character are showing up unrecognized in the terminal – Juan Feb 15 '19 at 13:25
  • Possible duplicate of [How to compare char variables (c-strings)?](https://stackoverflow.com/questions/8355465/how-to-compare-char-variables-c-strings) – phuclv Feb 15 '19 at 15:40
  • I got the code to work but french character are not recognized by default and when I use wcout it stills comes out the same. any cin inputs with accented characters are unrecognized – Juan Feb 16 '19 at 07:57
  • It's more complicated than you think. The accented ê can be encoded differently (http://unicode.org/reports/tr15/) and you have platform specific issues. I would prefix your strings explicitly with u8 or L (https://en.cppreference.com/w/cpp/language/string_literal) and then use std::string or std::wstring. – andreaplanet Feb 16 '19 at 10:18
  • ...and you may have to set the console code page, it depends on the platform. https://stackoverflow.com/questions/1371012/how-do-i-print-utf-8-from-c-console-application-on-windows – andreaplanet Feb 16 '19 at 10:29

1 Answers1

1

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;
}
Kunal Puri
  • 3,419
  • 1
  • 10
  • 22
  • How do I make userAnswer == answer work and make it compare their actual content – Juan Feb 15 '19 at 12:39
  • @Juan Do you want to use `char *` or `wchar_t *`? I have separate answers for each two. – Kunal Puri Feb 15 '19 at 12:41
  • I actually don't know which one to use I used char * in order to reduce red error marks this is my first time interacting with unicode which is better wchar_t or char can you tell me – Juan Feb 15 '19 at 12:43
  • It actually depends on the type of encoding and the platform. See this: https://stackoverflow.com/a/17871934/5859925. So, I think `char *` is sufficient. I will answer accordingly. – Kunal Puri Feb 15 '19 at 12:45
  • Use either std::string or std::wstring. No C-Strings and it will work. To compare C-Strings, you need std::strcmp. – taminob Feb 15 '19 at 12:46
  • 2
    "_`std::getline` is defined for `std::string`._" Is wrong. As per the [documentation](https://en.cppreference.com/w/cpp/string/basic_string/getline), `std::getline` is not tied to a particular class (`std::string`, or `std::wstring`), or particular stream (`std::cin`, or `std::wcin`). – Algirdas Preidžius Feb 15 '19 at 13:01
  • @AlgirdasPreidžius Thanks for pointing it out. I have corrected it. – Kunal Puri Feb 15 '19 at 13:05