I am having a couple of problems. I think I got the formatting down for the most part. I am having issues in a few calculations and how to iterate through them. Basically I need to take the radius and calculate the circumference and the volume. Once I have the calculation I need to print them Radius in Decimal and Binary, then circumference in Hex and finally volume in scientific notation.

Questions.
1.) where would be best to declare circumference and volume?
2.) once they are declared whats the best place to do the calculations?
3.) I can't see an array for this assignment so how would I do this for radius values of 20 - 30.

Please see the attached code.

//***********************************************************************
// TITLE:                     Formatted Output
// FILENAME:                  CS215Lab2.cpp
// PREPARED FOR:              CS215, Section CSGA
// PROGRAMMER(S):             Kris Armstrong
// DEVELOPMENT DATE:          10/20/2011
// COMPILER USED:     	      Visual Studio 2010
// TARGET PLATFORM:           Dell Core i7
//=======================================================================
//                           PROJECT FILES
//    <LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT HERE>
//		CS215Lab2.cpp
//		sphere.h
//		sphere.cpp
//		spherefunction.h
//		spherefunction.cpp
//		
//=======================================================================
//   		REVISION HISTORY
//   List revisions made to the Program
//
//	DATE		PROGRAMMER				DESCRIPTION OF CHANGES MADE
//	10/28/11	Kris Armstrong			Original
//
//=======================================================================

//=======================================================================
//   			PROGRAM DESCRIPTION
//	Objective - To exercise some of the C++ I/O stream formatting options
//	and to compose, manipulate and format computed values of a class 
//	(Sphere). 
//
//	Specifications - Declare and define some global (free) functions that
//	will take a float/int number and output it to the screen using decimal,
//	hexadecimal, scientific, or binary notation depending on the
//	characteristic being output (see sample output below). The values
//	passed to these global functions will be values returned from member
//	functions of a user-defined Sphere class.  Given an object of a Sphere
//	class with a single, Radius, data member, produce member functions that
//	compute and return values for the Circumference and Volume of the
//	object.  Using this class and the global functions, print out the
//	data in columns for spheres that have radii of (as a minimum) 20 to 30
//	in increments of 1 unit. The output should look something like the
//	following (NOTE the centering between the margins required for the
//	first three lines of output):
//
// INPUTS:		Radius Values 20 - 30
//
// OUTPUTS:		Radius in Decimal & Binary. Circumference in HEX and 
//				Volume in Scientific Notation
//
//=======================================================================
//                          INCLUDE FILES
#include <iostream>
#include <iomanip>
#include <string>
#include "spherefunctions.h"
#include "sphere.h"

using namespace std;
//
//=======================================================================
//                        CONSTANT DEFINITIONS
//              <Constants which apply only to the file>
//
//=======================================================================
//                      EXTERNAL CLASS VARIABLES
//
//=======================================================================


//***********************************************************************
//                     BEGINNING OF PROGRAM CODE
//***********************************************************************
int main()
{
	double volume = 0;
	double circumference = 0;
//***********************************************************************
//                     Function Call to Center Text 
//***********************************************************************
	centerLine("Data On Spheres CS215 Sec ",80);
	centerLine("By,",80);
	centerLine("Kris Armstrong",80);
	
//***********************************************************************
//                     Function Call to Print Doulbe Horizontal Line
//***********************************************************************	
	printDoubleHorizontalLine(80);

//***********************************************************************
// Print values in decimal, binary, hex and scientific
//***********************************************************************	
		for(int i=20; i<31; i++)
			sphere::SetRadius(i) 
			printDecimal(i);
			printBinary (i);
	printHex(circumference);
	printScientific(volume);

//***********************************************************************
//                     Function Call to Print Doulbe Horizontal Line
//***********************************************************************	
	printDoubleHorizontalLine(80);
	
	return 0;
}

