Originally Posted by
jimblumberg
Look at this snippet:
Code:
do {
cin.getline(temp_input_line,79);
x=0; // Set beginning array count to 0
while (temp_input_line[x] != 13){ //Check for Return
When will that while() statement ever end? Answer never.
Jim
So the the only way to make this work is to include an && as below (which violates the unwritten rule [using only what you've learned up till now]) .
But another problem is it only stops when 'done' is the last word. It won't quit while 'done' is inside the sentence.
Code:
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int x,y = 0,done = 1;
unsigned long z = 0;
int Numwords = 0;
int set = 0;
char temp_input_line[80];
char current_word [80] = "";
do {
cin.getline(temp_input_line,79);
x=0; // Set beginning array count to 0
z = strlen(temp_input_line);
while (x <= z ){ //Run only as long as the string length
while (temp_input_line[x] != ' ' && x <= z){ //Check for Space and less than strlen
current_word [y] = temp_input_line [x];
x++;
y++;
set = 1; //Set Flag that word started
}
x++;
//End check for space
Numwords += set; //Increase word count
set = 0; //Reset word count flag
current_word [y + 1] = '\0';
y = 0;
done = strcmp("done",current_word); //If 'done' found, result is 0
}
}while (done);
cout << "Number of words is : " << Numwords << endl;
return 0;
}
Output:
eat me eat me
eat me eat me
eat me done eat me
done
Number of words is : 14