0

I am trying to read the following input from user where the input looks like :

3
5 斤 2 两
7 斤 3 两
6 斤

to solve this question : https://uva.onlinejudge.org/external/125/12555.pdf

However, I have no success so far. I have googled a lot, but I am not able to put it together and I get wrong inputs everytime. Moreover, I am not sure if I am passing the inputs correctly as whether the console supports Unicode or not, which again is an overhead for me, as I am already not confident about my Unicode usage. Please help.

I tried this :

int main()
{
    int t,a,b;
    wchar_t ignore;
    float res;

    cin >> t; //number of test cases
    for(int k=1;k<=t;k++)
    {
        a=b=0;
        cin >> a;
        wcin >> ignore;

        if(cin.peek() != '\n')
        {
            cin >>  b;
            wcin >> ignore;
        }


        cout << "a : " << a << " b : " << b << endl;
    }
    return 0;
}

My output :

a : 5 b : 0
a : 0 b : 0
a : 0 b : 0

EDIT I tried a little variation as well, but still wrong inputs :

int main()
{
    int t,a,b;
    wchar_t ignore;
    float res;

    wcin >> t; //number of test cases
    cout << t << endl;
    for(int k=1;k<=t;k++)
    {
        a=b=0;
        wcin >> a;
        wcin >> ignore;

        if(wcin.peek() != '\n')
        {
            wcin >>  b;
            wcin >> ignore;
        }


        cout << "a : " << a << " b : " << b << endl;
    }
    return 0;
}

EDIT2 This worked for me on a Ubuntu machine, but the online judge is still rejecting my solution. Also on my Windows bash, I am still getting the wrong output:

#include <iostream>
#include <locale>
#include <clocale>

using namespace std;

int main()
{
    int t,a,b;
    wchar_t ignore;
    std::locale loc ("");
    std::locale::global (loc);
    std::setlocale(LC_ALL, NULL);
    std::setlocale(LC_ALL, "");    
    wcin.imbue (loc);

    wcin >> t; //number of test cases
    for(int k=1;k<=t;k++)
    {
        a=b=0;
        wcin >> a;
        wcin >> ignore;

        if(wcin.peek() != L'\n')
        {
            wcin >>  b;
            wcin >> ignore;
        }


        cout << "Case " << k << ": " << a*0.5+b*0.05 << endl;
    }
    return 0;
}
Naveen
  • 7,944
  • 12
  • 78
  • 165
  • It's unlikely that your input is mixed single and wide chars at the same time. You should solely use wide char functions to read the input correctly. – πάντα ῥεῖ Jul 29 '18 at 08:39
  • @πάνταῥεῖ : I tried that as well, but failed to get the correct inputs. Let me share my code in 2 minutes – Naveen Jul 29 '18 at 08:40
  • @πάνταῥεῖ : Secondly, I have already seen the link you shared before posting this question. As I said, I didn't see anywhere the same combination/parsing of integers and characters , so I posted this question. – Naveen Jul 29 '18 at 08:44
  • Again as for your edit: Use solely wide char functions to read the input. It isn't possible that your input receives mixed regular and wide char input. Also those integers will be represented as wide chars at input. – πάντα ῥεῖ Jul 29 '18 at 08:45
  • @πάνταῥεῖ : Ok Got it. So it means I need to input them as wide characters and then convert to integers? – Naveen Jul 29 '18 at 08:47
  • `wcin` will do the conversion to integers correctly already. No need to do something extra. Recommended [read](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/) BTW. – πάντα ῥεῖ Jul 29 '18 at 08:49
  • @πάνταῥεῖ : But in my EDIT, I have now only used wcin everywhere. Can you please post a sample code as answer? I am not able to get the point here. Thanks – Naveen Jul 29 '18 at 08:53
  • And this: `cin >> t;` or this `if(cin.peek() != '\n')`? – πάντα ῥεῖ Jul 29 '18 at 08:55
  • Oh..even that. Thanks. It works now – Naveen Jul 29 '18 at 08:56
  • @πάνταῥεῖ Oops its not working....my mistake....still the same problem. Replaced all `cin` with `wcin`. Can you please post a sample code as answer. It will also help all the future visitors. – Naveen Jul 29 '18 at 09:00
  • Can you change the code in the question to what you have now? Otherwise I probably can't help. – πάντα ῥεῖ Jul 29 '18 at 09:06
  • @πάνταῥεῖ : I have changed the code in EDIT to what I have now. Please check. – Naveen Jul 29 '18 at 09:14
  • The problem is likely here: `if(wcin.peek() != '\n')`. Use a wide char literal for comparison: `if(wcin.peek() != L'\n')` – πάντα ῥεῖ Jul 29 '18 at 09:20
  • Tried this . Still the same output, as given in EDIT with just 5 and rest zeroes. – Naveen Jul 29 '18 at 09:24
  • How exactly do you provide the unicode input to your program? – πάντα ῥεῖ Jul 29 '18 at 09:28
  • I have saved the input as a file using Sublime Editor. Then I use `./out < input` to test my program. – Naveen Jul 29 '18 at 09:30
  • Which shell is used to do that? Does your input file have a BOM, what encoding was exactly used to save the file? – πάντα ῥεῖ Jul 29 '18 at 09:32
  • I am not sure. I just used Sublime to save it. If its needed, how can I add that? I am using bash shell on Windows. – Naveen Jul 29 '18 at 09:35
  • You have very good control over encoding and BOM using Notepad++ as editor. Also inspect your file with a hex editor and check if all codepoints are there as expected. – πάντα ῥεῖ Jul 29 '18 at 09:37
  • I saved the file with UTF-8 encoding with BOM. Still the same issue. I changed those two wierdo characters with x and y and ran through the same program. I got the correct result. So perhaps my inputs is still getting read incorrectly. – Naveen Jul 29 '18 at 09:46
  • You should save the input file as UTF-16 encoding without BOM, to interact correctly with the wide characters. – πάντα ῥεῖ Jul 29 '18 at 09:47
  • @πάνταῥεῖ I saved the file with UTF-16 without BOM and still I face this issue. Is this `./out < input` causing the problem? I can't think of alternate solution to test this – Naveen Jul 29 '18 at 10:39
  • Try to open and read the file from inside the program. I mentioned that the shell could be a problem as well. – πάντα ῥεῖ Jul 29 '18 at 10:40

0 Answers0