Looping problems

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 852 Views - Last Post: 24 April 2009 - 11:01 AM Rate Topic: -----

#1 Wingzerorx93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 18-April 09

Looping problems

Posted 24 April 2009 - 08:30 AM

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

namespace Project1
{
	class Program
	{
		public static void Main()
		{
			double first;
			double second;
			string operation;
			double answer;
			
			Console.WriteLine("Enter Number");
			first = Convert.ToDouble(Console.ReadLine());
			Console.WriteLine("Operation");
			operation = Console.ReadLine();
			Console.WriteLine("Enter Number");
			second = Convert.ToDouble(Console.ReadLine());
			while (operation != "quit")
			{

				if (operation == "+")
				{
					answer = first + second; Console.WriteLine("{0} + {1} = {2}", first, second, answer);
				}
				else if (operation == "-")
				{
					answer = first - second; Console.WriteLine("{0} - {1} = {2}", first, second, answer);
				}
				else if (operation == "*")
				{
					answer = first * second; Console.WriteLine("{0} * {1} = {2}", first, second, answer);
				}
				else if (operation == "/")
				{
					answer = first / second; Console.WriteLine("{0} / {1} = {2}", first, second, answer);
				}
				Console.WriteLine("Enter Number");
				first = Convert.ToDouble(Console.ReadLine());
				Console.WriteLine("Operation");
				operation = Console.ReadLine();
				Console.WriteLine("Enter Number");
				second = Convert.ToDouble(Console.ReadLine());
			}
		}
	}
}



I am trying to use the answer from the calculation and use it in another calculation. Is there anyway to read the answer and compute a new answer?

Is This A Good Question/Topic? 0
  • +

Replies To: Looping problems

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Looping problems

Posted 24 April 2009 - 08:40 AM

View PostWingzerorx93, on 24 Apr, 2009 - 07:30 AM, said:

I am trying to use the answer from the calculation and use it in another calculation. Is there anyway to read the answer and compute a new answer?


After the first calculation, do you want answer to be first or second? Do you want to add the new result to the answer?

I don't understand what you are asking. :blink: :crazy:
Was This Post Helpful? 0
  • +
  • -

#3 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Looping problems

Posted 24 April 2009 - 08:45 AM

I think he wants to start a calculation, finish it, and then use that answer with another operator and a NEW number to get ANOTHER answer. Like a calculator works.
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Looping problems

Posted 24 April 2009 - 08:49 AM

That is what I though. Just wanted to be sure. :)
Was This Post Helpful? 0
  • +
  • -

#5 Wingzerorx93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 18-April 09

Re: Looping problems

Posted 24 April 2009 - 08:52 AM

View PostSixOfEleven, on 24 Apr, 2009 - 07:40 AM, said:

View PostWingzerorx93, on 24 Apr, 2009 - 07:30 AM, said:

I am trying to use the answer from the calculation and use it in another calculation. Is there anyway to read the answer and compute a new answer?


After the first calculation, do you want answer to be first or second? Do you want to add the new result to the answer?

I don't understand what you are asking. :blink: :crazy:


I want to use the answer that was in the first operation, and use it again in another operation, if that makes any sense.
example:
4+4=8
operation: +
number: 4
8+4=12
Was This Post Helpful? 0
  • +
  • -

#6 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Looping problems

Posted 24 April 2009 - 08:55 AM

Just create a third int value called Answer and use that in subsequent operations.
Was This Post Helpful? 0
  • +
  • -

#7 Wingzerorx93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 18-April 09

Re: Looping problems

Posted 24 April 2009 - 08:59 AM

View Postpapuccino1, on 24 Apr, 2009 - 07:55 AM, said:

Just create a third int value called Answer and use that in subsequent operations.

Will it conflict with the variable that I already declared, called answer?
Was This Post Helpful? 0
  • +
  • -

#8 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Looping problems

Posted 24 April 2009 - 09:13 AM

View PostWingzerorx93, on 24 Apr, 2009 - 07:52 AM, said:

View PostSixOfEleven, on 24 Apr, 2009 - 07:40 AM, said:

View PostWingzerorx93, on 24 Apr, 2009 - 07:30 AM, said:

I am trying to use the answer from the calculation and use it in another calculation. Is there anyway to read the answer and compute a new answer?


After the first calculation, do you want answer to be first or second? Do you want to add the new result to the answer?

I don't understand what you are asking. :blink: :crazy:


