Question

In: Computer Science

C++ You should have one main function + 4 other functions for your submission for this...

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.

Solutions

Expert Solution

Points :-

  • cin.clear() :- It clears the error on cin to get desired input.
  • cin.ignore(numeric_limits<streamsize>::max(), '\n') :- It will ignore the characters untill '\n' reads. Ignore function is used skip character from the input string untill the delimeter occurred.

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;

}


Related Solutions

C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
Programming language Python It have to be in Functions with a main function Samuel is a...
Programming language Python It have to be in Functions with a main function Samuel is a math teacher at Hogwarts School of Witchcraft and Wizardry. He loves to give his students multiplication exercises. However, he doesn’t care about the actual operation result but the unit sum of its digits. At Hogwarts School of Witchcraft and Wizardry, they define the unit sum (US) of N as the unit that it is left after doing the sum of all the digits of...
Language C++ *note* Your function count should match those given in the description; you have no...
Language C++ *note* Your function count should match those given in the description; you have no need for extra functions beyond that, and you should not have less. -Write a function, doTheRegression(), that takes as input a variable of type integer and returns a value of type double. It computes and returns 80% of the value given as the amount after reduction. -In your main function, write code that asks the user what the original amount is, and if they...
Write the functions needed by the main function that is given. multiply2nums should accept 2 values...
Write the functions needed by the main function that is given. multiply2nums should accept 2 values and return the product (answer you get when you multiply). greeting should accept one value and print an appropriate greeting using that value. For example, if you sent "Steven" to the greeting function, it should print "Hello, Steven" DO NOT CHANGE ANYTHING IN main() def main(): user = input("Please enter your name ") greeting(user) try: num1 = int(input("Please enter an integer ")) num2 =...
Answer one of the following questions in essay form. Your submission should be at least one...
Answer one of the following questions in essay form. Your submission should be at least one page long. The essay is worth 10 points. Topic: Addiction Choose one Essay Topic for 10 points Are you addicted to technology? Does your use of technology negatively affect your daily life? Is there addiction in your family? Do you want to share your experience with addiction in family member's or friend's life?
Split the main function given into multiple functions. You have been given a very simple program...
Split the main function given into multiple functions. You have been given a very simple program that performs basic operations (addition, subtraction, editing) on two randomly generated integer vectors. All functionality has been included in main, causing code segments to be repeated as well as diminishing the readability. Rewrite the program by grouping calculations and related operations into functions. In particular, your program should include the following functions. InitializeVectors: This is a void function that initializes the two vectors by...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT