tags:

views:

195

answers:

4

Using type std::string to accept a sentence, for practice (I haven't worked with strings in C++ much) I'm checking if a character is a vowel or not. I got this:

for(i = 0; i <= analyse.length(); i++) {
if(analyse[i] == 'a' || analyse[i] == 'e' [..etc..]) {
 ...vowels++;    
} else { ...
 ...consonants++;
}

This works fine if the string is all one word, but the second I add a space (IE: aeio aatest) it will only count the first block and count the space as a consonant, and quit reading the sentence (exiting the for loop or something).

Does a space count as no character == null? Or some oddity with std::string?, It would be helpful to know why that is happening!

EDIT: I'm simply accepting the string through std::cin, such as:

std::string analyse = "";
std::cin >> analyse;
+4  A: 

I can't tell from the code that you have pasted, but I'm going to go out on a limb and guess that you're reading into the string using the stream extraction operator (stream >> string).

The stream extraction operator stops when it encounters whitespace.

If this isn't what's going on, can you show us how you're populating your string, and what its contents are?

If I'm right, then you're going to want a different method of reading content into the string. std::getline() is probably the easiest method of reading from a file. It stops at newlines instead of at whitespace.


Edit based on edited question: use this (doublecheck the syntax. I'm not in front of my compiler.):

std::getline(std::cin, analyze); 

This ought to stop reading when you press "enter".

jwismar
+2  A: 

If you want to read in an entire line (including the blanks) then you should read using getline. Schematically it looks like this:

#include <string>
istream& std::getline( istream& is, string& s );

To read the whole line you do something like this:

string s;
getline( cin, s );
cout << "You entered " << s << endl;

PS: the word is "consonant", not "consenent".

Alexandros Gezerlis
getline works fine. Glad it wasn't some random problem, helps me learn.
Nullw0rm
+4  A: 

I'd guess you're reading your string with something like your_stream >> your_string;. Operator >> for strings is defined to work (about) the same as scanf's %s conversion, which reads up until it encounters whitespace -- therefore, operator>> does the same.

You can read an entire line of input instead with std::getline. You might also want to look at an answer I posted to a previous question (provides some alternatives to std::getline).

Jerry Coffin
Thanks for the answer link, +1
Nullw0rm
+1  A: 

The >> operator on an istream separates strings on whitespace. If you want to get a whole line, you can use readline(cin,destination_string).

Ken Bloom
Perhaps you intended `getline` rather than `readline`? While there are libraries that include functions named `readline`, it's not part of the standard library.
Jerry Coffin
Yes, I did intend getline. (And it's other **languages** that use readline, which is why I'm getting confused.)
Ken Bloom