# computing negative numbers

Page 1 of 1

## 14 Replies - 2729 Views - Last Post: 18 March 2013 - 04:04 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=315672&amp;s=841ed98ac6cedc5b488ff58cf832ec45&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 robertchu

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 16-March 13

# computing negative numbers

Posted 17 March 2013 - 12:34 AM

```#include "stdafx.h"
#include<iostream>
#include<iomanip >
using namespace std;

int main()
{
const int CIRCLE_CHOICE =1,
RECTANGLE_CHOICE =2,
TRIANGLE_CHOICE =3,
QUIT_CHOICE =4;

double PI =3.14159;
double RectangleArea;
double TriangleArea;
double CircleArea;
double length;
double width;
double Height;
double Base;
int choice;

cout <<fixed<<showpoint<<setprecision(3);
do
{
<< "1.Area of Circle\n"
<<"2.Area of Rectangle\n"
<<"3.Area of Triangle\n"
<<"4.Quit Program!!\n\n"
<<"Enter Your Choice: ";
cin>>choice;
while(choice<CIRCLE_CHOICE || choice>QUIT_CHOICE)
{
cout<<"Please Try again with the number 1-4:" ;
cin>>choice;
}
if(choice == QUIT_CHOICE)
{  cout<<"Program ENDING\n" ;

}

if(choice == CIRCLE_CHOICE)
{

cout<<"The Area of the Circle is:"<<CircleArea<<endl;
}
if (choice == RECTANGLE_CHOICE)
{

cout<<"Enter length:\n";
cin>>length;

cout<<"Enter width:\n";
cin>>width;
cout<<"The Area of the Rectangle is:"<<RectangleArea<<endl;
}
if (choice == TRIANGLE_CHOICE)
{
cout<<"Enter Height:\n";
cin>>Height;

cout<<"Enter Base:\n";
cin>>Base;
cout<<"The Area of the Triangle is:" <<TriangleArea<<endl;
}
switch(choice)
{
case CIRCLE_CHOICE:
break;

case RECTANGLE_CHOICE:
RectangleArea = (length) *(width);
break;
case TRIANGLE_CHOICE:
TriangleArea = (Base)*(Height)*(.5);
break;

}

}while(choice != QUIT_CHOICE);
return 0;
}

```

Hello
My program is computing negative numbers when I put values in for my numbers it computes negative areas. I've tried and search for a results but couldn't find any because the results were above my knowledge of ctt i am still new to programming Its saying that My variables are not initialized. I actually tried the while loop but i found the do while loop better for validation. I've tried using if statements and found that the program runs more smoothly then my while loop i was trying to do.This is the edited version with if statements.
Actually made two different versions but i chose this to be the more organized manner. I'm not sure if my method is wrong, i'm trying to use the do while loop, but it appears that my numbers are negative. Any tips or ideas of how i can solve my issue.

Is This A Good Question/Topic? 0

## Replies To: computing negative numbers

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 2140
• Posts: 4,205
• Joined: 30-May 10

## Re: computing negative numbers

Posted 17 March 2013 - 01:11 AM

You need to do this
80 RectangleArea = (length) *(width);

Before you do this
62 cout<<"The Area of the Rectangle is:"<<RectangleArea<<endl;

C++ doesn't look ahead to see what relevant lines of code might be applicable at the point you try and print something.

### #3 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

## Re: computing negative numbers

Posted 17 March 2013 - 02:05 AM

** Renamed title to be more descriptive. Please don't name titles "Need help" **

### #4 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,890
• Joined: 16-October 07

## Re: computing negative numbers

Posted 17 March 2013 - 03:55 AM

Functions, functions, functions. What if, instead of having all those bloody variables, you defined someting like:
```double getCircleArea(double radius) { return (PI)*(radius)*(radius); }

```

```if(choice == CIRCLE_CHOICE) {
cout<<"The Area of the Circle is:"<<getCircleArea(radius)<<endl;
} else if (choice == RECTANGLE_CHOICE) {

```

Clearer?

In any case, you have that wonky little switch at the end, there. Choose an if or a switch. Don't separate them out, it's confusing. It confused you.

