C++ coding Help understaing code using namespace std; bool die( const string & m
ID: 3674991 • Letter: C
Question
C++ coding Help understaing code
using namespace std;
bool die( const string & msg ); <<<<<<<<<<<<<<< what does this mean?
int main(){
unsigned cents; // total # cents <<<<<<<<<<<<<<<<<< so unsigned is only positive numbers? what does the // mean?
cout <<"cents: ";
cin >>cents || die( "input failure" ); <<<<<<<<<<<<<<<<<<<<<< inputs a number or cents or die("input failure") so this means the fuction dies if what?
if( cents >= 25000 ) die( "too much money" );
cout <<cents/25 <<" quarters" <<endl;
cout <<cents%25/10 <<" dimes" <<endl; <<<<<<<<<<<<<<<<<< so cents/25, take remainder divide by 10 output dimes?
cout <<cents%25%10/5 <<" nickels" <<endl;
cout <<cents%5 <<" pennies" <<endl;
} // main
bool die( const string & msg ){
cout <<"Fatal error: " <<msg <<endl; <<<<<<<<<<<<< does this function have to be on all bool die statements? what exactally is it?
exit( EXIT_FAILURE );
} // die
Explanation / Answer
Hi, I have explained your doubts. Go through comments. Also i have simplified mathematical expression
#include<iostream>
using namespace std;
// this is function prototype
// this function returns bool type and take a constant string parameter as a refrence
bool die( const string & msg ); <<<<<<<<<<<<<<< what does this mean?
int main(){
// since cents can not be negative, thats why it is declared as insigned
unsigned cents; // total # cents << so unsigned is only positive numbers? what does the // mean?
cout <<"cents: ";
// cents has declared as int,
// cin will return false if you enter a letter rather than a digit.
// so in this case die function will be evaluated/called
cin >>cents || die( "input failure" ); // inputs a number or cents or die("input failure")
//so this means the fuction dies if what?
if( cents >= 25000 ) die( "too much money" );
cout <<cents/25 <<" quarters" <<endl; // getting number of quater
int rem = cents%25; // remainder , after quater
cout <<rem/10 <<" dimes" <<endl; //<<< so cents/25, take remainder divide by 10 output dimes?
rem = rem %10; // remainder affter dimes
cout <<rem/5 <<" nickels" <<endl;
cout <<cents%5 <<" pennies" <<endl;
} // main
// if user enter invalid input, then this function will called with message
// this function will print message and return failure status to Operating System
bool die( const string & msg ){
cout <<"Fatal error: " <<msg <<endl;//<<<< does this function have to be on all bool die statements?
//what exactally is it?
exit( EXIT_FAILURE );
} // die
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.