Welcome to Minetown!
To join our community, please login or register!
Internet Explorer
Internet Explorer is not supported. Please upgrade to a more modern browser.

[Enjin Archive] This weeks C++ homework!
Started by Unknown User

if you were doing java I could help you, but sorry mate
English please guys o_o
I just realized on the 2nd exercise I forgot to do setw(6). So we can insert numbers longer than 6 digits in length.. Probably going to get a point off for that.
Moar Homework!

1. Write a program that asks for four floating point numbers then prints them (nicely formatted) and displays the sum of the four. Sample output:

Enter number 1: 0

Enter number 2:123.123456677658

Enter number 3: 99999999999.12312412341234

Enter number 4: 27

0.0000

123.1235

99999999999.1231

27.0000

====================

100000000149.2466

2. Write a program that asks for a string, an integer and a float then writes the values to a file. Sample output:

Enter a string, integer, and float separated by spaces: thisisastring 15 123.123

Write complete!

When you open the file, it should contain:

thisisastring

15

123.123

3. Write a program that reads the the file you wrote above and displays the contents. Sample output:

Reading file...

Contents:

thisisastring

15

123.123
Nobody's gonna do this.. we can just read C++ for like 3 months and code an insane proggy <object class="emojione" data="https://resources.enjin.com/1489581540/themes/core/images/emojione/svg/1f642.svg?0" type="image/svg+xml" standby=":)">:)</object>
I gots number 1!

//Project 1. Asking for 4 long double numbers then displaying them with 4 decimal places and all added together.

long double num1, num2, num3, num4;

cout << "Enter number 1: ";

cin >> num1;

cout << "Enter number 2: ";

cin >> num2;

cout << "Enter number 3: ";

cin >> num3;

cout << "Enter number 4: ";

cin >> num4;

cout << "\n" << fixed << setprecision(4) << num1 << endl;

cout << fixed << setprecision(4) << num2 << endl;

cout << fixed << setprecision(4) << num3 << endl;

cout << fixed << setprecision(4) << num4 << endl;

cout << "====================\n";

cout << fixed << setprecision(4) << (num1+num2+num3+num4) << endl;

cin.get();

cin.get();
//Exercise 2. Asking the user to type in a word, an integer and a float then displaying it in a text document.

ofstream outputFile;

outputFile.open("exercise2.txt");

cout <<"\nEnter a string, integer, and float separated by spaces: ";

char outputtext ;

int outputnumber;

float floatnumber;

cin >> outputtext;

cin >> outputnumber;

cin >> floatnumber;

outputFile << outputtext << endl;

outputFile << outputnumber << endl;

outputFile << floatnumber << endl;

outputFile.close();

cout <<"Write complete!";

cin.get();

cin.get();

//Exercise 3. Reading what we just wrote into the text document.

ifstream inFile;

const int SIZE = 81;

char line;

inFile.open("exercise2.txt");

cout << "\n\nReading file...\nContents:\n";

inFile >> line;

cout << line << endl;

inFile >> line;

cout << line << endl;

inFile >> line;

cout << line << endl;

inFile.close();

cin.get();

return 0;

}

I did it all by myself cryston! <object class="emojione" data="https://resources.enjin.com/1489581540/themes/core/images/emojione/svg/1f600.svg?0" type="image/svg+xml" standby=":d">:d</object>
1. Write a program that performs basic calculator operations – addition, subtraction, multiplication and division of 2 doubles. Below is a sample run of the program. The inputs entered by the user have been highlighted.

Enter two numbers: 1.2 2.3

Choose from the following menu items:

a) Add the numbers

b) Subtract the numbers

c) Multiply the numbers

d) Divide the numbers

>a

1.200 + 2.300 is 3.500

Similar results should be displayed if the user selects subtraction, multiplication or division. For example:

5.000 – 3.000 is 2.000

5.000 X 3.000 is 15.000

6.000 / 2.000 is 3.000

1. Now that the basic calculator is working, let’s add some error handling. There are two errors to be concerned with. If the user enters an invalid option, for example z, then the following message should be displayed:

Invalid Selection!

If the user tries to divide by zero (the second number they enter is zero and they choose option d) then display the following error message:

You attempted to divide by zero! You should be ashamed of yourself!
#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

//Variables for the 2 input numbers & selection for the user.

double num1, num2;

char selection;

//Asking the user to input the 2 numbers needed.

cout<<"Enter two numbers: ";

cin>>num1>>num2;

//Displaying the calculators options to do with the 2 numbers & asking which option you want to use.

cout<<"\n\n\nChoose from the following menu items:\n\n";

cout<<"a) Add the numbers\n";

cout<<"b) Subtract the numbers\n";

cout<<"c) Multiply the numbers\n";

cout<<"d) Divide the numbers\n\n";

cout<<">";

cin >> selection;

//The code that processes your selection and calculates your answer.

if(selection=='a')

cout<<"\n"<<fixed<<setprecision(3)<<num1<<" + "<<num2<<" is "<<num1+num2;

else if(selection=='b')

cout<<"\n"<<fixed<<setprecision(3)<<num1<<" - "<<num2<<" is "<<num1-num2;

else if(selection=='c')

cout<<"\n"<<fixed<<setprecision(3)<<num1<<" X "<<num2<<" is "<<num1*num2;

else if(selection=='d')

if(num2==0)

cout<<"\nYou attempted to divide by zero! You should be ashamed of yourself!";

else

cout<<"\n"<<fixed<<setprecision(3)<<num1<<" / "<<num2<<" is "<<num1/num2;

else

cout<<"\nInvalid Selection!";

//Waiting for the user to see his answer/response then waiting for the user to terminate the program.

cin.get();

cin.get();

return 0;

}

I just made that project my bitch!
I hated C++