The only real reason for those const int menu options is to make the switch read better. I'm guess that's part of the code you were given? Use the switch, then.
```case CIRCLE_CHOICE:
cout<<"The Area of the Circle is:"<<CircleArea<<endl;
break;

```

If you prefer the if, also use the else if to avoid extra checks.

Hope this helps.

### #5 robertchu

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 16-March 13

## Re: computing negative numbers

Posted 17 March 2013 - 06:17 AM

baavgai, on 17 March 2013 - 03:55 AM, said:

Functions, functions, functions. What if, instead of having all those bloody variables, you defined someting like:
```double getCircleArea(double radius) { return (PI)*(radius)*(radius); }

```

```if(choice == CIRCLE_CHOICE) {
cout<<"The Area of the Circle is:"<<getCircleArea(radius)<<endl;
} else if (choice == RECTANGLE_CHOICE) {

```

Clearer?

In any case, you have that wonky little switch at the end, there. Choose an if or a switch. Don't separate them out, it's confusing. It confused you.

The only real reason for those const int menu options is to make the switch read better. I'm guess that's part of the code you were given? Use the switch, then.
```case CIRCLE_CHOICE:
cout<<"The Area of the Circle is:"<<CircleArea<<endl;
break;

```

If you prefer the if, also use the else if to avoid extra checks.

Hope this helps.

So i cant have both methods to compute? Bare with me i have very little experience with ctt. And im not sure what you mean about avoiding extra checks. I do like your get method. But do i have to create another class so the problem can get the radius? And how do i not accept negative values would i have to have a default?

no2pencil, on 17 March 2013 - 02:05 AM, said:

** Renamed title to be more descriptive. Please don't name titles "Need help" **

Sorry how do i rename the title?

### #6 robertchu

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 16-March 13

## Re: computing negative numbers

Posted 17 March 2013 - 06:31 AM

robertchu, on 17 March 2013 - 12:34 AM, said:

```#include "stdafx.h"
#include<iostream>
#include<iomanip >
using namespace std;

int main()
{
const int CIRCLE_CHOICE =1,
RECTANGLE_CHOICE =2,
TRIANGLE_CHOICE =3,
QUIT_CHOICE =4;

double PI =3.14159;
double RectangleArea;
double TriangleArea;
double CircleArea;
double length;
double width;
double Height;
double Base;
int choice;

cout <<fixed<<showpoint<<setprecision(3);
do
{
<< "1.Area of Circle\n"
<<"2.Area of Rectangle\n"
<<"3.Area of Triangle\n"
<<"4.Quit Program!!\n\n"
<<"Enter Your Choice: ";
cin>>choice;
while(choice<CIRCLE_CHOICE || choice>QUIT_CHOICE)
{
cout<<"Please Try again with the number 1-4:" ;
cin>>choice;
}
if(choice == QUIT_CHOICE)
{  cout<<"Program ENDING\n" ;

}

if(choice == CIRCLE_CHOICE)
{

cout<<"The Area of the Circle is:"<<CircleArea<<endl;
}
if (choice == RECTANGLE_CHOICE)
{

cout<<"Enter length:\n";
cin>>length;

cout<<"Enter width:\n";
cin>>width;
cout<<"The Area of the Rectangle is:"<<RectangleArea<<endl;
}
if (choice == TRIANGLE_CHOICE)
{
cout<<"Enter Height:\n";
cin>>Height;

cout<<"Enter Base:\n";
cin>>Base;
cout<<"The Area of the Triangle is:" <<TriangleArea<<endl;
}
switch(choice)
{
case CIRCLE_CHOICE:
break;

case RECTANGLE_CHOICE:
RectangleArea = (length) *(width);
break;
case TRIANGLE_CHOICE:
TriangleArea = (Base)*(Height)*(.5);
break;

}

}while(choice != QUIT_CHOICE);
return 0;
}

```

Hello
My program is computing negative numbers when I put values in for my numbers it computes negative areas. I've tried and search for a results but couldn't find any because the results were above my knowledge of ctt i am still new to programming Its saying that My variables are not initialized. I actually tried the while loop but i found the do while loop better for validation. I've tried using if statements and found that the program runs more smoothly then my while loop i was trying to do.This is the edited version with if statements.
Actually made two different versions but i chose this to be the more organized manner. I'm not sure if my method is wrong, i'm trying to use the do while loop, but it appears that my numbers are negative. Any tips or ideas of how i can solve my issue.

