views:

79

answers:

1

This program is in response to the assignment:

"Create a method named Sum()that accepts any number of integer parameters and displays their sum. Write a Main()method that demonstrates the Sum()method works correctly when passed one, three, five, or an array of 10 integers. Save the program as UsingSum.cs."

from Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell

My code in the "//step 1:" part is getting the CS0019 error, which states that it cannot be applied to operands of type 'bool' and 'int'

I highly suspect there are also other problems with this code, but it's a great improvement over what I had four hours ago...

using System;

public class UsingSum
{
    public static void Main()
    {

        Sum();

    }

    public static void Sum() 

   { 

// Step 1: Addition of One, Three, Five

  int n;
  int o;
  int p;
  int firstTotal;  
  string k, l, m;
  bool q;
  bool r;
  bool s;

  Console.Write("Type the number 1: ");    
  k = Console.ReadLine();
  q = Convert.ToBoolean(k);
  n = Convert.ToInt32(k);

  Console.WriteLine();

  if(q == 1)
        {
           Console.WriteLine("Input accepted.");
        }

     else
        if(!(q == 1))
     {
     Console.WriteLine("Error: You didn't type the number 1. Please try again.");
     Console.Write("Type the number 1: ");    
     k = Console.ReadLine();
     q = Convert.ToBoolean(k);
     n = Convert.ToInt32(k);

     }

  Console.Write("Type the number 3: ");    
  l = Console.ReadLine();
  r = Convert.ToBoolean(l);
  o = Convert.ToInt32(l);

  Console.WriteLine();

  if(r <= 2 || r >= 4)
     {
     Console.WriteLine("Error: You didn't type the number 3. Please try again.");
     Console.Write("Type the number 3: ");    
     l = Console.ReadLine();
     r = Convert.ToBoolean(l);
     o = Convert.ToInt32(l);

     }

     else
        if(r = 3)
        {
           Console.WriteLine("Input accepted.");
        }

  Console.Write("Type the number 5: ");    
  m = Console.ReadLine();
  s = Convert.ToBoolean(m);
  p = Convert.ToInt32(m);

  Console.WriteLine();

  if(s <= 4 || s >= 6)
     {
     Console.WriteLine("Error: You didn't type the number 5. Please try again.");
     Console.Write("Type the number 5: ");    
     m = Console.ReadLine();
     s = Convert.ToBoolean(m);
     p = Convert.ToInt32(m);

     }

     else
        if(s = 5)
        {
           Console.WriteLine("Input accepted.");
        }

  firstTotal = n + o + p;

  Console.WriteLine("{0} + {1} + {2} = {3}", n, o, p, firstTotal); 

// Step 2: Entering integers for array[10]

  int a;
  int b;
  int c;
  int d;
  int e;
  int f;
  int g;
  int h;
  int i;
  int j;
  int unlimited;
  int arrayTotal;

  Console.Write("Enter first integer for addition: ");
  a = Convert.ToInt32(Console.ReadLine());
  Console.Write("Enter second integer for addition: ");
  b = Convert.ToInt32(Console.ReadLine());
  Console.Write("Enter third integer for addition: ");
  c = Convert.ToInt32(Console.ReadLine());
  Console.Write("Enter forth integer for addition: ");
  d = Convert.ToInt32(Console.ReadLine());
  Console.Write("Enter fifth integer for addition: ");
  e = Convert.ToInt32(Console.ReadLine());
  Console.Write("Enter sixth integer for addition: ");
  f = Convert.ToInt32(Console.ReadLine());
  Console.Write("Enter seventh integer for addition: ");
  g = Convert.ToInt32(Console.ReadLine());
  Console.Write("Enter eighth integer for addition: ");
  h = Convert.ToInt32(Console.ReadLine());
  Console.Write("Enter ninth integer for addition: ");
  i = Convert.ToInt32(Console.ReadLine());
  Console.Write("Enter tenth integer for addition: ");
  j = Convert.ToInt32(Console.ReadLine());

  arrayTotal = a + b + c + d + e + f + g + h + i +j; 

  Console.WriteLine("The total of {0} + {1} + {2} + {3} + {4} + {5} + {6} + {7} + {8} + {9} = {10}",
  a, b, c, d, e, f, g, h, i, j, arrayTotal);

//Step 3: Unlimited array addition

  int[] arrayTwo;
  string x;
  string ADD;
  int y;
  int total;


  while(Console.Write("Enter an integer for addition, or type ADD to calculate the sum: "))
  {
     x = Console.ReadLine();
     y = Convert.ToInt32(x);

     if(x = ADD)
     {
        Console.WriteLine("Calculating the total sum");
     }
  }   


  for(y = 0; y < arrayTwo.Length; ++y)
  {
  total = arrayTwo[y] + arrayTwo[y];              
  ++arrayTwo[y];
  Console.WriteLine("========================");
  Console.WriteLine("=/n= The total is: {0} =/n=", total);
  Console.WriteLine("========================");
  }                                         
 }  
}  
+1  A: 

You can't do (q == 1) in C#, because q is a bool and 1 is an int.

q can only be compared to another boolean expression, e.g. true, false, another boolean variable, etc.

Frédéric Hamidi
Well, I'm fortunate enough to have a teacher... It just takes him a week to email me back, and he's too far away to wack me with a ruler... or even throw it. Which means I can keep typing these miserable codes that don't work, Hans. Frederic, thank you for your answer. It looks like I need to go back and review the rules for writing my bools...
Nooob