Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Urgent Help

paggy24paggy24 KuwaitMember Posts: 0
edited August 2014 in Beginner C/C++

So basically I need to put in error checking and a ofstream function to get a txt file. I am a beginner here, could someone please help me out.

Write, compile and test a C++ program that will call three functions, namely convertWeight( ), display( ), and writeResult( ). The convertWeight( ) accepts a real value (in ounces) and a character outUnits (g or G for grams, p or P for pounds, k or K for kilograms) and then converts the value given in ounces to the equivalent value in outUnits and displays this value to the screen. The display( ) function displays the program description as output to the user. The writeResult() function will write the user entered data and the calculated results to a file called out.txt. The following is the conversion chart:

OutUnits Symbol Conversion
G (for grams) ounce to gram: 1 oz = 28.349527 g
P (for pounds) pounds to kilogram: 1 lb. = 0.45392 kg
K (for kilograms)

Format Manipulators: setw ( ), setprecision ( ), setiosflags(ios::showpoint), setiosflags(ios::fixed), setiosflags(ios::scientific), setiosflags(ios::left), setiosflags(ios::right).
Escape Sequences: \b, \f, \n, \r, \, \',

EXAMPLE RUN (user input appears in bold):
Please enter the weight in ounce and the outUnit symbol according to the following chart:
G or g for grams
P or p for pounds
K or k for kilograms
100.0 g
100.000 ounces is equal to 2834.953 grams.

Make sure:
(i) To follow the "Program Template" for all of your program documentation.
(ii) Result should be displayed with three decimal place accuracy
(iii) Adequate number of sample runs is required
(iv) data validation is required.
(v) out.txt should also be uploaded along with your cpp file.

include

include<math.h>

include

using namespace std;

//Constants
const float ounce_gram = 28.349527;
const float pounds_kilogram = 0.45392;

// Declaration of Variables

float ounce_kilogram=ounce_gram/1000;
float ounce_pounds=pounds_kilogram*ounce_kilogram;

// Function to convert the values.
float convertWeight(float value,char ch)
{
if(ch=='G'||ch=='g')
{
cout<<value<< " ounces is equal to "<< valueounce_gram<<" grams" <<endl;
}
else if(ch=='k'||ch=='K')
{
cout<<value<< " ounces is equal to "<< value
ounce_kilogram << " kilograms" <<endl;
}
else if(ch=='P'||ch=='p')
{
cout<<value<< " ounces is equal to "<< value*ounce_pounds<< " pounds" <<endl;
}
else
cout<<"\nEnter a valid symbol and start program again"<<endl;

}

// Function to display output to the user
void display(void)
{
cout<< "This program converts ounces to grams,pounds or kilograms."<<endl;

}

// Main function
int main()

{
//Program Description
display();

 // Variables: types and names
float num; // Hold the value from user
char ch;    // Hold the selected character


 // Display Menu

cout<<"\nPlease enter the weight in ounces and the outUnit symbol according to the following chart"<<endl;
cout<<"\nG or g for grams"<<endl;
cout<<"P or p for pounds"<<endl;
cout<<"K or k for kilograms"<<endl;
cout<<"\nEnter outUnit symbol:  ";
// store selection
cin >> ch;
cout<<"Enter the real value:  ";
cin >> num;
cout << fixed << setprecision(3);

convertWeight(num,ch);


return 0;

}

Sign In or Register to comment.