• Advertisement
Sign in to follow this  

help with C# please

This topic is 3575 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im having a little trouble with this code it wont compile and I cant figure out why. Please Help!
using System;
using System.IO;

public class Test
{
    public static void Main()
    {
        Console.WriteLine("Enter a number");
        int input = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(Test.getNum(input));

    }


    public static int getNum(int n)
    {
        for (int i = 0; i < n; i++)
        {
            if (i <= n)
            {
                return i;
            }
        }
        return;
    }
}

Thanks Andy

Share this post


Link to post
Share on other sites
Advertisement
Next time you should mention your error message but in this case your problem is caused by the 2nd return in getNum since it doesn't return an integer as you promise csharp will bark at you!

Share this post


Link to post
Share on other sites
I'm not sure what the goal of your program was but here is a version that works.
I am very new to programming, so go easy on me if this solution isn't what you were looking for :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Goofing_Around
{
class Program
{

static void Main(string[] args)
{

Console.WriteLine("Enter a number: ");
int input = Convert.ToInt32(Console.ReadLine());

//This calls the function that you made.
getNum(input);
Console.ReadLine();

}

//I made the variable that the function accepts the same type as the one
//you get from the user.
public static void getNum(Int32 n)
{
//I don't think you need the if statement there from what I could
//tell because the for loop will print each number.
for (int i = 0; i < n; i++)
{
//Instead of having this return anything you can just print i
//the number of times for the number entered by the user.
Console.WriteLine(i);

}



}
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement