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

Recommended Answers

All 4 Replies

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:

using System;

class main {
  public static void Main()
  {
    Console.Write("Enter an integer: ");
    string s = Console.ReadLine();

    try {
      int n = int.Parse(s);
      n *= 2;
      Console.WriteLine("Times two: " + n);
    }
    catch (Exception) {
      Console.WriteLine("Invalid integer");
    }

    Console.ReadKey(); // Pause
  }
}

OK, I am still getting a problem with the function, here is the updated code:

/*

* Date: 3/5/2005

* house.cs

*/

[b]using [/b]System;



[b]namespace [/b]BackGround_Process

{

class BackGround_Process

{

[b]int house[/b]([b]int [/b]length, [b]int [/b]width, [b]int [/b]height)

{

[b]int [/b]Dimension = 0;

Dimension = length * width * height;

return Dimension;

}

[b]public [/b]static void [b]Main[/b]()

{

[b]int [/b]length, width, height;

length = 0;

width = 0;

height = 0;

Console.[b]WriteLine[/b]("What are the dimensions of your house: ");

Console.[b]WriteLine[/b]("What is the length of your house? ");

string l = Console.[b]ReadLine[/b]();

length = [b]int[/b].[b]Parse[/b](l);

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

Console.[b]WriteLine[/b]("What is the width of your house? ");

string w = Console.[b]ReadLine[/b]();

width = [b]int[/b].[b]Parse[/b](w);

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

Console.[b]WriteLine[/b]("What is the height of your house? ");

string h = Console.[b]ReadLine[/b]();

height = [b]int[/b].[b]Parse[/b](h);

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

Console.[b]Write[/b]("The volume of your house is: ");

[b]int house[/b](length, width, height);

}

}

}

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:

using System;

namespace BackGround_Process {
  class BackGround_Process {
    static int house(int length, int width, int height)
    {
      return length * width * height;
    }

    public static void Main()
    {
      Console.Write("What is the length of your house? ");

      int length = int.Parse(Console.ReadLine());

      Console.WriteLine("The lenght of your house is {0}. ", length);
      Console.Write("What is the width of your house? ");

      int width = int.Parse(Console.ReadLine());

      Console.WriteLine("The width of your house is {0}. ", width);
      Console.Write("What is the height of your house? ");

      int height = int.Parse(Console.ReadLine());

      Console.WriteLine("The height of your house is {0}. ", height);
      Console.Write("The volume of your house is: ");
      Console.WriteLine(house(length, width, height));
    }
  }
}

ok that did it. Thanks for the help Narue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.