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
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
In my C++
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?
Trending now
This is a popular solution!
Step by step
Solved in 9 steps with 8 images