//***********************************************************************
//                    END OF PROGRAM CODE FILE
//***********************************************************************
//***********************************************************************
//***********************************************************************
// TITLE:                     Formatted Output
// FILENAME:                  CS215Lab2.cpp
// PREPARED FOR:              CS215, Section CSGA
// PROGRAMMER(S):             Kris Armstrong
// DEVELOPMENT DATE:          10/20/2011
// COMPILER USED:     	      Visual Studio 2010
// TARGET PLATFORM:           Dell Core i7
//=======================================================================
//                           PROJECT FILES
//    <LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT HERE>
//		CS215Lab2.cpp
//		sphere.h
//		sphere.cpp
//		spherefunction.h
//		spherefunction.cpp
//		
//=======================================================================
//   		REVISION HISTORY
//   List revisions made to the Program
//
//	DATE		PROGRAMMER				DESCRIPTION OF CHANGES MADE
//	10/28/11	Kris Armstrong			Original
//
//=======================================================================
//
//=======================================================================
//                  STANDARD AND USER DEFINED INCLUDES
#include <iostream>
//***********************************************************************
//              PROCESS THIS FILE ONLY ONCE PER PROJECT
//
#ifndef CS215Lab2_sphere_h //Preprocessor directives to prevent duplicate
#define CS215Lab2_sphere_h // declarations. Use the file name of the identifier
//substituting an _ underscore for the period
//=======================================================================
//                        CONSTANT DEFINITIONS
//              <Constants which apply only to the file>
//
//=======================================================================
//                      GLOBAL CONSTANTS & OBJECTS
//	     <Variables and Constants exported to other files>
//=======================================================================

//***********************************************************************
//                     Class declaration for sphere
//***********************************************************************

class sphere {
	int radius();
	
public:
	sphere();
	sphere(int InRadius);
	void(SetRadius(int NewRadius));
	int GetRadius();
	float CalcCircumference();
	float CalcVolume();
};

#endif
//***********************************************************************
//                    END OF PROGRAM CODE FILE
//***********************************************************************
//***********************************************************************
//***********************************************************************
// TITLE:                     Formatted Output
// FILENAME:                  CS215Lab2.cpp
// PREPARED FOR:              CS215, Section CSGA
// PROGRAMMER(S):             Kris Armstrong
// DEVELOPMENT DATE:          10/20/2011
// COMPILER USED:     	      Visual Studio 2010
// TARGET PLATFORM:           Dell Core i7
//=======================================================================
//                           PROJECT FILES
//    <LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT HERE>
//		CS215Lab2.cpp
//		sphere.h
//		sphere.cpp
//		spherefunction.h
//		spherefunction.cpp
//		
//=======================================================================
//   		REVISION HISTORY
//   List revisions made to the Program
//
//	DATE		PROGRAMMER				DESCRIPTION OF CHANGES MADE
//	10/28/11	Kris Armstrong			Original
//
//=======================================================================
//=======================================================================
//             CLASSES, FREE, AND FRIEND FUNCTIONS IMPLEMENTED
// class Entry
// Definition of class Entry for spheres
//
//***********************************************************************
//                               CONSTANTS
//
//***********************************************************************
//                  STANDARD AND USER DEFINED INCLUDES
#include "sphere.h"
#include <iostream>
using namespace std;
//=======================================================================
//                        CONSTANT DEFINITIONS
//              <Constants which apply only to the file>
const double PI = 3.14159265;
//=======================================================================
//                      EXTERNAL CLASS VARIABLES
//
//=======================================================================

//***********************************************************************
// Constructor For sphere
//***********************************************************************
sphere::sphere()
{
	int radius=0;
}

//***********************************************************************
// member function to set the radius
//***********************************************************************
void sphere::SetRadius(int newRadius)
{
	radius = newRadius <= 0 ? 1 : newRadius;
}    //to avoid the "0" issue and set a new radius

//***********************************************************************
// member function to get the radius
//***********************************************************************
int sphere::GetRadius()
{
	for(int i = 20; i < 31; i++)
		sphere::SetRadius(i);
}