this Debug error confuses me, it says my variable are not initialized so im not so sure what that means

### #7 jimblumberg

Reputation: 5441
• Posts: 16,966
• Joined: 25-December 09

## Re: computing negative numbers

Posted 17 March 2013 - 06:35 AM

Quote

this Debug error confuses me, it says my variable are not initialized so im not so sure what that means

It means that you didn't initialize a variable before you used it. Do you know the difference between declaring a variable and initializing a variable?

Jim

### #8 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,890
• Joined: 16-October 07

## Re: computing negative numbers

Posted 17 March 2013 - 06:39 AM

robertchu, on 17 March 2013 - 08:17 AM, said:

So i cant have both methods to compute?

What I'm saying is:
```if(choice == QUIT_CHOICE) {
} else if(choice == CIRCLE_CHOICE)  {
} else if (choice == RECTANGLE_CHOICE) {
} else if (choice == TRIANGLE_CHOICE) {
} else {
// oops
}

```

Or:
```switch(choice) {
case QUIT_CHOICE:
break;
case CIRCLE_CHOICE:
break;
case RECTANGLE_CHOICE:
break;
case TRIANGLE_CHOICE:
break;
default:
// oops
}

```

Pick one. Note, I personally hate switch. It's a syntax is quirky and it's usage is far more limited compared to if then else. It's cute for certain cases, but usually not cute enough to bother with. Go with the former example.

robertchu, on 17 March 2013 - 08:17 AM, said:

But do i have to create another class so the problem can get the radius?

The word class means something very specific in C++. You don't need one of those.

Depending on how big your main is getting, I would use functions to make it smaller. e.g.
```int main() {
bool done = false;
while(!done) {
switch(getChoice()) {
case QUIT_CHOICE: done = true; break;
case CIRCLE_CHOICE: doCircle(); break;
case RECTANGLE_CHOICE: doRect(); break;
case TRIANGLE_CHOICE: doTri(); break;
default:
cout << "Invalid Choice" << endl;
}
}
return 0;
}

```

See how I used a switch when I told you not to? That's one of the ways it looks neat. Mostly because I don't need to store the result of getChoice() in a declared variable.

robertchu, on 17 March 2013 - 08:17 AM, said:

And how do i not accept negative values would i have to have a default?

I'd put that in a function too. Simply:
```double getDoubleFromUser() {
double n;
cin >> n;
while(n<0) {
cout << "Sorry, we don't like negative values.  Please enter again." << endl;
cin >> n;
}
return n;
}

```

Note, cin streams can be a little quirky. You might have to clear it out after a read if there's still crap in it.

### #9 robertchu

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 16-March 13

## Re: computing negative numbers

Posted 17 March 2013 - 06:49 AM

jimblumberg, on 17 March 2013 - 06:35 AM, said:

Quote

this Debug error confuses me, it says my variable are not initialized so im not so sure what that means

It means that you didn't initialize a variable before you used it. Do you know the difference between declaring a variable and initializing a variable?

Jim

I know declaring variables is giving it values and initialize i have no clue what that means

### #10 robertchu

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 16-March 13

## Re: computing negative numbers

Posted 17 March 2013 - 06:54 AM

baavgai, on 17 March 2013 - 06:39 AM, said:

robertchu, on 17 March 2013 - 08:17 AM, said:

So i cant have both methods to compute?

What I'm saying is:
```if(choice == QUIT_CHOICE) {
} else if(choice == CIRCLE_CHOICE)  {
} else if (choice == RECTANGLE_CHOICE) {
} else if (choice == TRIANGLE_CHOICE) {
} else {
// oops
}

```

Or:
```switch(choice) {
case QUIT_CHOICE:
break;
case CIRCLE_CHOICE:
break;
case RECTANGLE_CHOICE:
break;
case TRIANGLE_CHOICE:
break;
default:
// oops
}

```

Pick one. Note, I personally hate switch. It's a syntax is quirky and it's usage is far more limited compared to if then else. It's cute for certain cases, but usually not cute enough to bother with. Go with the former example.

