In: Computer Science
C++ You should have one main function + 4 other functions for your submission for this lab. you have no need for extra functions beyond that, and you should not have less. You may use any pre existing functions already defined in class or a previous lab.
-Write a function storeTotal(,) that takes two arguments of type double, and has a return type of type Boolean. This function will take the number 256 and divide it by the second parameter, and add the result to the first parameter. It will return true afterwards.
-You should be making mindful decisions of which parameters should be call by value and which should be call by reference.
-If dividing by the second parameter would result in a run time error, the program does not do the calculation, and instead returns false.
-Ask the user to input two numbers, one at a time, discarding excess input each time.
-The program should keep looping until the user enters valid input.
-Once the user enters input, call function storeTotal appropriately.
-Whether storeTotal runs successfully (returns true) or not (returns false), display an appropriate message.
-Output the results of the variable that is cumulating value. This number is ALWAYS displayed in scientific notation, accurate to 3 decimal places
Repeat this 2 times.
Sample Output
How much do you already have? A
Invalid Input!
How much do you already have? Bck
Invalid Input!
How much do you already have? 42.4
What is the split factor? ,!
Invalid Input!
What is the split factor? 3.5
You now have 1.155e+002
How much do you already have? 35.6
What is the split factor? 0
That didn't go well, you still have 3.560e+001
*Explanation* 256/3.5 and then added to 42.4 gives 115.54, which, in scientific notation, gives the output above.
*Note* How scientific notation is displayed can vary from compiler to compiler, as long as you are getting it done through proper knowledge of C++ then the output does not need to look exactly the same.
Points :-
Program :-
#include <iostream>
#include <iomanip>
bool storeTotal(double *first, double second);
using namespace std;
int main() {
int T=2;
while(T>0){
double num1, num2;
bool result;
cout << "How much do you already have? ";
while (true)
{
cin >> num1;
if (!cin)
{
cout << "Invalid Input!\nHow much do you already have? ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;
}
else break;
}
cout << "What is the split factor? ";
while (true)
{
cin >> num2;
if (!cin)
{
cout << "Invalid Input!\nWhat is the split factor? ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;
}
else break;
}
result = storeTotal(&num1, num2);
if(result)
cout << "You now have : " << setprecision(3) << scientific << num1;
else
cout << "That didn't go well, you still have : " << scientific << num1;
cout << "\n";
cout << "\n";
T--;
}
return 0;
}
bool storeTotal(double *first, double second)
{
double res = 0;
bool flag = true;
if(! (second == 0))
{
res = 256.0/second;
}
else
{
flag = false;
}
*first += res;
return flag;
}