Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a string to a variant in c++

I have this current code, which types "AAPL" into an excel sheet, and the returns the corresponding value.

I would like to make it so that after cout << "Ticker: "; i can type in a ticker symbol (such as AAPL) and use this as the variant_t ticker = "xxx". I tried doing so by using string but i get an error that says cannot convert from 'std::string to const _variant_t &' Is there anyway to do this? Thanks in advance.

 XL->Workbooks->Open(L"F:\\xxx\\Desktop\\fxxxx.xlsx");
 Excel::RangePtr pRange = pSheet->Cells;

 cout << "Ticker: ";
 variant_t ticker = "AAPL";

 pRange->Item[2][1] = ticker;
 double value = pRange->Item[2][2];
 cout << "\n Value = " << value << endl;
like image 292
user1594369 Avatar asked Dec 25 '12 23:12

user1594369


2 Answers

Call ticker.SetString(str.c_str()) should do the job.

See: http://msdn.microsoft.com/en-us/library/x295h94e%28v=vs.100%29.aspx

like image 131
Mats Petersson Avatar answered Nov 02 '22 17:11

Mats Petersson


Here is the working code:

    XL->Workbooks->Open(L"F:\\xx\\Desktop\\xxz.xlsx");                              //Open databse
    Excel::_WorksheetPtr pSheet = XL->ActiveSheet;                                      //Point to Active Sheet
    Excel::RangePtr pRange = pSheet->Cells;                                             //Point to Active Cells
    cout << " Ticker: ";                                                                //Prompt Ticker
    string tick;                                                                        //Define string "tick" to store ticker
    cin >> tick;                                                                        //Store ticker
    variant_t ticker;                                                                   //Define variant to be used for ticker
    ticker.SetString(tick.c_str());                                                     //Convert string to variant                                 
    pRange->Item[2][1] = ticker;                                                        //Write ticker to cell
    double bi = pRange->Item[2][2];                                                     //Read Beta, store as "bi"
    cout << "\n Beta = " << bi << endl;                                                 //Return Value
    XL->Application->Quit();
like image 20
user1594369 Avatar answered Nov 02 '22 16:11

user1594369