// This program shows how the toupper and tolower functions can be // applied in
ID: 3545906 • Letter: #
Question
// This program shows how the toupper and tolower functions can be
// applied in a C++ program
// PLACE YOUR NAME HERE
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
int main()
{
int week, total, dollars;
float average;
char choice;
Lesson 10A 191
cout << showpoint << fixed << setprecision(2);
do
{
total = 0;
for(week = 1; week <= 4; week++)
{
cout << "How much (to the nearest dollar) did you"
<< " spend on food during week " << week
<< " ?:" << endl;
cin >> dollars;
total = total + dollars;
}
average = total / 4.0;
cout << "Your weekly food bill over the chosen month is $"
<< average << endl << endl;
do
{
cout << "Would you like to find the average for "
<< "another month?";
cout << endl << "Enter Y or N" << endl;
cin >> choice;
} while(toupper(choice) != 'Y' && toupper(choice) != 'N');
} while (toupper(choice) == 'Y');
return 0;
}
Exercise 1: Run the program several times with various inputs.
Exercise 2: Notice the following do-while loop which appears near the end of the program:
do
{
cout << "Would you like to find the average for another month?";
cout << endl << "Enter Y or N" << endl;
cin >> choice;
} while(toupper(choice) != 'Y' && toupper(choice) != 'N');
How would the execution of the program be different if we removed this loop? Try removing the loop but leave the following lines in the program:
cout << "Would you like to find the average for another month?";
cout << endl << "Enter Y or N" << endl;
cin >> choice;
Record what happens when you run the new version of the program.
Exercise 3: Alter program case_convert.cpp so that it performs the same task but uses tolower rather than toupper.
Explanation / Answer
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
int main()
{
int week, total, dollars;
float average;
char choice;
Lesson 10A 191
cout << showpoint << fixed << setprecision(2);
do
{
total = 0;
for(week = 1; week <= 4; week++)
{
cout << "How much (to the nearest dollar) did you"
<< " spend on food during week " << week
<< " ?:" << endl;
cin >> dollars;
total = total + dollars;
}
average = total / 4.0;
cout << "Your weekly food bill over the chosen month is $"
<< average << endl << endl;
do
{
cout << "Would you like to find the average for "
<< "another month?";
cout << endl << "Enter Y or N" << endl;
// if you enter y,n ,Y or N then inner do-while loop end and go to outer do-while loop
cin >> choice; // if enter any other character then it will continue inner do-while loop
} while(toupper(choice) != 'Y' && toupper(choice) != 'N');
} while (toupper(choice) == 'Y'); // here it check user want to continue if enter Y (as toupper convert y to Y ) then it
//continue otherwise end
return 0;
}
========================================================================================
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
int main()
{
int week, total, dollars;
float average;
char choice;
Lesson 10A 191
cout << showpoint << fixed << setprecision(2);
do
{
total = 0;
for(week = 1; week <= 4; week++)
{
cout << "How much (to the nearest dollar) did you"
<< " spend on food during week " << week
<< " ?:" << endl;
cin >> dollars;
total = total + dollars;
}
average = total / 4.0;
cout << "Your weekly food bill over the chosen month is $"
<< average << endl << endl;
cout << "Would you like to find the average for "
<< "another month?";
cout << endl << "Enter Y or N" << endl;
cin >> choice; // it take any character for check
} while (toupper(choice) == 'Y'); // it continue only when enter y or Y (as toupper convert y to Y ) otherwise end
return 0;
}
===============================================================================
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
int main()
{
int week, total, dollars;
float average;
char choice;
Lesson 10A 191
cout << showpoint << fixed << setprecision(2);
do
{
total = 0;
for(week = 1; week <= 4; week++)
{
cout << "How much (to the nearest dollar) did you"
<< " spend on food during week " << week
<< " ?:" << endl;
cin >> dollars;
total = total + dollars;
}
average = total / 4.0;
cout << "Your weekly food bill over the chosen month is $"
<< average << endl << endl;
do
{
cout << "Would you like to find the average for "
<< "another month?";
cout << endl << "Enter Y or N" << endl;
// if you enter y,n ,Y or N then inner do-while loop end and go to outer do-while loop
cin >> choice; // if enter any other character then it will continue inner do-while loop
} while(tolower(choice) != 'y' && tolower(choice) != 'n');
} while (tolower(choice) == 'y'); // here it check user want to continue if enter y (as tolower convert Y to y )then it
//continue otherwise end
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.