I want to use the answer that was in the first operation, and use it again in another operation, if that makes any sense.
example:
4+4=8
operation: +
number: 4
8+4=12


Okay, here is a solution:

Rename the variable first to temp.

Where you read in first, change that to answer.

Console.WriteLine("Enter Number");
answer = Convert.ToDouble(Console.ReadLine());



You need to change your calculations a little. I will show you the one for "+".

if (operation == "+")
{
    temp = answer + second; 
    Console.WriteLine("{0} + {1} = {2}", answer, second, temp);
}



After all of your if statements do this:

answer = temp;



Get rid of the code that reads in first after the if statements.

If you have problems, leave a reply and I'll post my solution.
Was This Post Helpful? 1
  • +
  • -

#9 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Looping problems

Posted 24 April 2009 - 09:14 AM

You will always want to overwrite the answer to keep on calculating. :)
Was This Post Helpful? 1
  • +
  • -

#10 Wingzerorx93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 18-April 09

Re: Looping problems

Posted 24 April 2009 - 09:35 AM

View Postpapuccino1, on 24 Apr, 2009 - 08:14 AM, said:

You will always want to overwrite the answer to keep on calculating. :)


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

namespace Project1
{
	class Program
	{
		public static void Main()
		{
			double first;
			double second;
			string operation;
			double temp;

			Console.WriteLine("Enter Number");
			answer = Convert.ToDouble(Console.ReadLine());
			Console.WriteLine("Operation");
			operation = Console.ReadLine();
			Console.WriteLine("Enter Number");
			second = Convert.ToDouble(Console.ReadLine());

			while (operation != "quit")
			{

				if (operation == "+")
				{
					temp = first + second; Console.WriteLine("{0} + {1} = {2}", first, second, temp);
				}
				else if (operation == "-")
				{
					temp = first - second; Console.WriteLine("{0} - {1} = {2}", first, second, temp);
				}
				else if (operation == "*")
				{
					temp = first * second; Console.WriteLine("{0} * {1} = {2}", first, second, temp);
				}
				else if (operation == "/")
				{
					temp = first / second; Console.WriteLine("{0} / {1} = {2}", first, second, temp);
				}
				answer = temp;
				Console.WriteLine("Operation");
				operation = Console.ReadLine();
				Console.WriteLine("Enter Number");
				second = Convert.ToDouble(Console.ReadLine());
			}
		}
	}
}



I did what you told me to do and now I get an error that says the name 'answer' does not exist in current context.
Was This Post Helpful? 0
  • +
  • -

#11 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Looping problems

Posted 24 April 2009 - 09:45 AM

Add this int variable:

double first;
            double second;
            string operation;
            double temp;
            double answer; //<--------------ADD THIS HERE.


This post has been edited by papuccino1: 24 April 2009 - 09:45 AM

Was This Post Helpful? 1
  • +
  • -

#12 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Looping problems

Posted 24 April 2009 - 09:47 AM

View PostWingzerorx93, on 24 Apr, 2009 - 08:35 AM, said:

View Postpapuccino1, on 24 Apr, 2009 - 08:14 AM, said:

You will always want to overwrite the answer to keep on calculating. :)


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

namespace Project1
{
	class Program
	{
		public static void Main()
		{
			double first;
			double second;
			string operation;
			double temp;

			Console.WriteLine("Enter Number");
			answer = Convert.ToDouble(Console.ReadLine());
			Console.WriteLine("Operation");
			operation = Console.ReadLine();
			Console.WriteLine("Enter Number");
			second = Convert.ToDouble(Console.ReadLine());

			while (operation != "quit")
			{

				if (operation == "+")
				{
					temp = first + second; Console.WriteLine("{0} + {1} = {2}", first, second, temp);
				}
				else if (operation == "-")
				{
					temp = first - second; Console.WriteLine("{0} - {1} = {2}", first, second, temp);
				}
				else if (operation == "*")
				{
					temp = first * second; Console.WriteLine("{0} * {1} = {2}", first, second, temp);
				}
				else if (operation == "/")
				{
					temp = first / second; Console.WriteLine("{0} / {1} = {2}", first, second, temp);
				}
				answer = temp;
				Console.WriteLine("Operation");
				operation = Console.ReadLine();
				Console.WriteLine("Enter Number");
				second = Convert.ToDouble(Console.ReadLine());
			}
		}
	}
}



I did what you told me to do and now I get an error that says the name 'answer' does not exist in current context.


Change this:

double first;
double second;
string operation;
double temp;



to:

