# Looping problems

• (2 Pages)
• 1
• 2

## 15 Replies - 998 Views - Last Post: 24 April 2009 - 11:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=101424&amp;s=04d7172ada73b0c319028b069393bbbc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Wingzerorx93

Reputation: 0
• 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;

Console.WriteLine("Enter Number");
Console.WriteLine("Operation");
Console.WriteLine("Enter Number");
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");
Console.WriteLine("Operation");
Console.WriteLine("Enter Number");
}
}
}
}

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

• using Caffeine;

Reputation: 947
• Posts: 6,342
• Joined: 18-October 08

## Re: Looping problems

Posted 24 April 2009 - 08:40 AM

Wingzerorx93, 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.

### #3 papuccino1

• His name was Robert Paulson.

Reputation: 63
• 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.

### #4 SixOfEleven

• using Caffeine;

Reputation: 947
• 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.

### #5 Wingzerorx93

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

## Re: Looping problems

Posted 24 April 2009 - 08:52 AM

SixOfEleven, on 24 Apr, 2009 - 07:40 AM, said:

Wingzerorx93, 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.

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

### #6 papuccino1

• His name was Robert Paulson.

Reputation: 63
• 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.

### #7 Wingzerorx93

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

## Re: Looping problems

Posted 24 April 2009 - 08:59 AM

papuccino1, 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?

### #8 SixOfEleven

• using Caffeine;

Reputation: 947
• Posts: 6,342
• Joined: 18-October 08

## Re: Looping problems

Posted 24 April 2009 - 09:13 AM

Wingzerorx93, on 24 Apr, 2009 - 07:52 AM, said:

SixOfEleven, on 24 Apr, 2009 - 07:40 AM, said:

Wingzerorx93, 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.

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.

Console.WriteLine("Enter Number");

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

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

After all of your if statements do this:

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

### #9 papuccino1

• His name was Robert Paulson.

Reputation: 63
• 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.

### #10 Wingzerorx93

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

## Re: Looping problems

Posted 24 April 2009 - 09:35 AM

papuccino1, 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");
Console.WriteLine("Operation");
Console.WriteLine("Enter Number");

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);
}
Console.WriteLine("Operation");
Console.WriteLine("Enter Number");
}
}
}
}

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.

### #11 papuccino1

• His name was Robert Paulson.

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

## Re: Looping problems

Posted 24 April 2009 - 09:45 AM

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

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

### #12 SixOfEleven

• using Caffeine;

Reputation: 947
• Posts: 6,342
• Joined: 18-October 08

## Re: Looping problems

Posted 24 April 2009 - 09:47 AM

Wingzerorx93, on 24 Apr, 2009 - 08:35 AM, said:

papuccino1, 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");
Console.WriteLine("Operation");
Console.WriteLine("Enter Number");

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);
}
Console.WriteLine("Operation");
Console.WriteLine("Enter Number");
}
}
}
}

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 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

### #13 Wingzerorx93

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

## Re: Looping problems

Posted 24 April 2009 - 09:58 AM

SixOfEleven, on 24 Apr, 2009 - 08:47 AM, said:

Wingzerorx93, on 24 Apr, 2009 - 08:35 AM, said:

papuccino1, 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");
Console.WriteLine("Operation");
Console.WriteLine("Enter Number");

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);
}
Console.WriteLine("Operation");
Console.WriteLine("Enter Number");
}
}
}
}

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 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

### #14 SixOfEleven

• using Caffeine;

Reputation: 947
• 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 second;
string operation;
double temp = 0.0;

Console.WriteLine("Enter Number");
Console.WriteLine("Operation");
Console.WriteLine("Enter Number");

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);
}
Console.WriteLine("Operation");
Console.WriteLine("Enter Number");
}
}
}
}

### #15 Wingzerorx93

Reputation: 0
• 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.