// This program is supposed to figure out how much does a custom banner cost by figuring out the price for each letter and outputting it's total to the screen.

#include <iostream>
#include <string>
using namespace std;

int main()
{
	double cost, total;
	string banner;
	cout << "Enter Your Banner Name (Do not use spaces)" << endl;
	cout << endl;
	getline(cin,banner); //User inputs their custom banner name
	cout << endl;

	// Outputs how many letters are in the banner
	for (int i = 0; i < banner.length(); ++i) 
		cout << "This sentence has " << i << " letters in it." << endl;
	cout << endl;

	cout << "How much does each letter Cost? $";
	cin >> cost;
	cout << endl;

	// Calculates the total cost of your made banner
	total = cost * (double)banner.size(); 
	cout << "Your total cost is: $ " << total << endl;
	cout << endl;

	system("PAUSE");
	return 0;
}

When I run my program and type in I am Iron man it messes up. It says that I have 13 letters. It counts my spaces as letters. How can i fix this??

Recommended Answers

All 19 Replies

loop through the string and count the number of white spaces -- isspace() macro will tell you if it is a white space or not (white space is space, tabs and something backspace characters). Then subtract that from the total string length. Or just count everything except white space and you won't have to subtract anything.

If you're not doing this as an exercise, you can use std::count() to find the number of spaces and then subtract this from the total:

#include <iostream>
#include <string>
#include <algorithm>  // std::count() is in here

int main()
{
    std::string banner;
    std::cout << "Enter banner text:" << std::endl;
    getline(std::cin, banner);

    int spaces = std::count(banner.begin(), banner.end(),' ');

    std::cout << "Banner is " << banner.size() - spaces << " chargable letters." << std::endl;
    return 0;
}
commented: yours helped it work +0

Also, the functions in <cctype> may be helpful.
particularly isspace() and isalnum().
#include <cctype>

This is another useful way you could do it. The <string> library has an "at" function which lets you check an individual character of the string. You can then use an if statement inside of a loop to check all the characters of a string.

How you use it is you precede .at(#) (# being any integer) after your string variable and it will give you the result of the #'th letter in the string. Ex: text.at(5) gives you the 5th character in the string named text.

Here's how you can use it.

int letterCounter = 0;
    for (int i = 0; i < text.length(); i++)
    {
        if (text.at(i) != ' ')
        {
                       letterCounter++;
        }
    }

This code checks every character of the string "text" and adds 1 to every character that is NOT ' ' or in other words, a space. You could also use to it check other letters like 'a', 'b' or ect. Just remember to use the single quotation (') around the character or you might run into a compiler/logical error.

>> The <string> library has an "at" function which lets you check
Yes it does, but it is more convenient to just use the [] operator, and what about the tabs that may be in the string? Those have to be ignored too, which is why isspace() needs to be used.

int letterCounter = 0;
    for (int i = 0; i < text.length(); i++)
    {
        if (!isspace(text[i]))
        {
            letterCounter++;
        }
    }

>> The <string> library has an "at" function which lets you check
Yes it does, but it is more convenient to just use the [] operator

Yea... your right, I wasn't exactly aware that could use use the [] operator just like an array of characters, which is pretty much what it is after all, I am more fond of the .at() method so I used it that way.

If I'm not mistaken .at() will throw an exception if the programmer tries to access an element beyond the bounds of the array--while operator[] will not.

Consequently, .at() may be less efficient (so I've heard).

Wow! Thanks guys for all your help. I will try and do this, and if it comes up bad i will post my updated code. Thanks again

It didnt work

It didnt work

Which of the many suggestions are you talking about? Some of them absolutely will work.

Nvm guys, it worked! Thanks for all of your help =)

Assuming you can get your input into a string...

//Caleb Taylor 2011
//This is one way to do it.
#include <iostream>
#include <string>
#include <cctype>

int main()
{
  using namespace std;
  string input = "I want this on a banner.";
  unsigned int count = 0;
  for(string::iterator it = input.begin(); it != input.end(); ++it)
  {
    if( !isspace(*it) )
      count++;
  }
  cout << count << endl;
}

I would think by index of a simple array would be just as easy.

OK Guys, the code explicitly says:

cout << "Enter Your Banner Name (Do not use spaces)" << endl;

Note: (Do not use spaces)

The OP said

When I run my program and type in I am Iron man it messes up. It says that I have 13 letters. It counts my spaces as letters. How can i fix this??

You fix it by following the instructions in the code: IamIronman

OK Guys, the code explicitly says:

cout << "Enter Your Banner Name (Do not use spaces)" << endl;

Note: (Do not use spaces)

The OP said

You fix it by following the instructions in the code: IamIronman

I assumed that the "do not use spaces" instruction was put in the code by the OP as a kind of hack, to be used until a solution to the problem of spaces was found.

I assumed that the "do not use spaces" instruction was put in the code by the OP as a kind of hack, to be used until a solution to the problem of spaces was found.

Well, you know what they say about assume :icon_wink:

I've see too many assumptions made on this board that are extremely wrong and 10 posts try to help using that assumption. If the OP isn't clear, he needs to make it clear.

The main idea behind counting words (taking into consideration that they are separated by a ' '(space) or ','(comma) or '.'(Stop)) is by detecting these symbols & increment a counter every time these are encountered.

int counter = 0;
String str;
for(i = 0; i < str.length(); i++)
{
    char ch = str[i];
    if ((ch != ' ') || (ch != ',') || (ch != '.'))
    {
         counter++;
    } 
}
cout<<"The number of letters are : " + counter;

This should pretty much do it :)

commented: See what I mean? There was never any mention of counting words -3

That should do it? I think OP is using native C++. Where is 'i' created anyway?

The main idea behind counting words (taking into consideration that they are separated by a ' '(space) or ','(comma) or '.'(Stop)) is by detecting these symbols & increment a counter every time these are encountered.
This should pretty much do it :)

Although you might be right about counting words -- you have solved the wrong problem. The op is trying to count letters (non-white-space), not words.

Didn't quite get the point.
If you are counting letters in a sentence, all you need to do is pick up each char in the sentence(excluding spaces & punctuations) & increment a counter each time.

as far as the variable i is concerned, I forgot to declare it.
But as far as you understand the logic behind the code, you can always fix small miss like those during coding.

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.