using functions

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 573
Reputation: Dark_Omen is an unknown quantity at this point 
Solved Threads: 6
Dark_Omen Dark_Omen is offline Offline
Posting Pro

using functions

 
0
  #1
Mar 5th, 2005
Hello,

I just started laerning c# and I made this program that took the dimension of a box(house), and then use a function to get the volume of the box_house. One problem that I encountered was that I dont know how to make a string into an int. Here is a link to the code http://www.nomorepasting.com/paste.php?pasteID=33513.
If anyone can help that would be great.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: using functions

 
0
  #2
Mar 5th, 2005
ReadLine returns a string, and you can use the Parse method of int to convert a string representing a valid integer into an int value:
  1. using System;
  2.  
  3. class main {
  4. public static void Main()
  5. {
  6. Console.Write("Enter an integer: ");
  7. string s = Console.ReadLine();
  8.  
  9. try {
  10. int n = int.Parse(s);
  11. n *= 2;
  12. Console.WriteLine("Times two: " + n);
  13. }
  14. catch (Exception) {
  15. Console.WriteLine("Invalid integer");
  16. }
  17.  
  18. Console.ReadKey(); // Pause
  19. }
  20. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 573
Reputation: Dark_Omen is an unknown quantity at this point 
Solved Threads: 6
Dark_Omen Dark_Omen is offline Offline
Posting Pro

Re: using functions

 
0
  #3
Mar 5th, 2005
OK, I am still getting a problem with the function, here is the updated code:

 
/*

* Date: 3/5/2005

* house.cs

*/

using System;



namespace BackGround_Process

{

class BackGround_Process

{

int house(int length, int width, int height)

{

int Dimension = 0;

Dimension = length * width * height;

return Dimension;

}

public static void Main()

{

int length, width, height;

length = 0;

width = 0;

height = 0;

Console.WriteLine("What are the dimensions of your house: ");

Console.WriteLine("What is the length of your house? ");

string l = Console.ReadLine();

length = int.Parse(l);

Console.WriteLine("The lenght of your house is {0}. ", length);

Console.WriteLine("What is the width of your house? ");

string w = Console.ReadLine();

width = int.Parse(w);

Console.WriteLine("The width of your house is {0}. ", width);

Console.WriteLine("What is the height of your house? ");

string h = Console.ReadLine();

height = int.Parse(h);

Console.WriteLine("The height of your house is {0}. ", height);

Console.Write("The volume of your house is: ");

int house(length, width, height);

}

}

}

Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: using functions

 
0
  #4
Mar 6th, 2005
First, house needs to be static unless you plan on calling it on an object. Second, any call to a function does not include the return type or the function call will be seen as a function declaration. Try this:
  1. using System;
  2.  
  3. namespace BackGround_Process {
  4. class BackGround_Process {
  5. static int house(int length, int width, int height)
  6. {
  7. return length * width * height;
  8. }
  9.  
  10. public static void Main()
  11. {
  12. Console.Write("What is the length of your house? ");
  13.  
  14. int length = int.Parse(Console.ReadLine());
  15.  
  16. Console.WriteLine("The lenght of your house is {0}. ", length);
  17. Console.Write("What is the width of your house? ");
  18.  
  19. int width = int.Parse(Console.ReadLine());
  20.  
  21. Console.WriteLine("The width of your house is {0}. ", width);
  22. Console.Write("What is the height of your house? ");
  23.  
  24. int height = int.Parse(Console.ReadLine());
  25.  
  26. Console.WriteLine("The height of your house is {0}. ", height);
  27. Console.Write("The volume of your house is: ");
  28. Console.WriteLine(house(length, width, height));
  29. }
  30. }
  31. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 573
Reputation: Dark_Omen is an unknown quantity at this point 
Solved Threads: 6
Dark_Omen Dark_Omen is offline Offline
Posting Pro

Re: using functions

 
0
  #5
Mar 7th, 2005
ok that did it. Thanks for the help Narue.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 15206 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC