C# console input/output streams Difficulties

If the character is A, add the two integers. If it is S, subtract the

Page 1 of 1

4 Replies - 6053 Views - Last Post: 16 June 2008 - 12:05 PM Rate Topic: -----

#1 kobuew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-June 08

C# console input/output streams Difficulties

Posted 14 June 2008 - 04:15 PM

/*If the character is A, add the two integers. If it is S, subtract the second integer from the first. If it is M, multiply the integers*/

static int Main()
		{
			char A, S, M;
			char option;
			string integer1;
			string integer2;
			
	 Console.WriteLine("Enter an integer");
	 string integer1 = int.Parse(Console.ReadLine());
	 Console.WriteLine("Enter another integer");
	 string integer2 = int.Parse(Console.ReadLine());
	 Console.WriteLine ("Enter an option A, S or M");
	 string option = Console.ReadLine();

	 if (option == A)
			{
	   Console.WriteLine("The answer is");
	   return (integer1 + integer2);
	 }
	   if (option == S)
			{
	   Console.WriteLine("The answer is");
	   return (integer1 - integer2);
	  }
	   if(option == M)
		  {
		   Console.WriteLine("The answer is:");
		   return (integer1 * integer2);


Is This A Good Question/Topic? 0
  • +

Replies To: C# console input/output streams Difficulties

#2 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: C# console input/output streams Difficulties

Posted 14 June 2008 - 04:29 PM

Well, I think I see your problem.
First of all, delete the line that says 'Char A, S, M;'
Then replace this code:
if (option == A)
{
Console.WriteLine("The answer is");
return (integer1 + integer2);
}
if (option == S)
{
Console.WriteLine("The answer is");
return (integer1 - integer2);
}
if(option == M)
{
Console.WriteLine("The answer is:");
return (integer1 * integer2); 


With this code...
if (option == 'A')
{
Console.WriteLine("The answer is " + (integer1 + integer2).ToString());
}
if (option == 'S')
{
Console.WriteLine("The answer is " + (integer1 - integer2).ToString());
}
if(option == 'M')
{
Console.WriteLine("The answer is " + (integer1 * integer2).ToString());



I'm not very good at explaining things, but trust me, put this code in the place of what you have and it should work just fine.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: C# console input/output streams Difficulties

Posted 14 June 2008 - 04:37 PM

Next time please more clearly state what your problem is and use code tags to show off your code (it also makes it easier for us as helpers to copy and test your code)... thanks!

Now you had a lot of little syntax errors. Remember if you have a variable named "integer1" you probably should actually define it as an int. Also your char variables A, S, and M are not needed, you were looking to compare their input against characters, not variables.

        static void Main(string[] args)
        {
            char option;
            // Integers are integers, don't define as string
            int integer1;
            int integer2;

            // Collect input, parse it to an integer
            Console.WriteLine("Enter an integer");
            integer1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter another integer");
            integer2 = int.Parse(Console.ReadLine());

            // Parse our option to a char
            Console.WriteLine ("Enter an option A, S or M");
            option = Char.Parse(Console.ReadLine());

            // We compare what they typed to A, S, M chars, not variables.
            // You didn't even set the variables.
            if (option == 'A')
            {
                // We don't return values here to print them,
                // we just use them in the writeline.
                Console.WriteLine("The answer is " + (integer1 + integer2));
            }
            if (option == 'S')
            {
                Console.WriteLine("The answer is " + (integer1 - integer2));

            }
            if (option == 'M') {
                Console.WriteLine("The answer is: " + (integer1 * integer2));
   
            }
        } 



Now the example above does not take into account error checking, so I suggest you do that. But read my in code comments to see why I made the changes I did. You can run this and give it a try to see how it works.

Overall we take their input for two numbers, parse them into "int" variables, take their char and parse it into a "char" variable and compare that against three chars.. 'A', 'S', and 'M'. Notice that these are uppercase, so you might want to plan for when they enter lower case. (hint: Adjust the case of their input).

This should get you up and moving in the right direction. Enjoy!

"At DIC we be C# fixing code ninjas... we also fix VB, PHP, casino games and that annoying guy who won't pay you the cash he owes you... but that is for a price." :snap:
Was This Post Helpful? 0
  • +
  • -

#4 winstonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-June 08

Re: C# console input/output streams Difficulties

Posted 16 June 2008 - 10:16 AM

Thank You very much, the program is running. If you can please advice as what I could by doing wrong on this program.
// I am trying to create a method that creates 5 int variables and prints them to the sreen. 
// Pass the variables by value to the method passByValue(), add 5 to each variable then print to sreen
// Lastly Pass variables by reference to the method called PassByRef(), add 10 to each variable then print to sreen
  
public static class EntensionClass
	{
		public static int Addition( int a, int B)
		{
			return a + b;
		}
	}

	class PassByValue
	{
		static void Add5()
		{
			var numbers = new int[5] { 18, 20, 30, 25, 40 };

			foreach (var number in numbers){
			Console.WriteLine("Passed by value: ", number.Addition(5));
			}
		}
		class PassByReference
		{
			private static void add10(ref int c)
			{
				c = c + 10;
			}
			public static void main()
			{
				var x = new int[] { 55, 76, 89, 99, 100 };
				add10(ref x);
				Console.WriteLine("Passed by reference: ", x);
			}
		}

Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 290
  • View blog
  • Posts: 1,524
  • Joined: 07-April 08

Re: C# console input/output streams Difficulties

Posted 16 June 2008 - 12:05 PM

View Postwinstonk, on 16 Jun, 2008 - 10:16 AM, said:

Thank You very much, the program is running. If you can please advice as what I could by doing wrong on this program.
// I am trying to create a method that creates 5 int variables and prints them to the sreen. 
// Pass the variables by value to the method passByValue(), add 5 to each variable then print to sreen
// Lastly Pass variables by reference to the method called PassByRef(), add 10 to each variable then print to sreen
  
public static class EntensionClass
	{
		public static int Addition( int a, int B)
		{
			return a + b;
		}
	}

	class PassByValue
	{
		static void Add5()
		{
			var numbers = new int[5] { 18, 20, 30, 25, 40 };

			foreach (var number in numbers){
			Console.WriteLine("Passed by value: ", number.Addition(5));
			}
		}
		class PassByReference
		{
			private static void add10(ref int c)
			{
				c = c + 10;
			}
			public static void main()
			{
				var x = new int[] { 55, 76, 89, 99, 100 };
				add10(ref x);
				Console.WriteLine("Passed by reference: ", x);
			}
		}



let me help you.............


use the code tags.

[code*]YOUR CODE GOES HERE [/code*] Type that exactly as i have without the *'s


once you edit your post and use them (as you were instructed to do earlier in this post, i bet you'll get more help on this topic :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1