0

I am taking input from user in visual c++ through the following code

Console::WriteLine("Select the Cache Size.\n a. 1 Kb \n b. 2 Kb \n c. 4 Kb \n d. 8 Kb\n");
    String^ CACHE_SIZEoption = Console::ReadLine();
    Char wh= Char(CACHE_SIZEoption);

    switch(wh)
    {case 'a':
    break;

    case 'b':
    break;

    case 'c':
     break;

    case 'd':
    break;
    }

In this the conversion from String to Char is giving errors..

 error C2440: '<function-style-cast>' : cannot convert from 'System::String ^' to 'wchar_t'
4
  • What does String^ mean? It's not C++ for sure... Commented May 3, 2013 at 11:00
  • 2
    @trojanfoe It's C++/CLI, a Microsoft language based on C++ and integrated into the Common Language Infrastructure. Commented May 3, 2013 at 11:01
  • 1
    @Angew Yeah, I had a fair idea; just my way of saying "sort your tags out" :) Commented May 3, 2013 at 11:02
  • Please tell me hoe the conversion should take place... Please Commented May 3, 2013 at 11:05

2 Answers 2

2

It's unrealistic to expect to be able to convert a string into a character. A string can contain 0, 1 or more characters. Which character do you want?

If you want the first character, use CACHE_SIZEoption[0], after having checked that the string is not empty.

In your case you probably want to add a check that the string's length is exactly 1 because otherwise that means the user's input is invalid. Check CACHE_SIZEoption->Length.

Sign up to request clarification or add additional context in comments.

4 Comments

I want to use the switch statement, and switch only holds char.. Moreover i want to check if user selected the option a, or b or c
Yes, I understand all that. Is there a part of my answer that you don't understand?
Why would it not be applicable?
That's a completely and utterly different question, on a different subject altogether. I reverted your edit because it makes the existing answers to your original question invalid. If you want help with the other question, ask a new question. The answer lies in the fact that you are using #define rather than setting a variable. The thing about #define is that it is processed at compile time by the pre-processor. So it's absolutely not what you want. You need to use a variable instead rather than a macro.
0

I would try

Char wh= CACHE_SIZEoption[0];

or

Char wh= CACHE_SIZEoption->ToChar();

Found here: http://msdn.microsoft.com/en-us/library/bb335877%28v=vs.110%29.aspx

4 Comments

String does not have a ToChar() method
I have used the second option, the error is " left of '.ToChar' must have class/struct/union"
While I'm still confused what I don't understand about the documentation of String, there is yet one option left.
You could for example write IConvertible^ CACHE_SIZEoption_cvt = CACHE_SIZEoption; CACHE_SIZEoption_cvt->ToChar(); but that's much messier than just subscripting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.