My assignment is to develop a C++ program to count the number of capital letters in a given string. String will be entered by user. The idea is to build my knowledge of loops and loop terminations.

I've got the basics down (I think) but I just don't get what functions or commands I need to use to go through the keyboard-input string character by character. We're not supposed to know the isupper function yet, so I'm supposed to use a get. function of some kind.

#include <iostream>
#include <string>
#include <iomanip>

 namespace std;

int main()
{
	char lettr;
	int  charactrCtr = 0;
	int length;
	int count = 0;
	string userInput;

	cout << "Enter a stream of characters, both capital and lower case, then press return..." << endl;
	getline(cin, userInput);
	length = userInput.length();
	cout << length << endl;
	
	while (getline(userInput, 3000).good
	{
		if ((lettr >= 65) && (lettr <= 90));
			charactrCtr = charactrCtr + 1;

		count = count + 1;
	}
	
	cout << "The number of capital letters in the sequence you entered is " << charactrCtr << endl; 

	return 0;
} // end of main

Recommended Answers

All 7 Replies

Why have the while loop at all? You have the length of your input on line 17 and all the loop on line 20 is going to do is ask for more input. Use a for loop and march through the string by index (in string mystr = "abcd"; mystr[0] is 'a',mystr[1] is 'b')

Also to assure portability of your code use 'A' instead of 65 and 'Z' instead of 90, etc.

I am trying to use a "while" loop because that is the chapter we're on.

The input is done by keyboard and the professor has hinted we should use some kind of "while input is good" event test for exiting the loop. We've done that sort of thing for reading from a file, but I don't know exactly how to do it for a cin string.

I thought I could use the string length as the event terminating the loop, something like:

while (count < length)

get next letter, if capital then charactrCtr = charactrCtr +1
++count

I don't know the proper command for the "get next letter" bit, though.

Sorry to be a doofus, but I don't understand exactly how to do this, even with your suggestion of "Use a for loop and march through the string by index (in string mystr = "abcd"; mystr[0] is 'a',mystr[1] is 'b')"

Is it possible to initialize count to 1, then use the count to point to the "next" letter?

The functions isalpha() and isupper() will tell you whether the character is an upper-case character or not

for(int i = 0; i < userInput.length(); i++)
{
    if( isalpha(userInput[i])  && isupper(userInput[i]) )
        count++;
}

I am trying to use a "while" loop because that is the chapter we're on.

The input is done by keyboard and the professor has hinted we should use some kind of "while input is good" event test for exiting the loop. We've done that sort of thing for reading from a file, but I don't know exactly how to do it for a cin string.

But with the std::string you have the information about the length already. You should probably use it, like you tried to in your example


while (count < length)

get next letter, if capital then charactrCtr = charactrCtr +1
++count

I don't know the proper command for the "get next letter" bit, though.

Sorry to be a doofus, but I don't understand exactly how to do this, even with your suggestion of "Use a for loop and march through the string by index (in string mystr = "abcd"; mystr[0] is 'a',mystr[1] is 'b')"

Is it possible to initialize count to 1, then use the count to point to the "next" letter?

Yep. That's what I was trying to say. In your snippet above if count starts at zero then userInput[count] would give you the letter at the "count" position (zero based).
So something like

string userInput = "input";
int count = 0;
while(count < userInput.length())
     cout<<userInput[count]<<" ";

would get you "i n p u t". That's all I meant when I said marching through the string. I happened to suggest the use of a for loop because the length is known but the while is just as effective.

@AD: I think the OP said that isupper (and presumably isalpha) is off limits...

I tried it as you suggested and it does work, but it gives me the total letter count, not the capital letter count.

include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

int main()
{
	char lettr;
	int  charactrCtr = 0;
	int count = 0;
	string userInput;

	cout << "Enter a stream of characters, both capital and lower case, then press return..." << endl;
	cin >> userInput; 
	
	while (count < userInput.length())
	{
		lettr = userInput[(count)];
		if ((lettr >= 65) && (lettr <= 90));
			charactrCtr = charactrCtr + 1;

		count = count + 1;
	}
	
	cout << "The number of capital letters in the sequence you entered is " << charactrCtr << endl; 

	return 0;

What have I messed up?

Take a closer look at line 21. The statement meant to be associated with the "if" was being executed regardless. Can you see why?

Yes, I do see it - no ';' needed at the end of an if statement.
Fixed it, and now it runs correctly.
Thank you!

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.