Hello. I am relatively new to C++ programming, and I have a question about a program that I am supposed to write. The assignment reads...

Prompt the user for a word; call it SEARCH. Then begin reading words from the user, counting them until SEARCH is entered. Display how many words were entered until the SEARCH word was found. Run the program first to see the expected output. Use a WHILE loop to do this.

This is what I wrote.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string search = "";

int count = 0;
cout << "Enter a search word: ";
do
{getline (cin, word);
count++;
}
while (search != "");
cout << count << endl;
}

I am not sure why this does not run. If I type in a word E.G. (while (search != "ok") it runs like it is supposed too, but the program the prof. uses randomly generates words, and the output is always 4. I do not see what I am doing wrong here. Any help is appreciated. Thank you!

Recommended Answers

All 4 Replies

If you don't see why the program doesn't work, I suggest that you go through the program a line at a time and explain to us what each line does and why you are confident that it is correct.

If you don't see why the program doesn't work, I suggest that you go through the program a line at a time and explain to us what each line does and why you are confident that it is correct.

{
string search = ""; - Identifies variable

int count = 0; - sets counter
cout << "Enter a search word: "; - output
do - beginning of loop
{getline (cin, word); - user input
count++; - counter
}
while (search != ""); - ends loop 
cout << count << endl; - output
}

I am not sure why it does not work. When I run the program on my compiler it works, but the program my professor uses the program returns 4 every time no matter what the word is. That is why I so not get the issue. If I put "hello" in the while (search != "") statement, and I type "hello" the counter works for me, so I am at a loss.

Your remarks, such as "user input" and "ends loop", do nothing to convince me of anything, for several reasons.

The most important reason is that the program you originally posted does not compile, because it uses a variable named "word" that is not declared anywhere.

But even if it did compile, it could not possibly work, because the loop in the program terminates based on the condition search != "" and the variable "search" is never changed in the program after its initialization near the beginning.

So either the loop always terminates after running once, or it never terminates.

That is enough to convince me that the program cannot be correct.

Now it's your turn. Fix the program, and understand it well enough that you can explain why you are sure that every line in it is correct.

This is what I have come up with. When I compile the program the output is the number of words were typed to read "hello". Is this the proper way to write the program?

#include <iostream>
#include <string>
using namespace std;
int main()
{
string search = "";

int count = 0;
cout << "Enter a search word: ";
do
{getline (cin, search);
count++;
}
while (search != "hello");
search = count;
cout << count << endl;
}

Thank you for your help. It's very much appreciated. I am fairly new at learning programming languages, but am also very interested in them.

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.