View previous topic :: View next topic |
Author |
Message |
richardyork -
Joined: 22 Jun 2004 Posts: 411 Location: United Kingdom
|
Posted: Thu Mar 17, 2005 11:55 am Post subject: C# - Need Help For College Project |
|
|
Hi everyone,
I know this sort of question shouldn't be here because it is nothing to do with Abyss! But I need the help ASAP if you can. I'm doing C# at college at the moment and this is my very first program! All it does is add the users age and shoe size together....silly I know LOL! I have got it to input and display the users age and shoe size, but I can't get it to add them together and display the answer :-( From searching Google etc I think it may have something ti do with "int"? Any help from somebody with knowledge of C# that could help me would be VERY appreciated! I do realise this isn't an appropriate question for Aprelium's forums but I am in desperate need of help because I don't understand this problem!!
BTW: It's a Console Application and ignore the comments, some are in the wrong place etc and confusing....well, I am a TOTAL Beginner LOL!
Here's the code:
Code: | using System;
namespace InputWithReadLineV2
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
/*Declaring the variable, YourAge, as the data type, string */
string YourAge;
/*WriteLine prints whatever is in the brackets and speech marks
to the console screen*/
System.Console.WriteLine("Hello, Please Enter Your Age");
System.Console.WriteLine("");
/*This is where the program takes in the value for the "YourAge"
string*/
YourAge=System.Console.ReadLine();
System.Console.Write("");
System.Console.WriteLine("I have printed the age you entered on the screen");
System.Console.Write("Your Age Is ");
/*This puts the value that the user has input in the "YourAge"
string at the end of the previous line because the function
.Write was used instead of .WriteLine*/
System.Console.WriteLine(YourAge);
System.Console.WriteLine("");
/*Declaring the variable, YourAge, as the data type, string */
string YourShoeSize;
/*WriteLine prints whatever is in the brackets and speech marks
to the console screen*/
System.Console.WriteLine("Hello, Please Enter Your Shoe Size");
System.Console.WriteLine("");
/*This is where the program takes in the value for the "YourAge"
string*/
YourShoeSize=System.Console.ReadLine();
System.Console.Write("");
System.Console.WriteLine("I have printed the shoe size you entered on the screen");
System.Console.Write("Your Shoe Size Is ");
/*This puts the value that the user has input in the "YourAge"
string at the end of the previous line because the function
.Write was used instead of .WriteLine*/
System.Console.WriteLine(YourShoeSize);
System.Console.WriteLine("");
System.Console.WriteLine("To exit this program, press the Enter key");
System.Console.ReadLine();
}
}
}
|
Anything else, just ask!
Thank You! _________________ Please SEARCH the forums BEFORE asking questions! |
|
Back to top |
|
 |
Anonymoose -
Joined: 09 Sep 2003 Posts: 2192
|
Posted: Thu Mar 17, 2005 2:37 pm Post subject: |
|
|
Do you mean you want to add together the number for age and number for shoe size, or do you mean you want to display them both on the same line?
If you want to add them together, do not declare them as 'string' - use the 'int' that you mentioned, which is short for integer.
Code: |
int shoesize;
int age;
int total;
total = age + shoesize;
|
|
|
Back to top |
|
 |
