?
Solved

How do you convert a string to a char in C#

Posted on 2012-09-03
11
Medium Priority
?
762 Views
Last Modified: 2012-09-03
Hi experts! Got this error on the code below.... forgot how to convert

---cannot implicitly convert type 'string' to 'char' ----

        inputName = Console.ReadLine();

Open in new window

0
Comment
Question by:December2000
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 38361391
What is the definition of inputName
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 38361397
It needs to be a string NOT a char

exanple....

string inputName = Console.ReadLine()
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38361419
Is this what you wanted:
inputName = Console.ReadChar();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38361422
ps.  With the name of the variable inputName I imagine the comments by Neilsr are what you need.
0
 

Author Comment

by:December2000
ID: 38361440
The input definition is   ----- char QUIT = 'Z';
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 38361455
So do you want to input a string or a single keystroke?

console.getchar()  will return a single keystroke
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 501 total points
ID: 38361539
I suppose you could also do:

inputName = Console.ReadLine()[0];

Open in new window


...but that would allow entries like "Zoology" to terminate your program!
0
 

Author Comment

by:December2000
ID: 38361558
Thank you! it is saying that console.getchar()  doesn't exist in the current context
0
 
LVL 8

Assisted Solution

by:Andrei Fomitchev
Andrei Fomitchev earned 501 total points
ID: 38361585
string inputName = Console.ReadLine();

if ( inputName == "Z") { ... }

if ( inputName[0] == 'Z') { ... }
0
 
LVL 37

Assisted Solution

by:Neil Russell
Neil Russell earned 498 total points
ID: 38361610
Sorry I had my Java hea on NOT My C# head

You want

inputName = Console.Read()

That will return the next SINGLE Char in the input stream
0
 

Author Closing Comment

by:December2000
ID: 38361760
Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question