954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Statements loops

Hello

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

namespace ConsoleApplication1
{
    class WholeNumbersFor
    {
        private int numOfInPut;
        private int sum;

        public void start();
    
        WriteProgramInfo();
        ReadInPut();
        SumNumbers();
        ShowResults();
    }
    private void ReadInPut()
{

    Console.Write ("Number of Values to sum?");
    NumOfInPut = int.Parse(console.Readkey());
    console.Writeline();
}
    private void WriteProgramInfo()
{
    console.Writeline("\n\n ++++++ Summation of whole numbers ++++++");
    console.Writeline();

}
    private void SumNumbers()
{

    int index;
    int Sum = 0;

    for (index = 0; index < NumOfInPut; index++)
{
}


    private void ShowResults();
{
}

    }


If you put in 9 in to Number of Values to sum? then it going to come up like 9 lines that say for example value no 1 and so on to no 9 that you can enter your number you want in to it.
I don't understand how i'm going to put it in. i know how to do it if you know the value of it but now it is 0 so i dont know how i can make it

Johan__
Newbie Poster
17 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

I'm looking at this, but the first thing I see is a typo on line 44 (the semi-colon).
Also the closing brace on 19 needs to be moved to 47 or 49.
Also, C# is case-sensitive, so your numOfInPut should match the case of NumOfInPut.

...and if this was meant to be the main module of a console app, you're missing Main().
Did you accidentally delete it?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Your code seems to be all messed up.
You got to use an Array or better a List to store your inputted numbers
Write an input loop for that.
Then loop trough your array or list and sum all the numbers.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Have a look at this and let us know if it needs clarifying:

public void Implementation()
        {
            int mySum = GetNumbers(GetNumberCount()).Sum();
        }
        public int GetNumberCount()
        {
            int iCount = 0;
            Console.WriteLine("Enter number of values to sum: ");
            while (int.TryParse(Console.ReadLine(), out iCount) == false || iCount <= 0)
                Console.WriteLine("Invalid number, try again");
            return iCount;
        }
        public List<int> GetNumbers(int numCount)
        {
            List<int> returnList = new List<int>();
            for (int i = 0; i < numCount; i++)
            {
                int iValue = 0;
                Console.WriteLine("Enter Number " + (i + 1).ToString() + ":");
                while (int.TryParse(Console.ReadLine(),out iValue) == false)
                    Console.WriteLine("Invalid number, try again");
                returnList.Add(iValue);
            }
            return returnList;
        }
skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

thx for the help. i have one questien. if i want to type in numbers in to a loop like this Console.WriteLine("What is the number you want nr " +index ); when it start it goung to say What is the number you want nr 1 and after one i want to put in a number how do i do it?

Johan__
Newbie Poster
17 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Console.ReadLine()
...then convert the result to a numeric type like an integer

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

thx for it but is it a way to get it next to it and not under it?

Johan__
Newbie Poster
17 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Using .WriteLine() implies that you want to start a newline (it automatically appends Environment.Newline to the end of the string). You should use .Write() instead if you want to achieve this functionality.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

int a;
for(a=0;a<=10;a++)
{
listbox1.items.add(a.tostring());
}

hussain_ahmed
Newbie Poster
2 posts since Jan 2012
Reputation Points: 8
Solved Threads: 0
 

Yes, just put it next to it.
Are you using Visual Studio to edit your code?

using System;

namespace DW_409292_CS2_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Enter a number");
         string strData = Console.ReadLine().Trim();
         int intData = 0;

         if (!int.TryParse(strData, out intData))
         {
            Console.WriteLine("Invalid number: " + strData);
            return;
         }

         Console.WriteLine(intData.ToString() + " is a nice number");
      }
   }
}
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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

namespace ConsoleApplication1
{
class WholeNumbersFor
{
private int numOfInPut = 0;
private int sum = 0;

public void start()
{

WriteProgramInfo();
ReadInPut();
SumNumbers();
ShowResults();
}
private void ReadInPut()
{

Console.Write("Number of Values to sum?");
try
{

numOfInPut = int.Parse(Console.ReadLine().Trim());
}
catch
{
Console.WriteLine("Enter valid number");
}
Console.WriteLine();
}
private void WriteProgramInfo()
{
Console.WriteLine("\n\n ++++++ Summation of whole numbers ++++++");
Console.WriteLine();

}
private void SumNumbers()
{

int index;
int temp = 0;


for (index = 0; index < numOfInPut; index++)
{
try
{
Console.WriteLine("Enter the" + index.ToString() + "th Value");
temp = int.Parse(Console.ReadLine().Trim());
if (temp == 0)
{
index--;
}
else
{
sum += temp;
}
}
catch
{
Console.WriteLine("enter Valid integer");
index--;
}
}
}

private void ShowResults()
{
Console.WriteLine("\n\n ++++++ Summation of whole numbers is"+ sum.ToString() +" ++++++");
Console.WriteLine();
}
}
}

mani-hellboy
Junior Poster in Training
69 posts since Feb 2012
Reputation Points: 0
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: