Okay, I asked this over in C++ forum, but I think it's just too much C for them there.
Maybe you guys can answer better.
In C++ Primer Plus, Chapter 5 question 8 it asks to:
Write a program that uses an array of char and a loop to read one word at a time
until the word ‘done’ is entered. The program should then report the number of
words entered (not counting done). A sample run could look like this:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
You should include the cstring header file and use the strcmp() function to
make the comparison test.
I can get it, but it's cheating basically because I'm using && and that hasn't been covered yet. Neither has if/else.
So basically only do-while, while, and for statements have been covered.
So is the solution to this problem doable given the limited tools?
working code:
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 ){ //While less than or equal to string lenght
while (temp_input_line[x] != ' ' && x <= z){ //Check for Space or end of string
current_word [y] = temp_input_line [x];
x++;
y++;
set = done; //Set Flag that word started
}
x++;
//End check for space or string end
Numwords += set; //Increase word count
set = 0; //Reset word count flag
current_word [y + 1] = '\0';
y = 0;
done = done && (strcmp("done",current_word)); //If 'done' found, result is 0
} //End check less than string length
}while (done);
cout << "Number of words is : " << Numwords - 1 << endl;
return 0;
}
Output:
one two three four
five six done eight nine ten
Number of words is : 6