Program help

Hello! I'm very new to C++ and I have a program I need to write that I do not know how to start. The prompt is: "A loop that keeps reading words until it runs out of input, and prints the words that are alphabetically first and last. You can assume that the words are actual words and all in lowercase." This is what I have so far, but I am very confused on how to continue. Thank you!


int main ()
{
while (cin>>word)
{
cout<<"Enter a word"<<".\n";
string word;
cin>>word;
}
}
closed account (LA48b7Xj)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main()
{
	std::vector<std::string> words;

	std::string s;
	while(std::cin >> s)
		words.push_back(s);

	std::sort(words.begin(), words.end());

	for(auto& e : words)
		std::cout << e << '\n';
}


If you are on Windows you can enter ctrl + z to signal end of file which makes the loop break. You could modify this easy to make a special word break the loop also.
Thank you!! But is it possibly to do this program without using vectors and just using loops? I don't have any experience with vectors.
closed account (LA48b7Xj)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <algorithm>
#include <iostream>
#include <string>

int main()
{
	std::string words[1000];
	int top = 0;

	std::string s;
	while(std::cin >> s)
		words[top++] = s;

	std::sort(&words[0], &words[0] + top);

	for(int i=0; i<top; ++i)
		std::cout << words[i] << '\n';
}
A loop that keeps reading words until it runs out of input...


Does the loop read the words from a file?
It's saying that the loop will stop once the user input stops, so whenever the user to done giving input the loop needs to stop. I was trying to use a while statement, but I don't know what the condition would be if the loop stops based on user input.
But is it possibly to do this program without using vectors and just using loops?

Yes. Rather than storing all of the words, you need just three variables, to hold the current word, the lowest word and the highest word (in alphabetical sequence).


I was trying to use a while statement, but I don't know what the condition would be if the loop stops based on user input.

The original code was almost heading in the right direction.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    string word;
    cout << "Enter a word.\n";
    
    while (cin >> word)
    {
        // add the code here
        // to do something with the word
        
        
        cout << "Enter a word.\n";
    }
} 


Note: if you redirect the input from a file, the prompts are not really necessary. But if entering by hand from the keyboard, they probably help. In that case use CTRL-Z and ENTER (windows) or CTRL-D (other os).
Last edited on
So, would I initialize the three values, so:

string current;
string lowest;
string highest;

and then would I use an if statement to compare the words based on which would be current, lowest, and highest based on the new word?
Yes.
Would an if statement work when comparing the words? As in, how would the if statement know if the word was higher or lower to the current word in terms of it being alphabetically?
current word in terms of it being alphabetically

Can you give us some examples?
closed account (LA48b7Xj)
the < (less than), > (greater than) operators work for strings

so

1
2
3
4
if(current < lowest)
    lowest = current;
else if(current > highest)
    highest = current;

Last edited on
closed account (48T7M4Gy)
Use this as a reference for comparing strings and applying to your 'if' idea.

http://www.cplusplus.com/reference/string/string/operators/
If a user would to input the words, "chair, apple, cup, tree" then the program would put the words in alphabetically order, but only print the first and last word on that last. So it would output would be "apple and tree".
closed account (48T7M4Gy)
It's not clear what you mean. You should try it out in your code instead of just thinking about it. Your first step is to write some pseudocode, here's a start:

1. get a word
2. is it higher than the highest word?
2a if it is ...
3. is it lower than the lowest word?
3a if it is ...
4 get another word

Topic archived. No new replies allowed.