Greetings, this is my first time posting here, I've been a long time lurker. I have a lab assignment 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 generated so far, it builds successfully but does not work correctly and I can't figuru out why? Any help would be greatly appreciated. Thanks for any help or suggestions!

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char letter;
int count = 0;
double ppl = 0;
double finalCost = ppl * (count - 1);

cout << "\tWelcome to My Price Per Letter Calculator!\n";
cout << " What is your next character? Type '*' to end : ";
cin >> 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;
}

Recommended Answers

All 15 Replies

Call me crazy, but I'm pretty sure you misinterpreted "Do not use any string or array functions"
How can you store a letter (sentence, letter as in formal message) without at least allocating memory for it in some arbitrary place or another?
This tells me that it must be OK to at least have an array of strings to store the sentences.

I'm pretty sure the assignment is suggesting that you may not use std::for_each, std::getline, and etc...

Call me crazy, but I'm pretty sure you misinterpreted "Do not use any string or array functions"
How can you store a letter (sentence, letter as in formal message) without at least allocating memory for it in some arbitrary place or another?

I don't see where in this assignment does it say print the sentence, so thereofre would a string or an array really be needed?

Close, but no banana. You need to store the letters in a char array, and the count should not be subtracted by 1 at the end. IE:

#include <iostream>
#include <stdio.h>
#define MAXLETTERS 100;
using namespace std;
int main(int argc, char* argv[])
{
    char letters[MAXLETTERS+1];
    char letter = 0;
    int count = 0;
    double ppl = 0;
    char done;
    double finalCost = ppl * (count - 1);
    cout << "\tWelcome to My Price Per Letter Calculator!\n";
    cout << " What is your next character? Type '*' to end : ";
    do
    {
        cout << " What is your next character? Type '*' to end : " << flush;
        letter = fgetc(stdin);
        if (letter != '*')
        {
            letters[count] = letter;
            count++;
        }
    }
    while (letter != '*' && count < MAXLETTERS);
    letter[count] = 0; // Terminate the string.
    cout << " What is the price per letter to pay? ";
    cin >> ppl;
    cout << " You have "<< dec << count << " letters at $"<< ppl
         << " per letter, and your total cost is $" << finalCost << "."
         << endl;
    cout << "Press any key to finish: " << flush;
    fgetc(stdin);
    return 0;
}

Note the combination of C and C++ i/o functions. This is a case where C functions are sometimes simpler to use than C++, yet can be used interleaved as shown. Exercise for you - convert the fgetc() function calls to C++ ones that will return after getting one character... :-) In your original code, this isn't happening - cin >> letter will only return after something like an enter key is pressed, or a separator (space, tab, etc) is detected.

I also go with the above comments...

But I am having a suggestion here...

declare a varible to store the string to be printed(as mentioned in your question)

just use the builtin function strlen(variable name) to calculate how many characters are there in the variable.

Then get the price per letter

Calculate the result.

A simple method :-D

But if you need for loop then go with manual counting procedure as others suggested.

Hope this helps you.

Have a happy coding time..

Ok, Thanks for all the input. I did a redo on the code. I even got it to where it doesn't count spaces in the Character total. HOWEVER, when I input the price of $3.45 per character it's taking the spaces into that Calculation, for instance if I type: (space)Hello(space)World It will tell me I have 10 chars. But when I enter the price per letter and it calculates the total cost, it's adding those two (spaces) into the total thus upping the cost. Any ideas/suggestions? Here's my reworked code. Thanks in advance, it's greatly appreciated!

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace 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;

}

So letter is in fact literal, and the cost is per letter of a large banner.

Your first approach was closer to the correct solution, inupt each letter.
Additionally, you could add a predicate ([alphanumeric]) at the time of input.

for(;letter!='*')
{
    cin >> letter;
    if( isAlphanumeric(letter) ) ++count;
}

So I'm better off keeping my initial code I first posted to meet the requirements of the assignment?

You can keep it.. Thats your wish... But for your knowledge,I think you can make use of trim() to remove white spaces i hope.

