1. Write a complete C++ program named "CheckAmount" that accepts command line arguments. It checks whether there is a command line argument of "-amount" followed by an amount number. It will print out that number amount. For all other cases, it will print out -1 For example, if you run this program with correct arguments as follows, it will print out 1.99, 0.75 and 1.1 respectively CheckAmount -amount 1.99 -help -amount 0.75 -help -check -amount 1.1 -verbose CheckAmount CheckAmount And if you run the program with invalid arguments, it will print -l in all cases CheckAmount -amount -verbose CheckAmount -help -amount CheckAmount -amount CheckAmount amount 1.0 CheckAmount eint main(int argc, char* argv[]) 81 82 cout < "1. CheckAmount program that checks commandline arguments:" <« endl; for (int i = 1; i < argc; i++) 83 84 85 Exception Thrown if (strcmp(argv[i], "-amount")) { 87 Exception thrown at 0X7AA2FF80 (ucrtbased.dllI) in MidtermPractice2b.exe: 0xC0000005: Access violation reading location Ox00000000 stod(argv[i 1]); true) int x = 8 if (isdigit(x) == { cout << argv[i 1] << endl; } 90 91 92 Copy Details Start Live Share session... else 93 Exception Settings Break when this exception type is thrown cout < -1 <« endl; 94 Except when thrown from: ucrtbased.dll 95 96 Open Exception Settings Edit Conditions 97 1

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

In my C++ program, I am trying to write code that will check command line arguments for "-amount" and if the next argument is a double, and if this is true, to print out the double value. So far I have the code: 

int main(int argc, char* argv[])
{
cout << "1. CheckAmount program that checks commandline arguments:" << endl;
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-amount"))
{
int x = stod(argv[i + 1]);
if (isdigit(x) == true)
{
cout << argv[i + 1] << endl;
}
else
cout << -1 << endl;

}
}

 

The debugger gives me an error because an exception is thrown with the conversion line of code. I do not yet know how to do exception handling, is there another way to check if the command line argument is a double?

1. Write a complete C++ program named "CheckAmount" that accepts command
line arguments. It checks whether there is a command line argument of "-amount"
followed by an amount number. It will print out that number amount. For all other
cases, it will print out -1
For example, if you run this program with correct arguments as follows, it will
print out 1.99, 0.75 and 1.1 respectively
CheckAmount
-amount 1.99
-help -amount 0.75
-help -check -amount 1.1 -verbose
CheckAmount
CheckAmount
And if you run the program with invalid arguments, it will print -l in all cases
CheckAmount
-amount -verbose
CheckAmount
-help -amount
CheckAmount
-amount
CheckAmount
amount 1.0
CheckAmount
Transcribed Image Text:1. Write a complete C++ program named "CheckAmount" that accepts command line arguments. It checks whether there is a command line argument of "-amount" followed by an amount number. It will print out that number amount. For all other cases, it will print out -1 For example, if you run this program with correct arguments as follows, it will print out 1.99, 0.75 and 1.1 respectively CheckAmount -amount 1.99 -help -amount 0.75 -help -check -amount 1.1 -verbose CheckAmount CheckAmount And if you run the program with invalid arguments, it will print -l in all cases CheckAmount -amount -verbose CheckAmount -help -amount CheckAmount -amount CheckAmount amount 1.0 CheckAmount
eint main(int argc, char* argv[])
81
82
cout < "1. CheckAmount program that checks commandline arguments:" <« endl;
for (int i = 1; i < argc; i++)
83
84
85
Exception Thrown
if (strcmp(argv[i], "-amount"))
{
87
Exception thrown at 0X7AA2FF80 (ucrtbased.dllI) in
MidtermPractice2b.exe: 0xC0000005: Access violation reading location
Ox00000000
stod(argv[i 1]);
true)
int x =
8
if (isdigit(x)
==
{
cout << argv[i 1] << endl;
}
90
91
92
Copy Details
Start Live Share session...
else
93
Exception Settings
Break when this exception type is thrown
cout < -1 <« endl;
94
Except when thrown from:
ucrtbased.dll
95
96
Open Exception Settings Edit Conditions
97
1
Transcribed Image Text:eint main(int argc, char* argv[]) 81 82 cout < "1. CheckAmount program that checks commandline arguments:" <« endl; for (int i = 1; i < argc; i++) 83 84 85 Exception Thrown if (strcmp(argv[i], "-amount")) { 87 Exception thrown at 0X7AA2FF80 (ucrtbased.dllI) in MidtermPractice2b.exe: 0xC0000005: Access violation reading location Ox00000000 stod(argv[i 1]); true) int x = 8 if (isdigit(x) == { cout << argv[i 1] << endl; } 90 91 92 Copy Details Start Live Share session... else 93 Exception Settings Break when this exception type is thrown cout < -1 <« endl; 94 Except when thrown from: ucrtbased.dll 95 96 Open Exception Settings Edit Conditions 97 1
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 9 steps with 8 images

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education