Read char* argv[] to wchar_t then evaluate in a case statement


Hello all, I have been working on this quite bit but not getting anywhere.
What I want to do is read unicode as argument...

for instance from command line.

./main.cpp "sin(θ)"

then in my program (in pseudo) I would like to run the string through a case statement letter by letter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

for(int i = 0; i < len of argument; i++)
{
convert each wchar_t to a float or double or long (what ever) array
}

for (int i = 0; i < len of numeric array; i++)
{

run each member of numeric array into switch
if the numeric value of θ is evaluated print out the string "theta"

}




This is for a larger project this is the piece I am stuck on.
any help or advise would be great.
Last edited on
To convert char* argv[] to wchar_t
assuming that we have a conforming (non-GNU) implementation of the standard library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <codecvt>
#include <locale>
#include <vector>

int main( int argc, char* argv[] )
{
    // http://en.cppreference.com/w/cpp/locale/wstring_convert
    std::wstring_convert< std::codecvt<wchar_t,char,std::mbstate_t> > converter ;
    // use std::wstring_convert< codecvt_utf8<wchar_t> > if UTF-8 to wchar_t conversion is required

    std::vector<std::wstring> args ;
    for( int i = 1 ; i < argc ; ++i ) args.push_back( converter.from_bytes( argv[i] ) ) ;

    for( const std::wstring& wstr : args )
        std::wcout << wstr << L'\n' ;
}


To convert std::wstring to a numeric value:
http://en.cppreference.com/w/cpp/string/basic_string/stol
http://en.cppreference.com/w/cpp/string/basic_string/stoul
http://en.cppreference.com/w/cpp/string/basic_string/stof
Last edited on
thanks so much! Will test this in the morning!!
Topic archived. No new replies allowed.