@OP
[
Keep your original code. Change (count - 1) to (count)
[1 letter] is [1 many PPL]
Add a predicate [isAlphanumeric] to filter whitespace, etc...
]
@ss125
[
No he may not use trim(), as he may not use string/array [library] functions;
as stated explicity and being the primary topic of the thread at present.
]

@Unimportant

As I suggested above it's just an idea...

Anyway ur suggestion also helped me...:-D

Thank U

@Unimportant, Thanks, that solved that issue..

Keep your original code. Change (count - 1) to (count)

But, do you have any ideas, when I build the program, it builds correctly but when I run it, If I enter a letter or type a word and press return, it will show the letter but then I can't progress and anything else.

The only way I can progress through the program is if I enter the * key. Which at the end then asks me how much each letter is, and when I enter $3.45 it's totaling $0 dollars because there's no characters.
I'm not able to input any characters? I attached my new code,

P.S- I'm holding off on adding the alphanumeric part until I can get passed this dilemma!

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

char letter;

int count = 0;

double ppl = 0;

double finalCost = ppl * (count);

cout << "\FSchioppa_Module4_LabAssignment\n";

cout << " What is your next 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) << " letters at $"<< ppl <<" per letter, and your total cost is $" << finalCost << ".";

system("pause");

return 0;

}

Firstly,
for ( ; letter != '*'; ++count);
for(;;); means, "Forever, do nothing."
This is because for refers to exactly 1 statement [do]
Typically enclosed by brackets, i.e. for(...) { expr };
for( ... ) ; means,
for( ... ) { };
which will invariably terminate immediately if the opening condition is false, or continue forever.

In otherwords, you have a typo in your code.
It's also important [when] you initialize finalPrice.
You always initialize finalPrice while count is 0.
double finalCost = ppl * (count); <-- count is 0 at this moment.

By the way, here was how I wrote your problem for my own compiler after I copied it.

#include <iostream>
using namespace std;
int main()
{
    int count(0);
    cout << "Schioppa_Module4_LabAssignment\r\n";
    cout << "What is your next character? Type '*' to end : \r\n";
    for (char letter=0;letter != '*';)
    {
        letter = std::getchar(  );
        if( letter > 47 && letter < 58 ) ++count;    // [ 0 - 9 ]
        if( letter > 64 && letter < 91 ) ++count;    // [ A - Z ]
        if( letter > 96 && letter < 123 )    ++count;    // [ a - z ]
    }
    cout << "What is the price per letter to pay?\r\n";
    double ppl(0.00);
    cin >> ppl;
    double finalCost( ppl * count );
    cout << "You have "<< count << " letters at $"<< ppl <<" per letter, and your total cost is $" << finalCost << ".\r\n";
    system("pause");
}

Which works as you had intended your program to work.

I'll be damned, took out the semi-colon in for ( ; letter !='*';++count) and substituted the semicolon with char and it's working perfect!!!

I LOVE C++, you'd think it wouldn't build correctly but I guess technically it was correct, I was telling it to do nothing up to that point.

Thanks for the knowledge! it's much appreciated!!

Best regards,
Frank

use for refers to exactly 1 statement [do]
Typically enclosed by brackets, i.e. for(...) { expr };

for ( ; letter != '*'; ++count);
for(;;); means, "Forever, do nothing."
This is because for refers to exactly 1 statement [do]
Typically enclosed by brackets, i.e. for(...) { expr };
for( ... ) ; means,
for( ... ) { };

Understood. Which, since it was originally not recording any characters was 0ing out but could potentially cause issues in the calculation, correct?

It's also important [when] you initialize finalPrice.
You always initialize finalPrice while count is 0.
double finalCost = ppl * (count); <-- count is 0 at this moment.

you'd think it wouldn't build correctly but I guess technically it was correct

This is a key lesson with C++. Just because it compiles doesn't mean it's correct, either semantically where the language is concerned or logically in terms of the application's intended behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.