Scenario: As you peruse various websites on the Internet, looking to create a ba
ID: 3696503 • Letter: S
Question
Scenario:
As you peruse various websites on the Internet, looking to create a banner for your new business, you notice that many places charge you by the letter. You want to be able to quickly count the letters on the various banners you design, so you decide to create a letter counter for yourself.
Write an application where you ask the user to input the price per letter (PPL), and then ask the user to input the sentence they want printed. The application should then calculate the number of letters and give the user the total cost in the following manner:
You have 40 letters at $3.45 per letter, and your total is $138.00.
Important Your application should only use "FOR" loops. Do not use any String or Array functions.
This must be written in C++
Explanation / Answer
/** C++ code to quickly count the letters on the various banners design **/
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int count(0);
for (char letter=0;letter != '*';)
{
// input the sentence whose print is required
cout << "What is your next character? Type '*' to end : ";
cin >> letter;
if( letter > 47 && letter < 58 ) ++count; // [ 0 - 9 ]
if( letter > 64 && letter < 91 ) ++count; // [ A - Z ]
if( letter > 96 && letter < 123 ) ++count; // [ a - z ]
}
// input the price per letter (PPL)
cout << "What is the price per letter to pay? ";
double ppl(0.00);
cin >> ppl;
// calculate the total cost
double finalCost( ppl * count );
// print the number of letters and give the user the total cost
cout << "You have "<< count << " letters at $"<< ppl <<" per letter, and your total cost is $" << finalCost << ". ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.