richardyork -
Joined: 22 Jun 2004 Posts: 411 Location: United Kingdom
|
Posted: Thu Mar 17, 2005 3:25 pm Post subject: Thank You! |
|
|
Hi Anonymoose,
Thank you for your reply! Im still having problems though :-(
I have tried your suggestion but it still dosnt work! I did want to just add them together, your right. It now when I hover over "System.Console.ReadLine" It displays this in a tool tip: "Cannot implicitly convert type 'string' to 'int'" I am confused! Thank you for taking the time Anonymoose!
Heres the new code with the changes:
Code: |
using System;
namespace InputWithReadLineV2
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int YourAge;
System.Console.WriteLine("Hello, Please Enter Your Age");
System.Console.WriteLine("");
YourAge=System.Console.ReadLine();
System.Console.Write("");
System.Console.WriteLine("I have printed the age you entered on the screen");
System.Console.Write("Your Age Is ");
System.Console.WriteLine(YourAge);
System.Console.WriteLine("");
int YourShoeSize;
System.Console.WriteLine("Now Please Enter Your Shoe Size");
System.Console.WriteLine("");
YourShoeSize=System.Console.ReadLine();
System.Console.Write("");
System.Console.WriteLine("I have printed the shoe size you entered on the screen");
System.Console.Write("");
System.Console.Write("Your Shoe Size Is ");
System.Console.WriteLine(YourShoeSize);
System.Console.WriteLine("");
/*This is where the two values will be added*/
int Total;
Total = YourAge + YourShoeSize;
System.Console.WriteLine("");
System.Console.WriteLine("");
System.Console.WriteLine("");
System.Console.WriteLine("");
System.Console.WriteLine("To exit this program, press the Enter key");
System.Console.ReadLine();
}
}
}
|
I have got rid of those comments so you can see it more clearly.
Thank You once again Anonymoose! _________________ Please SEARCH the forums BEFORE asking questions! |
|
Back to top |
|
 |
k1ll3rdr4g0n -
Joined: 04 Jul 2004 Posts: 609
|
Posted: Fri Mar 18, 2005 2:17 am Post subject: |
|
|
I dont know about C# but when I teaching someone VB and i had them declare a string. Then add a textbox, we were working with numbers then, and in order to get the number from the textbox we had to do a sText val(text1.text). Note i know string is the best for numbers but it was introductory :).
I dont know if this will help but check your C# manual for some syntax like Val(). and try YourShoeSize=Val(System.Console.ReadLine());
Oh and try and put the variables at the begining. I know some languages dont like it when you delcare variables all over the place ;o. _________________
 |
|
Back to top |
|
 |