//***********************************************************************
// member function to calculate circumference
//***********************************************************************
float sphere::CalcCircumference()
{
	double circumference = 0;
		circumference = PI * 2(radius)
};

//***********************************************************************
// member function to calculate volume
//***********************************************************************
float sphere::CalcVolume()
{
	double volume = 0;
	volume = radius*radius*radius*PI*4/3
};
//***********************************************************************
//                    END OF PROGRAM CODE FILE
//***********************************************************************
//***********************************************************************
//***********************************************************************
// TITLE:                     Formatted Output
// FILENAME:                  CS215Lab2.cpp
// PREPARED FOR:              CS215, Section CSGA
// PROGRAMMER(S):             Kris Armstrong
// DEVELOPMENT DATE:          10/20/2011
// COMPILER USED:     	      Visual Studio 2010
// TARGET PLATFORM:           Dell Core i7
//=======================================================================
//                           PROJECT FILES
//    <LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT HERE>
//		CS215Lab2.cpp
//		sphere.h
//		sphere.cpp
//		spherefunction.h
//		spherefunction.cpp
//		
//=======================================================================
//   		REVISION HISTORY
//   List revisions made to the Program
//
//	DATE		PROGRAMMER				DESCRIPTION OF CHANGES MADE
//	10/28/11	Kris Armstrong			Original
//
//=======================================================================
//
//***********************************************************************
//              PROCESS THIS FILE ONLY ONCE PER PROJECT
//
#ifndef	CS215Lab2_spherefunctions_h //Preprocessor directives to prevent duplicate
#define	CS215Lab2_spherefunctions_h // declarations. Use the file name of the identifier
//substituting an _ underscore for the period
//=======================================================================
//                        CONSTANT DEFINITIONS
//              <Constants which apply only to the file>
//
//=======================================================================
//                      GLOBAL CONSTANTS & OBJECTS
//	     <Variables and Constants exported to other files>
//=======================================================================

extern void printDecimal( long inNum );
extern void printHex( long inNum );
extern void printBinary( int inNum, int numDigits = 8 );
extern void printScientific( float inNum );
extern void centerLine( const std::string &text, unsigned int lineLength );
extern void printDoubleHorizontalLine(unsigned int lineLength);

#endif
//***********************************************************************
//                    END OF PROGRAM CODE FILE
//***********************************************************************
//***********************************************************************
//***********************************************************************
// TITLE:                     Formatted Output
// FILENAME:                  CS215Lab2.cpp
// PREPARED FOR:              CS215, Section CSGA
// PROGRAMMER(S):             Kris Armstrong
// DEVELOPMENT DATE:          10/20/2011
// COMPILER USED:     	      Visual Studio 2010
// TARGET PLATFORM:           Dell Core i7
//=======================================================================
//                           PROJECT FILES
//    <LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT HERE>
//		CS215Lab2.cpp
//		sphere.h
//		sphere.cpp
//		spherefunction.h
//		spherefunction.cpp
//		
//=======================================================================
//   		REVISION HISTORY
//   List revisions made to the Program
//
//	DATE		PROGRAMMER				DESCRIPTION OF CHANGES MADE
//	10/28/11	Kris Armstrong			Original
//
//=======================================================================

//*****************************************************************************
// 				INCLUDE FILES
#include <iostream>				// for cin,cout
#include <iomanip>				// for formatting cout
#include <string>
#include "spherefunctions.h"	

using namespace std;
//***********************************************************************************

//***********************************************************************
// function is what fills in the '.'
//***********************************************************************
void printDecimal (long inNum)

	char currentFill = cout.fill('.');		
	long currentFlags = cout.flags();		

	cout << left << setw(20) << inNum;
	cout.fill(currentFill);					
	cout.flags(currentFlags);
}

//***********************************************************************
// function puts the value in base 16 format
//***********************************************************************
void printHex(long inNum)				
{
	char currentFill = cout.fill('.');
	long currentFlags = cout.flags();

	cout.setf(ios::hex, ios::basefield);
	cout.setf(ios::showbase);
	cout << left << setw(20) << inNum;
	cout.fill(currentFill);				
	cout.flags(currentFlags);			
}

