Hello, I have a lab assignment do which is:
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.
Your application should only use "FOR" loops. Do not use any String or Array functions.
This is the code I have come up with, I even was able to get it to not count the spaces just the letter characters, however, when I input the price per letter of $3.45, it's adding the spaces into the cost. Am I going about this the right route? Any help suggestions are greatly appreciated! Below is the code I have thus far. Thanks again!
#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
double cost, total;
string banner;
cout << "Please Enter Your Text for Banner Here:" ;
getline(cin, banner); //User inputs their custom text
int numOfChars = banner.length();
// Outputs how many letters are in the created banner
for (int i = 0; i < banner.length(); i++) {
if (banner.at(i) == ' ') {
numOfChars--;
}
}
cout << "You have " << numOfChars << " letters in this sentence." << endl;
cout << endl;
cout << "What is the cost of each letter? $";
cin >> cost;
cout << endl;
// Calculates the total cost of text in your banner
total = cost * (double)banner.length();
cout << "Your total cost is: $ " << total << endl;
cout << endl;
system("PAUSE");
return 0;
}
Ok, I think I understand what you're getting at "sorry I'm new to c++". Cost * (double)banner.length. The double banner.length is what is causing the issue correct? I tried taking (double)banner.length out and using cost*numOfChars but it won't compile. So I'm guessing its not numOfChars?
Thanks for the quick response, it's greatly appreciated!
You're right. You should be multiplying cost by numOfChars. You must be typing something incorrectly (or forgetting a semicolon) when you modified it for it to be giving you a compiler error.
haha. Yes, user error on my part, was accidentally typing the number 0 in place of capital O!!!!! Oiiii Vayyyyy... Thanks Thumper!! It's calculating perfectly now!
Ok, Instructor said my code above doesn't follow the rules of not using String or Array Functions!! ugghhhhhh.. I can only use FOR loops. So I started from scratch, this new code does compile but its not working correctly, for example, when I ask "input your character" If I input a character the character gets displayed and the program just locks up. But if I don't input a character and I enter the * key, the program will advance.. Here's the new code I'm working on avoiding the use of strings or array functions. I'm at a complete mind freeze right now.. Thanks again!
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char letter;
int count = 0;
double ppl = 0;
double finalCost = ppl * (count - 1);
cout << "\FSchioppa_Module4_PricePerLetter!\n" ;
cout << " What is your character? Type '*' to end : ";
cin >> letter;
cout << letter;
for ( ; letter != '*'; ++count);
{
cout << " What is your next character? Type '*' to end : ";
cin >> letter;
}
cout << " What is the price per letter to pay? ";
cin >> ppl;
cout << " You have "<< (count - 1) << " letters at $"<< ppl <<" per letter, and your total cost is $" << finalCost << ".";
system("pause");
return 0;
}