double answer = 0.0;
double second;
string operation;
double temp;



and wherever you have first in your program change it to answer.

Example:

Change:

if (operation == "+")
{
	temp = first + second; Console.WriteLine("{0} + {1} = {2}", first, second, temp);
}




to:

if (operation == "+")
{
	temp = answer + second; Console.WriteLine("{0} + {1} = {2}", answer, second, temp);
}



That shold solve all of your problems. :)

This post has been edited by SixOfEleven: 24 April 2009 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#13 Wingzerorx93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 18-April 09

Re: Looping problems

Posted 24 April 2009 - 09:58 AM

View PostSixOfEleven, on 24 Apr, 2009 - 08:47 AM, said:

View PostWingzerorx93, on 24 Apr, 2009 - 08:35 AM, said:

View Postpapuccino1, on 24 Apr, 2009 - 08:14 AM, said:

You will always want to overwrite the answer to keep on calculating. :)


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

namespace Project1
{
	class Program
	{
		public static void Main()
		{
			double first;
			double second;
			string operation;
			double temp;

			Console.WriteLine("Enter Number");
			answer = Convert.ToDouble(Console.ReadLine());
			Console.WriteLine("Operation");
			operation = Console.ReadLine();
			Console.WriteLine("Enter Number");
			second = Convert.ToDouble(Console.ReadLine());

			while (operation != "quit")
			{

				if (operation == "+")
				{
					temp = first + second; Console.WriteLine("{0} + {1} = {2}", first, second, temp);
				}
				else if (operation == "-")
				{
					temp = first - second; Console.WriteLine("{0} - {1} = {2}", first, second, temp);
				}
				else if (operation == "*")
				{
					temp = first * second; Console.WriteLine("{0} * {1} = {2}", first, second, temp);
				}
				else if (operation == "/")
				{
					temp = first / second; Console.WriteLine("{0} / {1} = {2}", first, second, temp);
				}
				answer = temp;
				Console.WriteLine("Operation");
				operation = Console.ReadLine();
				Console.WriteLine("Enter Number");
				second = Convert.ToDouble(Console.ReadLine());
			}
		}
	}
}



I did what you told me to do and now I get an error that says the name 'answer' does not exist in current context.


Change this:

double first;
double second;
string operation;
double temp;



to:

double answer = 0.0;
double second;
string operation;
double temp;



and wherever you have first in your program change it to answer.

Example:

Change:

if (operation == "+")
{
	temp = first + second; Console.WriteLine("{0} + {1} = {2}", first, second, temp);
}




to:

if (operation == "+")
{
	temp = answer + second; Console.WriteLine("{0} + {1} = {2}", answer, second, temp);
}



That shold solve all of your problems. :)


Now it is doing something strange:
Enter Number
6
Operation
+
Enter Number
6
6+6 =12
operation
+
Enter Number
3
6+3=9
Was This Post Helpful? 0
  • +
  • -

#14 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Looping problems

Posted 24 April 2009 - 10:07 AM

You've done a lot of work. I'll post the full solution.

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

namespace Project1
{
    class Program
    {
        public static void Main()
        {
            double answer;
            double second;
            string operation;
            double temp = 0.0;

            Console.WriteLine("Enter Number");
            answer = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Operation");
            operation = Console.ReadLine();
            Console.WriteLine("Enter Number");
            second = Convert.ToDouble(Console.ReadLine());

            while (operation != "quit")
            {

                if (operation == "+")
                {
                    temp = answer + second; Console.WriteLine("{0} + {1} = {2}", answer, second, temp);
                }
                else if (operation == "-")
                {
                    temp = answer - second; Console.WriteLine("{0} - {1} = {2}", answer, second, temp);
                }
                else if (operation == "*")
                {
                    temp = answer * second; Console.WriteLine("{0} * {1} = {2}", answer, second, temp);
                }
                else if (operation == "/")
                {
                    temp = answer / second; Console.WriteLine("{0} / {1} = {2}", answer, second, temp);
                }
                answer = temp;
                Console.WriteLine("Operation");
                operation = Console.ReadLine();
                Console.WriteLine("Enter Number");
                second = Convert.ToDouble(Console.ReadLine());
            }
        }
    }
}

Was This Post Helpful? 1
  • +
  • -

#15 Wingzerorx93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 18-April 09

Re: Looping problems

Posted 24 April 2009 - 10:29 AM

Both of you were right, I accidentally set answer to equal zero. Thank you for the help. :D :^:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2