• 0

Basic C# Newb Questions


Question

So I have started learning C#...I have a little bit of a background in C++ so I was wondering if someone could provide a clear answer, how do I go about doing something like this in C#

in C++ it would be

cout<<"enter your name";
cin>>namel;

I can't seem to find a clear and simple answer about how to do this in C# either through the Console or through a Windows Application.

Thanks so much in advance. :blush:

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

what the hell am i doing wrong?

this is the simple code:

Console.WriteLine("Please enter the number of hours you worked");
 	 int hours = Console.Read();
 	 Console.WriteLine("Please enter your hourly wage");
 	 int wage = Console.Read();
 	 int pay = hours * wage;
 	 Console.WriteLine("You have made: ");
 	 Console.WriteLine(pay);

and it displays this:

Please enter the number of hours you worked
10
Please enter your hourly wage
You have made:
2352
Press any key to continue

which obviously isn't right....thank you for any help

Link to comment
Share on other sites

  • 0

Is it not letting you enter hourly wage? I don't see that in your input. ReadLine may be a better way. The way you're doing it now, the characters are returned as ints, so the numbers will be wrong.

Link to comment
Share on other sites

  • 0
yeah, it won't let me enter the hourly wage...i will try it with ReadLine

The problem is that Read will return any characters you put in up until a newline character.

So:

int i;
char c;
while (true)
{
 i = Console.Read ();
 if (i == -1) break;
 c = (char) i;
 Console.WriteLine ("Echo: {0}", c);
}
Console.WriteLine ("Done");
return 0;

would output:

Echo: 1

Echo: 0

Done

And since 1=49 in ascii and 0=48 in ascii, you're getting 49*48=2352.

Edited by kjordan2001
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.