richardyork -
Joined: 22 Jun 2004 Posts: 411 Location: United Kingdom
|
Posted: Fri Mar 18, 2005 2:28 am Post subject: |
|
|
Thank You, k1ll3rdr4g0n!
I will try this when I get to college tomorrow first thing! I'm not lucky enough to own a copy of Visual Studio :-( so the only chance I get to mess with it is when I'm at college :-( Thank you anyway!! It is much appreciated! I was working on it for ages earlier, but no luck! I hate it when something gets the better of me! It all gets sorted in the end though, with or without the help of other people...LOL
Thank You For Your Input! _________________ Please SEARCH the forums BEFORE asking questions! |
|
Back to top |
|
 |
TheLinker -
Joined: 05 Apr 2002 Posts: 165 Location: Oslo, Norway
|
Posted: Fri Mar 18, 2005 2:49 pm Post subject: Re: |
|
|
When converting console input (string) to integer, I use Convert... like YourAge=Convert.ToInt32( System.Console.ReadLine() ); |
|
Back to top |
|
 |
richardyork -
Joined: 22 Jun 2004 Posts: 411 Location: United Kingdom
|
Posted: Fri Mar 18, 2005 6:40 pm Post subject: |
|
|
Thank You, TheLinker,
How would I implement that into my code to add them together (age and shoe size) and display the answer? _________________ Please SEARCH the forums BEFORE asking questions! |
|
Back to top |
|
 |
admin Site Admin
Joined: 03 Mar 2002 Posts: 1332
|
Posted: Fri Mar 18, 2005 10:19 pm Post subject: |
|
|
richardyork wrote: | Thank You, TheLinker,
How would I implement that into my code to add them together (age and shoe size) and display the answer? |
According to TheLinker, replace in your code:
Code: | YourShoeSize=System.Console.ReadLine(); |
with
Code: | YourShoeSize=Convert.ToInt32(System.Console.ReadLine()); |
and replace:
Code: | YourAge=System.Console.ReadLine(); |
with
Code: | YourAge=Convert.ToInt32(System.Console.ReadLine()); |
The rest of the code seems to be fine. |
|
Back to top |
|
 |
richardyork -
Joined: 22 Jun 2004 Posts: 411 Location: United Kingdom
|
Posted: Fri Mar 18, 2005 10:48 pm Post subject: |
|
|
Thank You,
But, If I do as you say then I still get the error message: "Cannot implicitly convert type 'int' to 'string'" This is confusing me sooo much! Could somebody copy and paste the code from above and modify it to do what I want?
I know this is a lot to ask but it is really bugging me!! :-(
BTW:
I am not using Visual Studio, Im using the next best thing, ic#code!
Please Help Me!
Thank You Everyone So Far!!!!!! _________________ Please SEARCH the forums BEFORE asking questions! |
|
Back to top |
|
 |
Anonymoose -
Joined: 09 Sep 2003 Posts: 2192
|
Posted: Sat Mar 19, 2005 10:46 am Post subject: |
|
|
It was a fairly simple suggestion - don't be afraid to experiment a bit more before giving up!
Lines in bold were modified / added.
Quote: |
using System;
namespace Test
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int YourAge;
System.Console.WriteLine("Hello, Please Enter Your Age");
System.Console.WriteLine("");
YourAge=Convert.ToInt32( System.Console.ReadLine() );
System.Console.Write("");
System.Console.WriteLine("I have printed the age you entered on the screen");
System.Console.Write("Your Age Is ");
System.Console.WriteLine(YourAge);
System.Console.WriteLine("");
int YourShoeSize;
System.Console.WriteLine("Now Please Enter Your Shoe Size");
System.Console.WriteLine("");
YourShoeSize=Convert.ToInt32( System.Console.ReadLine() );
System.Console.Write("");
System.Console.WriteLine("I have printed the shoe size you entered on the screen");
System.Console.Write("");
System.Console.Write("Your Shoe Size Is ");
System.Console.WriteLine(YourShoeSize);
System.Console.WriteLine("");
/*This is where the two values will be added*/
int Total;
Total = YourAge + YourShoeSize;
System.Console.WriteLine("The total of your age and shoe size is");
System.Console.WriteLine(Total);
System.Console.WriteLine("");
System.Console.WriteLine("");
System.Console.WriteLine("To exit this program, press the Enter key");
System.Console.ReadLine();
}
}
}
|
Compiles fine.
Sample output :
Quote: |
Hello, Please Enter Your Age
25
I have printed the age you entered on the screen
Your Age Is 25
Now Please Enter Your Shoe Size
13
I have printed the shoe size you entered on the screen
Your Shoe Size Is 13
The total of your age and shoe size is
38
To exit this program, press the Enter key
|
Thanks for the link to that compiler BTW! Much less bloat for simple console work than installing the whole of Visual Studio! Definitely one to add to my toolkit... |
|
Back to top |
|
 |
richardyork -
Joined: 22 Jun 2004 Posts: 411 Location: United Kingdom
|
Posted: Sat Mar 19, 2005 7:41 pm Post subject: |
|
|
It's a pleasure Anonymoose, I'm glad to be of service to you for a change...lol!!
Believe me, I had been up till 02:00AM trying different configurations and I still had no luck!! Your answer is similar to what I had tried but I'll let you know where I slipped up, as soon as I get chance to try it!!! (It's hard having two kids and a house to look after at the age of 17!)
P.S. I use Visual Studio at college and ic#code at home (because I can't afford Visual Studio) but I think ic#code does just as well of a job that Visual Studio does!!!!
Thank You to everyonme that has helped me! I do honestly really appreciate this!!
See You Later!! _________________ Please SEARCH the forums BEFORE asking questions! |
|
Back to top |
|
 |
richardyork -
Joined: 22 Jun 2004 Posts: 411 Location: United Kingdom
|
Posted: Sun Mar 20, 2005 3:32 pm Post subject: |
|
|
I found where I was going wrong!
I was using:
Quote: |
YourShoeSize=Convert.ToInt32( System.Console.WriteLine() );
|
Instead Of:
Quote: |
YourShoeSize=Convert.ToInt32( System.Console.ReadLine() );
|
Thank You, Anonymoose! _________________ Please SEARCH the forums BEFORE asking questions! |
|
Back to top |
|
 |
|