Vector of strings lab help

Hello, I'm pretty lost on how to approach an assignment for my class. I'm a new CS student and I'm having a hard time understanding the set up. This is it:

Create a program which takes continuous input from the user to generate a case-insensitive list of unique words the user has given, until the number of distinct words reaches 20 (you may use a smaller number while debugging). Specifically, keep a single vector of strings lexicon, and implement the following three functions:

1) searchLexicon : takes a string word and string vector lexicon as arguments, and returns a boolean - true if the word exists in the lexicon, and false otherwise. Note that this is case-insensitive (If the word "yo" were in the lexicon already, checking "Yo" would yield true).
2) addWord : also takes a string word and string vector lexicon as arguments, returns nothing (void). Checks first to see if the word is already in the lexicon (using the previous function), and if it is not, adds the word to the lexicon.
3) printLexicon : outputs each word in the lexicon in all lowercase, separating each with a new line and beginning with some indentation (Quick tip: \t is the tab character).

HINT: to continuously ask for input, a while-loop combined with cin such as the following:
std::string word;
while(cin >> word){ ... }

Will keep asking the user for input until either a direct break statement is issued, or the program is closed forcefully.

Example usage (using a limit of 10 because that's less typing):
$> Hello, please give me a bunch of words to add to the lexicon!
$> How about these words, are THESE words good enough? Of course they are! // input from user
$> Lexicon successfully filled! Check it out:
$> 1 - how
$> 2 - about
$> 3 - these
$> 4 - words,
$> 5 - are
$> 6 - good
$> 7 - enough
$> 8 - of
$> 9 - course
$> 10 - they
$>
$> Good bye!

I can't provide any code that I have written because I don't know where to start. Any advice on how to approach this would be very helpful. Thank you
Last edited on
declare string variable for word and std::vector<std::string> to hold the words
set-up loop (while k < 20) and within this loop …
getline user-input into word through std::cin
lowercase the word throughout using isupper(), tolower() from cctype library – google these functions if required
use std::find (again google) to see if this lowercase word already exists within the vector, if yes then report that to user and don't increment k; if no then push_back word into vector and increment k

throughout google extensively, all constituent parts of your program have extensive solutions on-line
1
2
3
4
5
6
7
8
9
10
11
string searchLexicon(string word, vector<string> lexicon) {
	
	while (cin >> word) {
		cout << "Hello, please give me words to add to the lexicon" << endl;
		cin >> word;
		lexicon.push_back(word);
		for (int i = 0; i < lexicon.size(); i++) {
			tolower(lexicon[i]));
		}
		
	}



This is what I currently have. Can anyone tell me why it is saying "no suitable conversion function from "std::string" to "int" exists? Why would tolower() ever mean that I want to convert it to an int?

Also, do I need to use an if statement inside the for loop to check for ifupper()?
Last edited on
lexicon[i] = tolower(lexicon[i]);
variable lexicon's type is std::vector<std::string>, so lexicon[i] is a std::string and you can't tolower() an entire string but rather individual characters of it:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::string s = "helloWorld";
    for (auto& elem : s)elem = tolower(elem);
    std::cout << s << "\n";
}


Topic archived. No new replies allowed.