robertchu, on 17 March 2013 - 08:17 AM, said:

But do i have to create another class so the problem can get the radius?

The word class means something very specific in C++. You don't need one of those.

Depending on how big your main is getting, I would use functions to make it smaller. e.g.
```int main() {
bool done = false;
while(!done) {
switch(getChoice()) {
case QUIT_CHOICE: done = true; break;
case CIRCLE_CHOICE: doCircle(); break;
case RECTANGLE_CHOICE: doRect(); break;
case TRIANGLE_CHOICE: doTri(); break;
default:
cout << "Invalid Choice" << endl;
}
}
return 0;
}

```

See how I used a switch when I told you not to? /> That's one of the ways it looks neat. Mostly because I don't need to store the result of getChoice() in a declared variable.

robertchu, on 17 March 2013 - 08:17 AM, said:

And how do i not accept negative values would i have to have a default?

I'd put that in a function too. Simply:
```double getDoubleFromUser() {
double n;
cin >> n;
while(n<0) {
cout << "Sorry, we don't like negative values.  Please enter again." << endl;
cin >> n;
}
return n;
}

```

Note, cin streams can be a little quirky. You might have to clear it out after a read if there's still crap in it.

hmm so in my if statement your saying im missing the else statement? I thought i had to use the switch statement but thank you for telling me it is limited usage because i did hate using the method. But the switch method does look neater for some reason.

### #11 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,890
• Joined: 16-October 07

## Re: computing negative numbers

Posted 17 March 2013 - 07:20 AM

A switch is neat is your case statements are nice and orderly. If they get messy, then switch will get hard to follow. An if then, because it follows the standard block notation, tends to be easier to follow when things grow. However, if those blocks get larger than your screen, throw them in a function.

Writing code is as much about organization as anything else.

### #12 jimblumberg

Reputation: 5441
• Posts: 16,966
• Joined: 25-December 09

## Re: computing negative numbers

Posted 17 March 2013 - 04:41 PM

Quote

I know declaring variables is giving it values and initialize i have no clue what that means

No, when you declare a variable you don't necessarily assign a value to it. When you initialize a variable you assign the initial value. For example:

```int var1;      // Declare, but not initialized.
int var2 = 10; // Declare, and also initialize to 10.

var1 = 23;     // Initialize the variable to 23.

```

Before you can use a variable in a calculation it must have a value assigned to it.

Jim

### #13 robertchu

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 16-March 13

## Re: computing negative numbers

Posted 17 March 2013 - 05:13 PM

baavgai, on 17 March 2013 - 07:20 AM, said:

A switch is neat is your case statements are nice and orderly. If they get messy, then switch will get hard to follow. An if then, because it follows the standard block notation, tends to be easier to follow when things grow. However, if those blocks get larger than your screen, throw them in a function.

Writing code is as much about organization as anything else.

```#include "stdafx.h"
#include<iostream>
#include<iomanip >
using namespace std;
int main() { int CIRCLE_CHOICE =1, RECTANGLE_CHOICE =2, TRIANGLE_CHOICE =3, QUIT_CHOICE =4;
double PI =3.14159;
double RectangleArea;
double TriangleArea;
double CircleArea;
double length=0;
double width=0;
double Height=0;
double Base=0;
int choice;

do
{
<< "1.Area of Circle\n"
<<"2.Area of Rectangle\n"
<<"3.Area of Triangle\n"
<<"4.Quit Program!!\n\n"
<<"Enter Your Choice: ";
cin>>choice;
while(choice<CIRCLE_CHOICE || choice>QUIT_CHOICE)
{
cout<<"Please Try again with the number 1-4:" ;
cin>>choice;
}
if(choice == QUIT_CHOICE)
{  cout<<"Program ENDING\n" ;

}

else if(choice == CIRCLE_CHOICE)
{

{cout<<"No negative numbers allowed\n";
}
cout<<"The Area of the Circle is:"<<CircleArea<<endl;

}
else if (choice == RECTANGLE_CHOICE)
{

cout<<"Enter length:\n";
cin>>length;

cout<<"Enter width:\n";
cin>>width;
RectangleArea = (length) *(width);
cout<<"The Area of the Rectangle is:"<<RectangleArea<<endl;
}
else if (choice == TRIANGLE_CHOICE)
{
cout<<"Enter Height:\n";
cin>>Height;

cout<<"Enter Base:\n";
cin>>Base;
TriangleArea = (Base)*(Height)*(.5);
cout<<"The Area of the Triangle is:" <<TriangleArea<<endl;
}

cout <<fixed<<showpoint<<setprecision(3);

}while(choice != QUIT_CHOICE);return 0;
}

```

i took your advise and went with the ifs it worked alot better. But now im trying to deny negative numbers and its not letting me. I had to declare my variables to 0 such as the length width base and height so i wouldn't get a large number

This post has been edited by baavgai: 17 March 2013 - 05:37 PM
Reason for edit:: tag fix

### #14 robertchu

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 16-March 13

## Re: computing negative numbers

Posted 17 March 2013 - 05:42 PM

robertchu, on 17 March 2013 - 05:13 PM, said:

baavgai, on 17 March 2013 - 07:20 AM, said:

A switch is neat is your case statements are nice and orderly. If they get messy, then switch will get hard to follow. An if then, because it follows the standard block notation, tends to be easier to follow when things grow. However, if those blocks get larger than your screen, throw them in a function.

Writing code is as much about organization as anything else.

```#include "stdafx.h"
#include<iostream>
#include<iomanip >
using namespace std;
int main() { int CIRCLE_CHOICE =1, RECTANGLE_CHOICE =2, TRIANGLE_CHOICE =3, QUIT_CHOICE =4;
double PI =3.14159;
double RectangleArea;
double TriangleArea;
double CircleArea;
double length=0;
double width=0;
double Height=0;
double Base=0;
int choice;

do
{
<< "1.Area of Circle\n"
<<"2.Area of Rectangle\n"
<<"3.Area of Triangle\n"
<<"4.Quit Program!!\n\n"
<<"Enter Your Choice: ";
cin>>choice;
while(choice<CIRCLE_CHOICE || choice>QUIT_CHOICE)
{
cout<<"Please Try again with the number 1-4:" ;
cin>>choice;
}
if(choice == QUIT_CHOICE)
{  cout<<"Program ENDING\n" ;

}

else if(choice == CIRCLE_CHOICE)
{

{cout<<"No negative numbers allowed\n";
}
cout<<"The Area of the Circle is:"<<CircleArea<<endl;

}
else if (choice == RECTANGLE_CHOICE)
{

cout<<"Enter length:\n";
cin>>length;

cout<<"Enter width:\n";
cin>>width;
RectangleArea = (length) *(width);
cout<<"The Area of the Rectangle is:"<<RectangleArea<<endl;
}
else if (choice == TRIANGLE_CHOICE)
{
cout<<"Enter Height:\n";
cin>>Height;

cout<<"Enter Base:\n";
cin>>Base;
TriangleArea = (Base)*(Height)*(.5);
cout<<"The Area of the Triangle is:" <<TriangleArea<<endl;
}

cout <<fixed<<showpoint<<setprecision(3);

}while(choice != QUIT_CHOICE);return 0;
}

```

i took your advise and went with the ifs it worked alot better. But now im trying to deny negative numbers and its not letting me. I had to declare my variables to 0 such as the length width base and height so i wouldn't get a large number

```cout<<"Enter Radius:\n";

{cout<<"No negative numbers allowed\n";
}
cout<<"The Area of the Circle is:"<<CircleArea<<endl;

}
This is what i have right now and im not sure why it is not denying the negatives? should i try something different?

```

### #15 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,890
• Joined: 16-October 07

## Re: computing negative numbers

Posted 18 March 2013 - 04:04 AM

Because order is everything. Computers do things in sequence. Also, stop will all the variables floating about.

```} else if(choice == CIRCLE_CHOICE) {
int radius; // this variable isn't needed outside this block

// have you done your check yet?  NO.  Radius could be anything
cout<<"No negative numbers allowed\n";
}
// the above works, radius is now a valid value
// unfortunately, you've already assigned a value to CircleArea
// and you never recalculate it
// do your calculations after radius has the value you're keeping

// again, you don't need this variable outside this block