Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello forum this is my first post am pretty excited and nervous but anyways im creating a practice code with different things to practice C#. i am a little more familiar with c++ so the difference gets frustrating with the little things but anyways im having trouble with calling my "info()" method and the do while loops in my set and get for age and weight i explained in more detail the problems im having in the code with backslash. i am a beginner in csharp .please have patience :D. thank you in advance.



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

namespace the_helped
{
    class Animal
    {
        private string name;
        private string race;
        private double weight;
        private double age;

        public double _age
        {
            get
            {
                return age;
            }
            set
            {
                if (value > 0 && value <= 60) age = value;
                else
                {
                    do//this do while is not working like i want it too. when an invalid amount is put in by the user it repeats please enter dog's age but immediatly after it says please enter dog's weight (the same this happens with weight but in vice versa) what im trying to accomplish is getting the question "please enter dog's weight: " to be repeated until the if statement is true. for example this is what my program does:"Please enter dog's age: 500(user input)Invalid Age!Dog's Age: 1 "Please enter dog's age: Please enter dog's weight: " until you enter a valid age number i want my program to keep asking instead of asking for the age again but immediatly asking for the weight.The same problem i have with age, i have with weight.
                    {
                        age = 1;
                        Console.WriteLine("Invalid Age!\nDog's age: " + value);
                        Console.Write("Please enter dog's age: ");

                    } while (value < 0 && value > 25);
                }
            }
        }
        public double _weight
        {

            get
            {
                return weight;
            }
            set
            {
                if (value > 0 && value <= 60) weight = value;
                else
                {
                    do
                    {
                        weight = 1;
                        Console.WriteLine("Invalid Weight!\nDog's weight: " + value);
                        Console.Write("Please enter dog's weight: ");

                    } while (value < 0 && value >= 60);

                }



            }
        }

        public Animal()
        {
            Console.Write("\nThese are the constructor details : ");

        }
        public void getdetails()
        {
            age = 1;
            weight = 1;
            Console.WriteLine("\n\nName: No Name");
            Console.WriteLine("Race: No Race");
            Console.WriteLine("Weight: " + weight);
            Console.WriteLine("Age: " + age);
            Console.WriteLine();
        }
        public void info()//heres another problem im having. i know its not necessary but in my practice code i want to call on a method to show the results but its a little different when using different classes in C# so im getting a little confused. its not like c++ where you call the method in main and then put inside the parameters (n,m) and send them to (x,y) method i know its the same concept but its just written differently. i know that the parenthesis are empty but i tried writing inside a variable for each but it was not working so it took them out        
{
            Console.WriteLine(dog.name);
            Console.WriteLine(dog.race);
            Console.WriteLine(dog.weight);
            Console.WriteLine(dog.age);
            Console.ReadLine();
        }


        public static void Main(String[] args)
        {
            Animal dog = new Animal();

            dog.getdetails();

            Console.Write("Please enter your dog's name: ");
            dog.name = Console.ReadLine();
            Console.Write("Please enter your dog's race: ");
            dog.race = Console.ReadLine();
            Console.Write("Please enter your dog's weight: ");
            dog._weight = double.Parse(Console.ReadLine());
            Console.Write("Please enter your dog's age: ");
            dog._age = double.Parse(Console.ReadLine());


            
        }
    }
}
Posted
Comments
CaseLost 1-Oct-13 15:45pm    
I just realized that I had to change them to value but when I ask for the dog's weight or age and the user puts in an invalid number the program tells the user "invalid" and shows the default number and instead of asking the user to put in a valid number and wait till he does it will just immediately jump to the next question. would I have to create a method that calls upon each question I ask and put it in the class it would correspond to?

1 solution

Hmm. This would do the same in C++:
C#
do
{
    age = 1;
    Console.WriteLine("Invalid Age!\nDog's age: " + value);
    Console.Write("Please enter dog's age: ");
} while (value < 0 && value > 25);
Since you don't change the value of value inside the loop...how do you expect it to do anything except what you see?

Your second problem is again the same in C++
C#
public void info()
{
    Console.WriteLine(dog.name);
    Console.WriteLine(dog.race);
    Console.WriteLine(dog.weight);
    Console.WriteLine(dog.age);
    Console.ReadLine();
}
Should be using the instance variables, since it is a non-static class method:
C#
public void info()
{
    Console.WriteLine(name);
    Console.WriteLine(race);
    Console.WriteLine(weight);
    Console.WriteLine(age);
    Console.ReadLine();
}
You then call it from your main method like this:
C#
Animal dog = new Animal();
dog.age= 2.5;
dog.weight = 23.7;
dog.info();


C# also has different conventions for naming things, which it woudl be a good idea for you to follow - you can read it here: http://www.scribd.com/doc/56782158/All-In-One-Code-Framework-Coding-Standards[^] - but basically properties shoudl start with an Uppercase letter, as should methods, property base fields should be the property with an unerscore prefix, and interal fields shoudl start with a lower case letter.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900