Question

Please help I am confused to where start. It C++ program thank you The stoi function...

Please help I am confused to where start. It C++ program thank you

The stoi function converts a string to an integer, but does not check whether or not the value of the string is a good integer. If the stoi function fails, an exception is thrown and the program terminates.

Write a program that asks the user to enter two strings, one called sNumerator and the other sDenominator. Using the stoi function, convert the two strings to numbers but individually catch any exceptions and send a message back to the user that the numbers they entered were not valid. If both convert successfully to numbers, divide the numerator by the denominator and catch the exception where the denominator is zero. (You CANNOT check for zero using an if statement before doing the calculation). If the exception is thrown, notify the user that you cannot divide by zero otherwise display the calculated fraction.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
int main() {

string sNumerator;
string sDenominator;
cin >> sNumerator >> sDenominator;
int iNumerator;
int iDenominator;
  
try { //try_1

iNumerator = stoi(sNumerator);
iDenominator = stoi(sDenominator);

try { //try_2
float result = (float)iNumerator / iDenominator ;
cout << "The Result of the Division is " << result << endl;
}   
catch(exception& e) { //catch_2
cout << "Cannot Devide by Zero" << endl;
}
}
catch(exception& e) { //catch_1
cout << "The Numbers you Entered is not Valid" << endl;
}

return 0;
}

/*Note that In C++ the Exception is not thrown when we try to divide by zero but if you want to throw an Exception you can do so externally. below is the code for that */

#include <iostream>
#include <stdexcept>
using namespace std;

float CheckDenominator(float denominator)
{
// if denominator is zero
// throw exception
if (denominator == 0)
{
throw runtime_error("Math error: Attempted to divide by zero\n");
}
else
return denominator;
} // end CheckDenominator

int main()
{

string sNumerator;
string sDenominator;
cin >> sNumerator >> sDenominator;
int iNumerator;
int iDenominator;

try
{ //try_1

iNumerator = stoi(sNumerator);
iDenominator = stoi(sDenominator);

try
{ //try_2
float result = (float)iNumerator / CheckDenominator(iDenominator);
cout << "The Result of the Division is " << result << endl;
}
catch (exception &e)
{ //catch_2
cout << "Cannot Devide by Zero" << endl;
}
}
catch (exception &e)
{ //catch_1
cout << "The Numbers you Entered is not Valid" << endl;
}

return 0;
}

/*please give an upvote if you like :) */

Add a comment
Know the answer?
Add Answer to:
Please help I am confused to where start. It C++ program thank you The stoi function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Division by Zero Problem: Create a program titled Division. Have the program prompt the user "Enter...

    Division by Zero Problem: Create a program titled Division. Have the program prompt the user "Enter numerator and denominator" and store the two values entered. (If you wish the user to "Enter numerator/denominator" be sure to adjust your data gathering variables to include a holder variable for the slash character.) Include a function titled Quotient that takes 2 integers as input and provides the quotient of the 2 integers as integer output. (The remainder is truncated. Thus, 5/3 will be...

  • The program needs to be written in C. Write a function void camelCase(char* word) where word...

    The program needs to be written in C. Write a function void camelCase(char* word) where word consists of more than two words separated by underscore such as “random_word” or "hello_world_my_name_is_sam". camelCase() should remove underscores from the sentence and rewrite in lower camel case” (https:// en.wikipedia.org/wiki/Camel_case). Watch out for the end of the string, which is denoted by ‘\0’. You have to ensure that legal strings are given to the camelCase() function. The program should only run when the input is...

  • C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

    C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...

  • In this assignment you are going to handle some basic input operations including validation and manipulation,...

    In this assignment you are going to handle some basic input operations including validation and manipulation, and then some output operations to take some data and format it in a way that's presentable (i.e. readable to human eyes). Functions that you will need to use: getline(istream&, string&) This function allows you to get input for strings, including spaces. It reads characters up to a newline character (for user input, this would be when the "enter" key is pressed). The first...

  • File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute...

    File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. In this program, two things can possibly occur: The program always returns 1 if negative integers are entered by the user. Returning 1 as the factorial of any negative integer is not correct. The program also returns negative numbers when the user enters integers greater than 16. Returning a negative number for values over 16 also...

  • C++ language only. Thank you C++ language (cout <). NO atoi function. And please pay attention...

    C++ language only. Thank you C++ language (cout <). NO atoi function. And please pay attention to the rules at the bottom. Extra good rating to whoever can help me with this. TIA. In certain programming situations, such as getting information from web forms, via text boxes, the data coming in may need to be numerical (we want to perform arithmetic on it), but it is stored in the form of a list of characters (the numerical value stored is...

  • Write a program in C++. You need everything everythihng given You will create a function that...

    Write a program in C++. You need everything everythihng given You will create a function that validates input. It should accept only non-negative integers. If the input is incorrect the function should throw an exception. The exception will not be caught in the function. You will create a function that reads in 2 integer values. One is a number that will be raised to the power of the second. So if 5 and 6 are entered the result would be...

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • Please note that I cannot use the string library function and that it must be coded...

    Please note that I cannot use the string library function and that it must be coded in C89, thank you! Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text in the first problem from “Please enter a string of maximum 30 characters:” to “Enter string: ”. Your assignment will be auto-graded and any changes in formatting will result in a loss in the grade. 2.Comments: Header comments...

  • I am required to use the try - catch block to validate the input as the...

    I am required to use the try - catch block to validate the input as the test data uses a word "Thirty" and not numbers. The program needs to validate that input, throw an exception and then display the error message. If I don't use the try - catch method I end up with program crashing. My issue is that I can't get the try - catch portion to work. Can you please help? I have attached what I have...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT