Reputation: 15
I have 3 classes and one struct and I am trying to print out the total using accessors. The program prints the message the total is . No result is printed. trying to figure this out?
Main:
class MainClass
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add("1. option one");
list.Add("2. option two");
list.Add("3. option three");
list.Add("4. option four");
list.Add("5. option five");
Console.WriteLine("1. option one");
Console.WriteLine("2. option two");
Console.WriteLine("3. option three");
Console.WriteLine("4. option four");
Console.WriteLine("5. option five");
Console.WriteLine(" Pick 3 options ");
UserInput usi = new UserInput();
usi.Get();
Console.WriteLine(" You Picked ");
usi.print();
Console.ReadLine();
}
}
Program class:
class Program
{
private int A, B;
public void GetAB()
{
Console.WriteLine("Enter A: ");
A = int.Parse(Console.ReadLine());
if ((A <= 0) || (A >= 6))
// Console.WriteLine("good");
throw new ArgumentOutOfRangeException("Number must be between 1-5");
Console.WriteLine("Enter B: ");
B = int.Parse(Console.ReadLine());
if ((B <= 0) ||(B >=6))
throw new ArgumentOutOfRangeException("Number must be between 1-5");
}
public void PrintAB()
{
Console.WriteLine("A = {0}\tb = {1}\t", A, B);
}
}
user input:
class UserInput : Program
{
int C;
public void Get()
{
try
{
Console.WriteLine("Valid entries are 1-5");
GetAB();
Console.Write("Enter C: ");
C = int.Parse((Console.ReadLine()));
if ((C <= 0) || (C >= 6))
throw new ArgumentOutOfRangeException("Number must be between 1-5");
}
catch (ArgumentOutOfRangeException ex)
{
// Show the user that 7 cannot be divided by 2.
Console.WriteLine("Error: {0}", ex.Message);
Console.WriteLine("Please try again ");
System.Environment.Exit(1);
}
}
public int A
{ get; set; }
public int B
{ get; set; }
public void total()
{
int total = (A + B + C);
Console.WriteLine(" Total is ", total);
}
public void print()
{
PrintAB();
Console.WriteLine("C = {0}", C);
total();
}
}
total:
struct Total
{
public int total { get; set; }
}
Upvotes: 0
Views: 53
Reputation: 987
You are doing like:
Console.WriteLine("Total is : ", total);
// In userInput.total()
It should be changed with:
Console.WriteLine("Total is : " + total);
OR
Console.WriteLine("Total is : {0}", total);
Upvotes: 1