//***********************************************************************
// function puts the value in base 2
//***********************************************************************
void printBinary(int inNum, int numDigits)		
{
	for(int shiftAmt = numDigits = 0; shiftAmt >=-1; shiftAmt--)
	{
		cout << ((inNum >> shiftAmt) & 01 );
	}
	char currentFill = cout.fill('.');
	
	cout << setw(20 - numDigits) << '.';
	cout.fill(currentFill);
}

//***********************************************************************
// function puts the value in Scientific Notation
//***********************************************************************
void printScientific(float inNum)		
{
	long currentFlags = cout.flags();
	int oldPrecision = cout.precision();
	
	cout.precision(2);
	cout.setf(ios::scientific, ios::floatfield);
	cout << inNum;
	cout.flags(currentFlags);
	cout.precision(oldPrecision);
}

//***********************************************************************
// function to center text
//***********************************************************************
void centerLine(const string &text, unsigned int lineLength)

{
	cout << setw((text.length() + lineLength) /2) << text << endl;
}

//***********************************************************************
// Function to print horizontal line
//***********************************************************************
void printDoubleHorizontalLine(unsigned int lineLength)

{
	char currentFill = cout.fill('=');
	
	cout << setw(lineLength) << '=' << endl;
	cout.fill(currentFill);
}
//***********************************************************************
//                    END OF PROGRAM CODE FILE
//***********************************************************************
//***********************************************************************

I am having a couple of problems. I think I got the formatting down for the most part. I am having issues in a few calculations and how to iterate through them. Basically I need to take the radius and calculate the circumference and the volume. Once I have the calculation I need to print them Radius in Decimal and Binary, then circumference in Hex and finally volume in scientific notation.

Questions.
1.) where would be best to declare circumference and volume?

What do you mean declare?

2.) once they are declared whats the best place to do the calculations?

After you decide you have the info you need to make the calculations.

3.) I can't see an array for this assignment so how would I do this for radius values of 20 - 30.

So make one if you think you need one. Or calculate and print each value as you go through the loop if you don't need an array.

Please see the attached code.

400-500 lines of code? I don't think so. Try posting only the parts you are having trouble with. Make it a little easy on us.

Looking just at the actual code in sphere.h:

class sphere {
    int radius();
 
public:
    sphere();
    sphere(int InRadius);
    void(SetRadius(int NewRadius));
    int GetRadius();
    float CalcCircumference();
    float CalcVolume();
};

radius is presumably your member variable, so it should not be declared as though it's a function. Try int radius; at line 2. Also, what's with the extra parentheses around SetRadius at line 7?

We can go over sphere.cpp next, but I think you need to answer the first couple of questions first. Where do -you- think those variables should be declared? Where do -you- think the calculations should be performed?

400-500 lines of code? I don't think so. Try posting only the parts you are having trouble with. Make it a little easy on us.

My apologies I was not trying to suggest you look through the entire code. I was merely trying to show not only where I am at but how far I have come. I also wanted to provide as much detail as possible. Sometimes to much detail. Let me see if I can simplify here.

I need to take the radius's in the range of 20 - 30 and calculate the circumference, and volume. Now these need to be printer in the fallowing format
Radius Decimal, and Radius Binary, then circumference Hex and Volume Scientific notation. I believe I have the functions for printing the output correct.

I am having issues understanding how to take the radius and calculate each value. I know what the formula is for calculation. Hopefully the shortened code below will be of better help.

The part of my main doing the print out. And where I think I should go grab the calculations. My thought here is a for loop starting at 20 and ending at 30. So it would take a radius of 20 calculate the circumference and radius and print them then move on to 21 and 22 etc.

//***********************************************************************
// Print values in decimal, binary, hex and scientific
//***********************************************************************
for(int i=20; i<31; i++)
sphere::SetRadius(i)
printDecimal(i);
printBinary (i);
printHex(circumference);
printScientific(volume);

