0

After running the program I can only insert data for the first student, and after that it only displays what I should insert, but does not give me the right to do so. No compile errors. Although I think that the problem is when I assign the variables, it might be an issue there, because I don't know if I should initialise the int with 0 or not. I obtain a lot of compiling error if I don't initialise the variables. Also, why does it display the Student phone number with a random number, "48" in my case?

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

namespace CustomCollections
{
    internal class Program
    {
        string studentNumber;
        string studentName;
        string age;
        int phoneNumber;

        public static void enterData()
        {
            int studentNumber, age, phoneNumber;
            string studentName;

            Console.WriteLine("Enter Student Number:");
            studentNumber = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter Name:");
            studentName = Console.ReadLine();

            Console.WriteLine("Enter Age:");
            age = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter phone number:");
            phoneNumber = int.Parse(Console.ReadLine());
        }

        public static void displayData()
        {
            string studentNumber=null;
            string studentName=null;
            string age=null;
            int phoneNumber=0;

            Console.WriteLine("Student Number:{0}",studentNumber);
            Console.WriteLine("Student Name:{0}",studentName);
            Console.WriteLine("Student Age:{0}", age);
            Console.WriteLine("Student phone number:{0}",phoneNumber);
            Console.ReadKey();


        }

        public static void Main(String[] args)
        {
            string studentNumber;
            string studentName;
            string age;
            int phoneNumber;
            enterData();
            displayData();
        }

    }
}

I will also display the result here:

Enter Student Number:
2
Enter Name:
seli
Enter Age:
22
Enter phone number:
2207885
Student Number:
Student Name:
Student Age:
Student phone number:48
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Corina
  • 137
  • 1
  • 1
  • 12
  • 2
    You have defined the same set of variables with the same names in four different places. – Lance U. Matthews Mar 27 '19 at 18:37
  • I commented 2 pairs of them, and let the pair with the initialisation of the variables, and I obtain the same errors. Even so, I don't think that I should obtain run time errors by defining a variable more than once. – Corina Mar 27 '19 at 18:50

1 Answers1

2

You have variables named studentNumber, studentName, age, and phoneNumber defined in four different places. Local variables with the same name in different methods do not represent the same storage. In order for the values read by enterData() to be accessible to displayData() you either need to pass them as parameters or store them in class-level, not local, variables; the latter will require the least changes to your code.

After making these changes...

  1. Add the static modifier to your fields so you can access them from static methods.
  2. Change the type of the studentNumber and age fields from string to int since you are parsing them to int.
  3. Delete the local studentNumber, studentName, age, and phoneNumber variables from the enterData(), displayData(), and Main() methods.

...that results in this code...

internal class Program
{
    static int studentNumber;
    static string studentName;
    static int age;
    static int phoneNumber;

    public static void enterData()
    {
        Console.WriteLine("Enter Student Number:");
        studentNumber = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter Name:");
        studentName = Console.ReadLine();

        Console.WriteLine("Enter Age:");
        age = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter phone number:");
        phoneNumber = int.Parse(Console.ReadLine());
    }

    public static void displayData()
    {
        Console.WriteLine("Student Number:{0}", studentNumber);
        Console.WriteLine("Student Name:{0}", studentName);
        Console.WriteLine("Student Age:{0}", age);
        Console.WriteLine("Student phone number:{0}", phoneNumber);
        Console.ReadKey();
    }

    public static void Main(String[] args)
    {
        enterData();
        displayData();
    }
}

...which worked as-expected for me.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
  • I did exactly what you said and I obtain the same error: Enter Student Number: 2 Enter Name: seli Enter Age: 22 Enter phone number: 072222316 Student Number:0 Student Name: Student Age:0 Student phone number:0 – Corina Mar 27 '19 at 19:00
  • after I press run I complete the data for the first student , but I can't add a second student. It allows me only to complete the data for one student , and after that it shows Student Number:0 followed by Student Name: followed by Student Age:0 followed by Student phone number:0. After this it requires me to press any key and it's done. – Corina Mar 27 '19 at 19:03
  • 1
    Without some kind of loop your code will only prompt for one student and then exit. It appears to be printing out default values for those variables. They are only defined as fields and not in any of those three methods, right? – Lance U. Matthews Mar 27 '19 at 19:06
  • yes, I did what you told me to , deleted them from all those methods and write them with static before any method in the internal class Program – Corina Mar 27 '19 at 19:08
  • I understand that I need a loop, but , why does it display those values after I insert data for the first student? – Corina Mar 27 '19 at 19:09
  • In the question and then again here in the comments you mentioned not being able to specify additional students, so I wanted to make sure you understood it's simply not coded to do that. Just to confirm, you left `enterData()` as-is other than deleting those variables? You didn't change `studentNumber = int.Parse(Console.ReadLine());` to just `int.Parse(Console.ReadLine());`, did you? – Lance U. Matthews Mar 27 '19 at 19:13
  • I just deleted the declaration of the variables from everywhere, and then I wrote them in internal class Program , as you showed me above – Corina Mar 27 '19 at 19:18
  • 1
    I have updated my answer with the complete code that works for me. – Lance U. Matthews Mar 27 '19 at 19:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190784/discussion-between-corina-and-bacon). – Corina Mar 27 '19 at 19:22