convert a string to a variant in c++
Asked Answered
W

2

8

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;
Wingate answered 25/12, 2012 at 23:29 Comment(0)
A
6

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

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

Aery answered 26/12, 2012 at 0:16 Comment(3)
Thanks a lot for your help, I tried adding this in various lines, but get an error, where exactly where i put it?Wingate
Somewhere between variant_t ticker; and pRange->Item .. = ticker;. Of course str should be whatever your std::string input is called. And you probably want to do that AFTER you've read in the string, or it won't have the right effect. ;)Aery
You may want to add your answer as an "answer" (makes the code much more readable)- and if you think I did enoug to help you get there, "accept" my answer.Aery
W
2

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();
Wingate answered 26/12, 2012 at 1:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.