Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Wrong output value in C# programm

Good evening everyone

I recently started learning C#. Since I have already studied C / C ++ many times, and this semester I am studying Java, I thought that it would not be very difficult. But I ran into an unexpected problem.

The task is like this: "There are four persons. Person data is given in such format: name (string), height in meters (double) and weight in kilos (double). Create a class Person to store data about single person. Initial data is entered from keyboard (user input). Write a program that finds average weight of person and calculate which person is the lowest. If multiple persons are found to be the lowest, print them all. At least 3 custom methods must be implemented: for data input, calculation (-s) and for displaying results to the screen.
Print initial data in table format, average weight and all data about the lowest person(-s) to console. Usage of an array of objects is not allowed."

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The problem is that in the results table, I have fields Name, Height and Width. And for some reason, the height data is also written to the width field:

Write a person`s name-: Artem
Write a person`s height-: 1,81
Write a person`s weight-: 67

Write a person`s name-: Yaroslav
Write a person`s height-: 1,80
Write a person`s weight-: 65,5

Write a person`s name-: Ivan
Write a person`s height-: 1,78
Write a person`s weight-: 70

Write a person`s name-: Ann
Write a person`s height-: 1,78
Write a person`s weight-: 64

Write a person`s name-: x
-----------------------------------------------------------------
|       Name     |      Height (m.)     |       Weight (kg.)    |
-----------------------------------------------------------------
|   Artem        |          1,81        |    1,81(must be 67)   |
-----------------------------------------------------------------
|   Yaroslav     |          1,80        |    1,80(must be 65,5) |
-----------------------------------------------------------------
|   Ivan         |          1,78        |    1,78(must be 70)   |
-----------------------------------------------------------------
|   Ann          |          1,78        |    1,78(must be 64)   |
-----------------------------------------------------------------

And I don’t even know why this is happening.

Code:

using System;
using System.Collections.Generic;

namespace Person
{
    internal class Program
    {
        class Person{
            private string name;
            private double width, height;

            public Person(string name, double width, double height) {
                this.name = name;
                this.width = width;
                this.height = height;
            }

            public string GetName() { return name; }
            public double GetHeight() { return height; }
            public double GetWidth() { return width; }
        }

        static void Main(string[] args)
        {
            string name;
            double height, width;
            int count = 0;
            List<Person> person = new List<Person>();

            for (int i = 0; ;i++) {
                name = getName();
                if (name == "X" || name == "x") {
                    break;
                }
                height = getHeight();
                width = getWidth();
                person.Add(new Person(name, height, width));
                count++;
            }

            Console.WriteLine("-----------------------------------------------------------------");
            Console.WriteLine("|\tName\t |\tHeight (m.)\t|\tWeight (kg.)\t|");
            Console.WriteLine("-----------------------------------------------------------------");
            for (int i = 0; i < count; i++) {
                Console.WriteLine("|   {0}\t |\t{2,8:f2}\t|\t{2,8:f2}\t|", person[i].GetName(), person[i].GetHeight(), person[i].GetWidth());
                Console.WriteLine("-----------------------------------------------------------------");
            }

            double avg = SetAverageWidth(person, count);
            Console.WriteLine("\nAvergae width-: {0:f2}", avg);
            Lowest(person, count);
        }

        static string getName() {
            Console.Write("\nWrite a person`s name-: ");
            string name = Console.ReadLine();
            return name;
        }
        static double getWidth(){
            Console.Write("Write a person`s width-: ");
            double width = double.Parse(Console.ReadLine());
            return width;
        }
        static double getHeight(){
            Console.Write("Write a person`s height-: ");
            double height = double.Parse(Console.ReadLine());
            return height;
        }
        static double SetAverageWidth(List<Person> person, int count)
        {
            double res = 0;
            for (int i = 0; i < count; i++)
            {
                //if (person[i] == null) { break; }
                res += person[i].GetWidth();
            }
            return res / count;
        }
        static void Lowest(List<Person> person, int count) {
            double max, min;
            int i = 0;
        
            max = min = count;
            for (int j = 0; j < count; j++) {
                if (person[i].GetHeight() > max)
                    max = person[i].GetHeight();
            }

            Console.WriteLine("\n-*-*-*-*-*-*-*-*-*-*-*-*- The Lowest -*-*-*-*-*-*-*-*-*-*-*-*-*- ");
            Console.WriteLine("-----------------------------------------------------------------");
            for (int j = 0; j < count; j++) {
                if (person[j].GetHeight() == max)
                {
                    Console.WriteLine("|   {0}\t |\t{2,8:f2}\t|\t{2,8:f2}\t|", person[j].GetName(), person[j].GetHeight(), person[j].GetWidth());
                    Console.WriteLine("-----------------------------------------------------------------");
                }
            }
        }
    }
}

>Solution :

Your format string only uses {0} and {2}. You’re missing {1} in line 50:

Console.WriteLine("|   {0}\t |\t{2,8:f2}\t|\t{2,8:f2}\t|", person[i].GetName(), person[i].GetHeight(), person[i].GetWidth());

and line 106:

Console.WriteLine("|   {0}\t |\t{2,8:f2}\t|\t{2,8:f2}\t|", person[j].GetName(), person[j].GetHeight(), person[j].GetWidth());

Change the second format string to {1,8:f2}.

Other than that:

  • check the order of arguments in the constructor of Person, you accidentally flipped width and height when you create a new person
  • read about properties. C# does not use Get...() and Set...() methods like Java
  • check whether you want width or weight of the person
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading