The following code is suppose to grab the first word of a sentence (if there's one) and print it letter by letter (The printing part works). The problem that I've encountered is that whenever I input a sentence in the prompt, it grabs every word in the sentence and not the first word (cin is suppose to stop at the first space,enter, etc... so I think the part that is wrong here is the while loop). How can I fix the issue? I think there's something I'm not understanding.
#include <iostream>
using namespace std;
int main(void){
int i = 0;
int length= 25;
string word[length];
cout << "Type another word: (Press Ctrl + D to quit):";
while( i < length && cin >> word[i]){
i++;
cout << "Type another word: (Press Ctrl + D to quit):";
}
for(int j = 0; j < i; j++){
cout<< endl << word[j]<< endl;
for(int k = 0; k < word[j].length(); k++)
cout << word[j].at(k) << endl;
}
}
As we can see this grabs the entire sentence and because of this it prints another prompt not needed.
EXAMPLE OF OUTPUT:
Type another word: (Press Ctrl + D to quit):testing this
Type another word: (Press Ctrl + D to quit):Type another word: (Press Ctrl + D to quit):december
Type another word: (Press Ctrl + D to quit):
testing
t
e
s
t
i
n
g
this
t
h
i
s
december
d
e
c
e
m
b
e
r
I will attach an 'ideal' output:
Type another word: (Press Ctrl + D to quit):testing this
Type another word: (Press Ctrl + D to quit):december
Type another word: (Press Ctrl + D to quit):
testing
t
e
s
t
i
n
g
december
d
e
c
e
m
b
e
r
When you wrote cin is suppose to stop at the first space,enter, etc... so I think the part that is wrong here is the while loop you almost nailed it. cin >> words[i]
reads one word into the array. The loop body increments i
and then goes around to read again. Word by word. Not the next sentence. The next word. Operator >>
doesn't understand sentences. Just words.
Instead use std::getline
to read the whole line in, then get the first word from that line. Easiest way to do that is probably to turn the line into a std::istringstream
and use operator >>
to extract a single word.
For example:
#include <iostream>
#include <string> // was missing.
#include <sstream> // defines std::istringstream
using namespace std;
int main(){ // no need for void. Compiler's smart enough to figure
// that out from empty braces.
int i = 0;
const int length= 25; // needs to be constant to be used to size
// an array
string word[length];
cout << "Type another word: (Press Ctrl + D to quit):";
std::string line; // storage for the line
while( i < length && std::getline(cin, line)){ //read whole line
std::istringstream strm(line); // make a stream out of the line
if (strm >> word[i]) // read first word from line
{
i++;
}
else // tell user they made a mistake or do something fancy.
{
std::cout << "No word on that line.\n";
}
cout << "Type another word: (Press Ctrl + D to quit):";
}
for(int j = 0; j < i; j++){
cout<< endl << word[j]<< endl;
for(int k = 0; k < word[j].length(); k++)
cout << word[j].at(k) << endl;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.