CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2012
    Posts
    9

    How can I have the user input the data?

    I just want to know how I could have a user input the data instead of having it fixed such as in { 56, 76, 2, 4, 5 }.
    Im aware of the three codes here to input data but I dont know how to incorporate them in the code

    Code:
         // Returns an int value from the console
      public static int GetInt(String prompt) { 
        Console.Write(prompt);   
        return int.Parse(Console.ReadLine()); 
      }
    
            // Returns a double value from the console.
      public static double GetDouble(String prompt) {
        Console.Write(prompt);   
        return double.Parse(Console.ReadLine()); 
      }
    
            // Returns a char value from the console.
      public static char GetChar(String prompt) {
        Console.Write(prompt);   
        return Console.ReadLine()[0]; 
      }
    }
    Thanks



    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[][] region = new int[3][];    // scores
                region[0] = new int[5] { 56, 76, 2, 4, 5 };  //region1 5 stores
                region[1] = new int[3] { 83, 44, 14 };      //region2 3 stores
                region[2] = new int[2] { 34, 78, };      //region3 2 stores
                double sum;       // sum of the scores for each region
                for (int i = 0; i < region.Length; i++)
                {
                    sum = 0;
                    for (int j = 0; j < region[i].Length; j++)
                        sum += region[i][j];
                    Console.Write
                       ("The average for each region {0} is ", i);
                    Console.WriteLine((sum / region[i].Length).ToString("F1"));
                }   Console.ReadKey();
            }
        }
    }
    Last edited by mxrx81; February 16th, 2012 at 08:33 PM.

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: How can I have the user input the data?

    So write a loop calling (one of) those methods prompting the user to input the data for each store...
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Feb 2012
    Posts
    9

    Re: How can I have the user input the data?

    Quote Originally Posted by BioPhysEngr View Post
    So write a loop calling (one of) those methods prompting the user to input the data for each store...
    Thats what i was thinking but I dont know exactly how to go about it.

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: How can I have the user input the data?

    Use the same structure as you used before:

    Code:
    for (int i = 0; i < region.Length; i++)
    {
        for (int j = 0; j < region[i].Length; j++)
        {
            //Assignment goes here
        }
    }
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured