14 Replies - 1940 Views - Last Post: 18 March 2013 - 04:04 AM Rate Topic: -----

#1 robertchu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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; 
		double radius; 
	int choice;  

	cout <<fixed<<showpoint<<setprecision(3); 
	do 
	{
		cout<<"\n\t\tGeometery Calculator Menu\n\n" 
			<< "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<<"Enter Radius:\n"; 
			     cin>>radius;
				 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: 
					CircleArea = (PI)*(radius)*(radius); 
						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  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1575
  • View blog
  • Posts: 2,997
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5066
  • View blog
  • Posts: 26,449
  • 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" **
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • 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); }



Then, in your menu code:
if(choice == CIRCLE_CHOICE) {
	cout<<"Enter Radius:\n"; 
	double radius;
	cin>>radius;
	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<<"Enter Radius:\n"; 
	double radius;
	cin>>radius;
	double CircleArea = (PI)*(radius)*(radius); 
	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.
Was This Post Helpful? 1
  • +
  • -

#5 robertchu  Icon User is offline

  • New D.I.C Head

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

Re: computing negative numbers

Posted 17 March 2013 - 06:17 AM

View Postbaavgai, 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); }



Then, in your menu code:
if(choice == CIRCLE_CHOICE) {
	cout<<"Enter Radius:\n"; 
	double radius;
	cin>>radius;
	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<<"Enter Radius:\n"; 
	double radius;
	cin>>radius;
	double CircleArea = (PI)*(radius)*(radius); 
	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?

View Postno2pencil, 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?
Was This Post Helpful? 0
  • +
  • -

#6 robertchu  Icon User is offline

  • New D.I.C Head

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

Re: computing negative numbers

Posted 17 March 2013 - 06:31 AM

View Postrobertchu, 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; 
		double radius; 
	int choice;  

	cout <<fixed<<showpoint<<setprecision(3); 
	do 
	{
		cout<<"\n\t\tGeometery Calculator Menu\n\n" 
			<< "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<<"Enter Radius:\n"; 
			     cin>>radius;
				 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: 
					CircleArea = (PI)*(radius)*(radius); 
						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.
Attached Image
this Debug error confuses me, it says my variable are not initialized so im not so sure what that means
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: computing negative numbers

Posted 17 March 2013 - 06:39 AM

View Postrobertchu, 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) {
	// your code
} else if(choice == CIRCLE_CHOICE)  {
	// your code
} else if (choice == RECTANGLE_CHOICE) {
	// your code
} else if (choice == TRIANGLE_CHOICE) {
	// your code
} else {
	// oops
}



Or:
switch(choice) {
	case QUIT_CHOICE: 
		// your code
		break; 
	case CIRCLE_CHOICE: 
		// your code
		break; 
	case RECTANGLE_CHOICE: 
		// your code
		break; 
	case TRIANGLE_CHOICE: 
		// your code
		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.


View Postrobertchu, 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? :P 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.

View Postrobertchu, 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.
Was This Post Helpful? 0
  • +
  • -

#9 robertchu  Icon User is offline

  • New D.I.C Head

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

Re: computing negative numbers

Posted 17 March 2013 - 06:49 AM

View Postjimblumberg, 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
Was This Post Helpful? 0
  • +
  • -

#10 robertchu  Icon User is offline

  • New D.I.C Head

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

Re: computing negative numbers

Posted 17 March 2013 - 06:54 AM

View Postbaavgai, on 17 March 2013 - 06:39 AM, said:

View Postrobertchu, 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) {
	// your code
} else if(choice == CIRCLE_CHOICE)  {
	// your code
} else if (choice == RECTANGLE_CHOICE) {
	// your code
} else if (choice == TRIANGLE_CHOICE) {
	// your code
} else {
	// oops
}



Or:
switch(choice) {
	case QUIT_CHOICE: 
		// your code
		break; 
	case CIRCLE_CHOICE: 
		// your code
		break; 
	case RECTANGLE_CHOICE: 
		// your code
		break; 
	case TRIANGLE_CHOICE: 
		// your code
		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.


View Postrobertchu, 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? :P/> 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.

View Postrobertchu, 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.
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • 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
Was This Post Helpful? 0
  • +
  • -

#13 robertchu  Icon User is offline

  • New D.I.C Head

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

Re: computing negative numbers

Posted 17 March 2013 - 05:13 PM

View Postbaavgai, 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; 
    double radius=0; 
int choice;  


do 
{
    cout<<"\n\t\tGeometery Calculator Menu\n\n" 
        << "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<<"Enter Radius:\n"; 
             cin>>radius;
			 
			 CircleArea = (PI)*(radius)*(radius); 
             while(radius<=0)
			 {cout<<"No negative numbers allowed\n";
			 cin>> radius; 
			 }
			 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

Was This Post Helpful? 0
  • +
  • -

#14 robertchu  Icon User is offline

  • New D.I.C Head

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

Re: computing negative numbers

Posted 17 March 2013 - 05:42 PM

View Postrobertchu, on 17 March 2013 - 05:13 PM, said:

View Postbaavgai, 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; 
    double radius=0; 
int choice;  


do 
{
    cout<<"\n\t\tGeometery Calculator Menu\n\n" 
        << "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<<"Enter Radius:\n"; 
             cin>>radius;
			 
			 CircleArea = (PI)*(radius)*(radius); 
             while(radius<=0)
			 {cout<<"No negative numbers allowed\n";
			 cin>> radius; 
			 }
			 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"; 
             cin>>radius;
			 
			 CircleArea = (PI)*(radius)*(radius); 
             while(radius<=0)
			 {cout<<"No negative numbers allowed\n";
			 cin>> radius; 
			 }
			 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?


Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • 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) {
	cout<<"Enter Radius:\n"; 
	int radius; // this variable isn't needed outside this block
	cin>>radius;

	// have you done your check yet?  NO.  Radius could be anything
	// CircleArea = (PI)*(radius)*(radius); 
	while(radius<=0) {
		cout<<"No negative numbers allowed\n";
		cin >> radius; 
	}
	// 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
	double area = (PI)*(radius)*(radius); 
	cout<<"The Area of the Circle is:"<<area<<endl; 
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1