Help me to run this.
using System;
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args);
int Heron(int a, int b, int c)
{
int p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
int main()
{
int a,b,c;
Console.WriteLine << " a: ";
Console.ReadLine >> a;
Console.WriteLine << " b: ";
Console.ReadLine >> b;
Console.WriteLine << " c: ";
Console.ReadLine >> c;
if(a<=0 || b<=0 || c<=0){
Console.WriteLine << " All values must be great from 0!\n";
system("pause");
return 1;
}
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Roy SPosted Dec 18, 2011, 8:21 AM
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
Console.WriteLine(" a: ");
a = int.Parse(Console.ReadLine());
Console.WriteLine(" b: ");
b = int.Parse(Console.ReadLine());
Console.WriteLine(" c: ");
c = int.Parse(Console.ReadLine());
if (a <= 0 || b <= 0 || c <= 0)
{
Console.WriteLine(" All values must be great from 0!\n");
Console.Read();
}
if (!(c < a + b) || !(a < b + c) || !(b < a + c))
{
Console.WriteLine("Triangle no exist!\n");
Console.Read();
}
else
{
Console.WriteLine("area triangle = {0}", Heron(a, b, c));
Console.Read();
}
}
static int Heron(int a, int b, int c)
{
int p = (a + b + c) / 2;
return (int)Math.Sqrt(p * (p - a) * (p - b) * (p - c));
}
}
}
The things that were 'wrong':
Console.WriteLine << " a: ";
Seems like a mix between c# and c++ syntaxis, it should be something like this:
Console.WriteLine(" a: ");
Console.ReadLine >> b;
Should be something like:
b = int.Parse(Console.ReadLine());
static void Main(string[] args);
//...
int main()
{
//...
}
The entry point (main function) should be defined as:
static void Main(string[] args)
{
//...
}
system("pause");
To pause the execution and wait for the user to press a key, try:
Console.Read()
return 1;
Since the main function returns void, you don't need to return 1 or 0.
return sqrt(p*(p-a)*(p-b)*(p-c));
sqrt is located in the Math class so it should be:
return Math.Sqrt(p*(p-a)*(p-b)*(p-c));
Also it should be cast to an int:
return (int)Math.Sqrt(p*(p-a)*(p-b)*(p-c));
int Heron(int a, int b, int c)
And lastly all function used in a console application should be marked as static
int static Heron(int a, int b, int c)
Sabaka SabakowPosted Dec 18, 2011, 11:49 AM