This contains my constructor for the class sphere who has one data member radius.

/***********************************************************************
// Constructor For sphere
//***********************************************************************
sphere::sphere()
{
int radius=0;
}
 
//***********************************************************************
// member function to set the radius
//***********************************************************************
void sphere::SetRadius(int newRadius)
{
radius = newRadius <= 0 ? 1 : newRadius;
} //to avoid the "0" issue and set a new radius
 
//***********************************************************************
// member function to get the radius
//***********************************************************************
int sphere::GetRadius()
{
for(int i = 20; i < 31; i++)
sphere::SetRadius(i);
}
 
//***********************************************************************
// member function to calculate circumference
//***********************************************************************
float sphere::CalcCircumference()
{
double circumference = 0;
circumference = PI * 2(radius)
};
 
//***********************************************************************
// member function to calculate volume
//***********************************************************************
float sphere::CalcVolume()
{
double volume = 0;
volume = radius*radius*radius*PI*4/3
};

Looking just at the actual code in sphere.h:

class sphere {
    int radius();
 
public:
    sphere();
    sphere(int InRadius);
    void(SetRadius(int NewRadius));
    int GetRadius();
    float CalcCircumference();
    float CalcVolume();
};

radius is presumably your member variable, so it should not be declared as though it's a function. Try int radius; at line 2. Also, what's with the extra parentheses around SetRadius at line 7?

We can go over sphere.cpp next, but I think you need to answer the first couple of questions first. Where do -you- think those variables should be declared? Where do -you- think the calculations should be performed?

Thanks for pointing that out. I fixed radius as well as the extra parenthesis.

Well, where do you need to calculate the circumference and volume? There are three choices:
1) Before you set the radius
2) After you set the radius and before you output answers
3) After you output answers
Choose wisely.

Now, where in

//***********************************************************************
// Print values in decimal, binary, hex and scientific
//***********************************************************************
for(int i=20; i<31; i++)
sphere::SetRadius(i)
printDecimal(i);
printBinary (i);
printHex(circumference);
printScientific(volume);

is that spot?

And don't forget to put your braces in the proper positions to get the loop correct.

OK, continuing on, your default sphere() constructor declares a local-variable radius which masks the member variable ... note how your next constructor, which takes an argument, simply sets the radius (but doesn't re-declare it as int).

Then your GetRadius(), CalcCircumference() and CalcVolume() methods are all declared that they return a value, but none of the implementations actually specify a return statement to return a value. The latter two also declare a local variable of type double but are declared to return type float. Pick one or the other, or (for maximum readability) explicitly cast the computed value to the return type when returning it, e.g.: return (float)value; Your loop for radius values between 20 and 30 (inclusive) looks fine, but is it the sphere-instance's job to perform that loop? Think of it in terms of what you really need to do: loop over different radius values, and then determine (and print) the circumference and volume of a sphere with that radius. Sounds like the loop should exist -outside- of the sphere implementation:

sphere s;
for (int r=20; r<=30; r++) {
    s.SetRadius(r);
    float c = s.CalcCircumference();
    ...
}

Ok everyone. I got the calculations all is looking good. I just have one findl peace to write and this is done. I"m having a hard time figuring out how to write the headers. Let me first share a sample output of what I have and then i'll post what the sample output should look like

The only part left is the herders

Data On Spheres CS215 Sec
                                 By,
                            Kris Armstrong
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20..................00010100.............0x7d................3.35e+004
21..................00010101.............0x83................3.88e+004
22..................00010110.............0x8a................4.46e+004
23..................00010111.............0x90................5.10e+004
24..................00011000.............0x96................5.79e+004
25..................00011001.............0x9d................6.54e+004
26..................00011010.............0xa3................7.36e+004
27..................00011011.............0xa9................8.24e+004
28..................00011100.............0xaf................9.20e+004
29..................00011101.............0xb6................1.02e+005
30..................00011110.............0xbc................1.13e+005
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

This is what it should look like. Except for some reason my post is not lingering up. But basically the headers should be centered.

Data on Spheres CS215 Sec <Your Section>  Lab 2
By
<Your Name>

RADIUS+++++++++RADIUS+++++++++CIRCUMFERENCE++++++++++VOLUME++++
DECIMAL	                  BINARY                         HEXIDECIMAL                          SCIENTIFIC

================================================================
20……………………00010100…………………..0x7d…………………………3.35e+004
21……………………00010101…………………..0x83…………………………3.88e+004
--------- cont.
================================================================

Here is my potential idea in code. Though I feel there is probably a better way using <string> ? Suggestions would be greatly appreciated and thank you all for you outstanding help

void printHeaderLineOne (unsigned int lineLength)
{
	char currentFill = cout.fill('+');		
	long currentFlags = cout.flags();		

	cout << left << setw(20) << "RADIUS";
	cout.fill(currentFill);	
	cout << internal << setw(20) << "RADIUS";
	cout.fill(currentFill);
	cout << internal << setw(20) << "Circumference";
	cout.fill(currentFill);
	cout << internal << setw(20) << "Volume";
	cout.flags(currentFlags);
}

Edited 5 Years Ago by karmstrong: n/a

How about something simple like

cout << "                      Data On Spheres CS215 Sec" << endl;
cout << "                                 By," << endl;
cout << "                            Kris Armstrong" << endl;

How about something simple like

cout << "                      Data On Spheres CS215 Sec" << endl;
cout << "                                 By," << endl;
cout << "                            Kris Armstrong" << endl;

Walt,

That would be fine except. I already have that section completed and we are not allowed to put spaces in. He wants us to use the cin.fill and sets and what not. The part I am having issues with is the headings for

RADIUS+++++++++RADIUS+++++++++CIRCUMFERENCE++++++++++VOLUME++++
DECIMAL BINARY HEXIDECIMAL SCIENTIFIC

So here is my latest attempt. The compiler dose not like me!!! :-)

spherefunctions.cpp

void headerLineOne(const string &one, string &two, string &three, string &four, unsigned int lineLength)
{
	char currentFill = cout.fill('+');		
	long currentFlags = cout.flags();
	cout << left << setw(20) << one;
	cout.fill(currentFill);
	cout << left << setw(20) << two;
	cout.fill(currentFill);
	cout << left << setw(20) << three;
	cout.fill(currentFill);
	cout << left << setw(20) << four;
	cout.flags(currentFlags);
	cout << endl;
}

spherefunction.h

extern void headerLineOne(const std::string &one, const std::string &two, const std::string &three, const std::string &four, unsigned int lineLength);

Finally my main

headerLineOne("RADIUS","RADIUS","CIRCUMFERENCE","VOLUME",screenSize);

So like I said the compiler is now very unhappy

/out:CS215lab2.exe
CS215lab2.obj
sphere.obj
spherefunctions.obj
CS215lab2.obj : error LNK2019: unresolved external symbol "void __cdecl headerLi
neOne(class std::basic_string<char,struct std::char_traits<char>,class std::allo
cator<char> > const &,class std::basic_string<char,struct std::char_traits<char>
,class std::allocator<char> > const &,class std::basic_string<char,struct std::c
har_traits<char>,class std::allocator<char> > const &,class std::basic_string<ch
ar,struct std::char_traits<char>,class std::allocator<char> > const &,unsigned i
nt)" (?headerLineOne@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D
@2@@std@@000I@Z) referenced in function _main
CS215lab2.exe : fatal error LNK1120: 1 unresolved externals

your prototype in spherefunction.h says all four strings should be const std::string & , yet your implementation in spherefunctions.cpp sepcifies that only the first one should be const. Keep all four const -- const-ness matters, especially when passing references: non-const reference variables can be modified by the function they're passed to, but clearly hard-coded strings aren't variables and can't be modified, thus if passed into pass-by-reference arguments, those arguments MUST be qualified as const.

This article has been dead for over six months. Start a new discussion instead.