I can't seem to figure out whats wrong. This isn't a finished program but it should compile anyways.

#include <iostream>
#include <string>

using namespace std;

const string SENTINEL = "\n";

int main ()
{  
    char letter;
    int  letterCt;

	cout << "Enter a line of characters" << endl;
   /* TO BE FILLED IN */

	cin >> letter;

	while (letter != SENTINEL)
	{
		cout << letter;
	}

    return 0;
}

If I comment out the while statement it compiles just fine. Otherwise I get 9 errors. Any suggestions?

Recommended Answers

All 7 Replies

I can't seem to figure out whats wrong. This isn't a finished program but it should compile anyways.

#include <iostream>
#include <string>

using namespace std;

const string SENTINEL = "\n";

int main ()
{  
    char letter;
    int  letterCt;

	cout << "Enter a line of characters" << endl;
   /* TO BE FILLED IN */

	cin >> letter;

	while (letter != SENTINEL)
	{
		cout << letter;
	}

    return 0;
}

If I comment out the while statement it compiles just fine. Otherwise I get 9 errors. Any suggestions?

You're comparing characters and strings. You cannot do that. Characters are denoted by single quotes, strings by double quotes. A string's end is denoted by '\n'. Thus I don't know if you can store a string as "\n" (I have never actually tried). Regardless, I don't think you want to define it as a string. Define SENTINEL as a character because '\n' is character and you are comparing a character to it. So try changing this line:

const string SENTINEL = "\n";

to this:

const char SENTINEL = '\n';

Note the single versus double quotes. That should get it to compile, but I doubt it will run correctly. '\n' is white space. The >> operator doesn't read white space so I don't think letter will ever contain '\n' (not positive, but I don't think so). You may want to use get or getline instead of the >> operator. What is the program supposed to do?

http://www.cplusplus.com/reference/iostream/istream/get.html
http://www.cplusplus.com/reference/iostream/istream/getline.html

Well the program counts the number of uppercase letters then i got to print them to the user.

I updated the code.

#include <iostream>
#include <string>

using namespace std;

const char SENTINEL = '\n';

int main ()
{  
    char letter;
    int  letterCt;

	cout << "Enter a line of characters" << endl;
   /* TO BE FILLED IN */

	cin >> letter;

	while (letter != SENTINEL)
	{
		cin.get(letter);
	}

    return 0;
}

I'm thinking I gotta use an isupper(letter) ?
Not sure i'm not very familiar with the <cctype>

You can simplify your loop by reading input in the condition. That way you don't have to prime the variable with cin>>letter before the loop:

while (cin.get(letter) && letter != SENTINEL) {
  if (isupper(letter))
    cout << letter;
}

It's working the way I want it now. I'm just getting the wrong answer.
The first letter in a line is always counted as a lower case.
So "AbbaDabbaDoo" would only return 2.

What does your code look like?

Oh sorry. I found my problem though. I did a cin, then a cin.get. Thats why it skipped the first. I just had to remove the first cin, and move the cin.get into the while (). Gotta love those mistakes that make you go "Duuhhh" *slaps forehead*.

Thanks for your help though.

int main ()
{  
    char letter;
    int  letterCt;

	cout << "Enter a line of characters" << endl;
   /* TO BE FILLED IN */

	cin >> letter;
	
	letterCt = 0;

	while (letter != SENTINEL)
	{
		cin.get(letter);
		if (isupper(letter))
			letterCt++;
		
	}

	cout << letterCt << endl;

    return 0;
}

While I'm pleased that you diligently use code tags, please keep in mind that "icode" and "code" are two different tags that aren't designed for the same purpose. "code" tags should be used for blocks of code that are separate from the text of a post.

Code tags are separate
Usually have multiple lines
                          And preserve indentation

"icode" (aka. inline code) tags are meant for code that's embedded in the text . These are usually parts of a single line of code, and possibly even a full line. Inline code tags do not allow multiple lines, and if you something in icode tags that should be in code tags, you will mess up the formatting, as the lack of indentation for the code in all of your posts for this thread shows.

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.