Constexpr Errors [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Okay I fixed the errors. Thank you guys. But now when I run it, I choose D in the menu but only "You chose to split the words & remove the duplicates in the paragraph" & "This is it:" prints out. It doesn't show anything after that ... Anyone know what it could be? Thank you in advance!!
This is how it should be:
When the 4th choice (“Split Words”) is selected, the words should be put into an array or a structure of your and each word should be displayed with a loop. After this duplicate removal should be performed and the program must determine the duplicate words and eliminate them. After this, the word list should be printed again
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>
#include <sstream>
#include <set>
using namespace std;
int main()
{
string s;
char selection;
string w;
string buf;
cout << "Enter a paragraph or a sentence : " ;
getline(cin, s);
int sizeOfString = s.length();
//cout << "The paragraph has " << sizeOfString << " characters. " << endl; ***Dummy call to see if size works.
//cout << "You entered " << s << endl; *** Dummy function !!
cout << "" << endl;
cout << " Menu " << endl;
cout <<" ------------------------" << endl;
cout << "" << endl;
cout << "A -- Convert paragraph to all caps " << endl;
cout << "B -- Convert paragraph to all lowercase " << endl;
cout << "C -- Delete whitespaces " << endl;
cout << "D -- Split words & remove duplicates " << endl;
cout << "E -- Search a certain word " << endl;
cout << "" << endl;
cout << "Please select one of the above: " ;
cin >> selection;
cout << "" << endl;
stringstream ss(s);
set<string> tokens;
switch (selection) //Switch statement
{
case 'a':
case 'A': cout << "You chose to convert the paragraph to all uppercase" << endl;
cout << "" << endl;
for(int i=0; s[i]!='\0'; i++)
{
s[i]=toupper(s[i]);
}
cout << "This is it: " << s << endl;
break;
case 'b':
case 'B': cout << "You chose to convert the paragragh to all lowercase" << endl;
cout << "" << endl;
for (int i=0; s[i] !='\0'; i++)
{
s[i]=tolower(s[i]);
}
cout << "This is it: " << s << endl;
break;
case 'c':
case 'C': cout << "You chose to delete the whitespaces in the paragraph" << endl;
cout << "" << endl;
for(int i=0; i<s.length(); i++)
if(s[i] == ' ') s.erase(i,1);
cout <<"This is it: " << s << endl;
break;
case 'd':
case 'D': cout << "You chose to split the words & remove the duplicates in the paragraph" << endl;
cout << "" << endl;
// Insert the string into a stream
// Create vector to hold our words
while (ss >> buf)
tokens.insert(buf);
cout << "This is it: " << endl;
for (set<string>::iterator it = tokens.begin(); it != tokens.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
break;
case 'e':
case 'E': cout << "You chose to search for a certain word in the paragraph. " << endl;
cout << "" << endl;
cout << "Enter the word you want to search for: ";
cin >> w;
s.find(w);
if ( s.find( w ) != std::string::npos )
{
cout << w << " was found in the paragraph. " << endl;
}
else
{
cout << w << " was not found in the paragraph. " << endl;
}
}
return 0;
}

1)
set<s>::iterator
should be
set<string>::iterator
2) Add brackets around the case statements for the local variables.
case 'D':{
}
break;
case 'e':
case 'E':{
}
break;

You are creating stringstream with empty string, which, in turn leads to stream being empty. Consider creating stringstream object after you actually read the string. stringstream doesn't hold a reference to your string object, so any modification to a string the stringstream was based on, doesn't reflect those changes in the stream.

Related

strcmp() function not working for the word "university" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
So in this small project for my school I needed to build a word guessing game everything works fine except the string university which always gives output incorrect, I tried changing it to universit and it works but university doesn't work. I cant figure out what the problem is.
Also are there any other alternatives to strcmp()?
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{ // at first we are basically going to give an intro to the gameseco
cout << " HANGMAN : GUESS THE WORD AND NOT DIE!!\n\n\n";
cout << " Welcome to the game player!!\n\n";
cout << " Rules: Guess the word by typing a letter based on given hints.\n";
cout << " Multiple incorrect guesses will HANG YOU.\n\n";
// here we add different words in the game for the guessing system using a 2 dimensional array
char word[20][20] = {
"higher",
"secondary",
"first",
"year",
"cotton",
"university",
"computer",
"science",
"project",
"hangman",
};
char newword[20], guess[10];
bool again = 1;
while (again == 1)
{
again = 0;
srand(time(0));
int x = 0, wrong = 0, z = (rand() % 10) + 0;
strcpy(newword, word[z]); // so we used strcpy command to copy an element of the 2D array of randomly generated index to a new string
cout << "Enter your guess. \nHINT: Word Length(indicated by underscores) = "; // hint no. one the gives us the exact lenth of word
for (x = 0; newword[x] != '\0'; x++)
{
cout << "-";
}
cout << "\nWord starts with: '" << newword[0] << "'"; // hint no. two which gives the player an idea what the word might be
cout << "\n\n";
for (wrong = 1; wrong <= 6; wrong++) // the loop here checks whether the input given by the user is correct or not
{
cin >> guess; // the input is taken here
if (strcmp(guess, newword) == 0) // the input is compared with the word to be guessed
{ // using the strcmp() function from the string.h library
cout << "Correct!\n"; // if the guess is correct the program will print correct and correct and end
break; // correct guess will terminate the loop
}
else if (wrong == 1)
{
cout << "Opps! Wrong guess!\n\n"; // if player inputs wrong word a poll will appear for hanging the person
cout << "I=-=\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 2)
{
cout << "Opps! Wrong guess again!\n\n"; // each wrong word will result in the appearance of of a hanging person
cout << "I=-=\n";
cout << "| |\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 3)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 4)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "I--|--\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 5)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "I--|--\n";
cout << "| o\n";
cout << "I\n";
}
else if (wrong == 6)
{
cout << "Opps! Wrong guess again!\nLast Chance!!\n\n"; // unfortunately the player couldn't guess the word
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n"; // the hanging person compeletely appears and the program ends
cout << "|--|--\n";
cout << "| o\n";
cout << "I | |\n";
cout << "YOU ARE DEAD HAHAHA!!!";
}
}
cout << "Do you want to play again?\nPress 1 for yes, 0 for no."; // here it is asked if we want to play again
cin >> again;
}
return 0;
}
The problem seems to be this declaration:
char guess[10];
Your array declaration should always cater for a \0 character at the end of string. The length of "university" is 10 character, to hold the entire string you required an array of 11 characters.
So please change the length of guess array to be longest string length plus 1.
char guess[11];
As a side note:
Since you are using C++, you should fully utilize the facilities offers by the language. In C++, you should use std::string to store array of characters, use std::vector to store a collection of objects etc.

C++ - Assigning character values to a string in a vector using range based for loop doesn't assign the value

I'm working on a self-imposed practice exercise. The parameters are that I allow the user to enter a name that is stored in a vector. Printing the list of names in the vector gives you the position of each name. You can choose to encrypt a name in the list by providing the name's position. Encryption compares each letter in the name with another string that is the allowed alphabet for names. When it finds the letter in the alphabet, it pulls a corresponding character from another string of random characters and assigns the new character to the same position.
Using a range based for loop I almost got it to work. By adding output statements I can see the code correctly comparing the characters of a name to the allowed alphabet and finding the corresponding value in the encryption key. However when the loop is complete and I print the list of names again, the characters in the name to be encrypted are unchanged.
Trying to troubleshoot the issue, I have commented out the range based for loop and tried to do the same thing with a traditional for loop. With this code I get and error during encryption:
Position 1 A is the same as #
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 26) >= this->size() (which is 2)
The "Position 1 A is the same as #" line is a debug output that I added to show that the code is able to find the correct string, a letter in the string, and the corresponding letter in they key.
Any help in understanding why I get those errors would be appreciated.
Here's my code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
// Declare strings for Encryption and Decryption
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "};
string key {"mnbvfghytcqwi1234567890`~!##$%^&*()-=_+[]\{}|;':,./<>?"};
//Declare collection of names for the list
vector <string> names {};
//Declare character to hold the user menu selection
char selection {};
string user_input{};
string banner (50, '=');
//Print menu
do
{
cout << "\n" << banner << endl;
cout << "A - Add name to list" << endl;
cout << "P - Print all names in list" << endl;
cout << "E - Encrypt a name in the list" << endl;
cout << "D - Decrypt a name in the list" << endl;
cout << "S - Show details of a name in the list" << endl;
cout << "C - Clear all names in the list" << endl;
cout << "Q - Quit" << endl;
cout << banner << endl;
cout << "Selection: ";
getline(cin, user_input);
if (user_input.size() != 1)
{
cout << "Error 4: Menu selection must be a single character" << endl;
selection = '1';
}
else
{
for (auto c: user_input)
{
if (!isalpha(c))
{
cout << "Error 5: Menu selection must be an alphabetic character" << endl;
selection = '1';
}
else
selection = c;
}
}
// cin >> selection;
// cin.clear();
// cin.sync();
switch (selection)
{
case 'a':
case 'A':
{
string temp_name{};
bool invalid_name {false};
cout << "Enter full name: ";
getline(cin, temp_name);
if (!isalpha(temp_name[0]))
cout << "Error 2: Names must begin with an alphabetic character" << endl << endl;
else
{
for (auto c: temp_name)
{
if (!isalpha(c) && !isspace(c) && c != '-')
{
invalid_name = true;
break;
}
else
invalid_name = false;
}
if (invalid_name)
cout << "Error 3: Name contains invalid characters" << endl << endl;
else
{
temp_name.at(0) = toupper (temp_name.at(0));
for (size_t i {1}; i < temp_name.size(); i++)
{
size_t position{i-1};
if (isspace(temp_name.at(position)) || temp_name.at(position) == '-')
{
temp_name.at(i) = toupper(temp_name.at(i));
}
}
names.push_back(temp_name);
cout << "Added name #" << names.size() << endl;
}
}
break;
}
case 'p':
case 'P':
{
for (size_t i {0}; i < names.size(); i++)
cout << i+1 << ". " << names.at(i) << endl;
break;
}
case 'e':
case 'E':
{
size_t encrypt_input{}, key_position{}, name_position {}, name_size {};
cout << "Enter the position of the name to encrypt: ";
cin >> encrypt_input;
cin.clear();
cin.sync();
if (encrypt_input < 1 || encrypt_input > names.size())
cout << "Error 6: Invalid selection for name to encrypt" << endl << endl;
else
{
name_position = encrypt_input - 1;
name_size = names.at(name_position).size();
cout << "Encrypting name: " << names.at(name_position) << " of size " << name_size << endl << endl;
cout << "Position 1 " << names.at(name_position).at(0) << " is the same as ";
key_position = alphabet.find(names.at(name_position).at(0));
cout << key.at(key_position) << endl;
for (size_t i {0}; i < name_size; i++)
{
key_position = alphabet.find(names.at(name_position).at(i));
cout << "Finding " << names.at(key_position).at(i) << " in key at position " << key_position << endl;
cout << "Found encryption value of " << key.at(key_position) << " at position " << key_position << endl;
cout << "Changing " << names.at(key_position).at(i) << " to " << key.at(key_position) << endl;
names.at(name_position).at(i) = key.at(key_position);
}
/*
for (auto c: names.at(encrypt_input-1))
{
cout << "Converting " << c << " to ";
key_position = alphabet.find(c);
cout << key.at(key_position) << endl;
c = key.at(key_position);
cout << "C is now " << c << endl << endl;
}
*/
}
cout << names.at(encrypt_input-1) << endl;
break;
}
case 'q':
case 'Q':
cout << "Goodbye" << endl << endl;
break;
default:
cout << "Error 1: Invalid menu selection" << endl << endl;
break;
}
} while (selection != 'Q' && selection != 'q');
return 0;
}
Welcome to Stackoverflow! I agree entirely with PaulMcKenzie that such a big function is not the best for a variety of reasons - the immediate reasons are that its hard to read and hard to find problems - but there are more reasons as well.
Having said that you have a bug that I can see in the E case.
for (size_t i {0}; i < name_size; i++)
{
key_position = alphabet.find(names.at(name_position).at(i));
cout << "Finding " << names.at(key_position).at(i) << " in key at position " << key_position << endl;
cout << "Found encryption value of " << key.at(key_position) << " at position " << key_position << endl;
cout << "Changing " << names.at(key_position).at(i) << " to " << key.at(key_position) << endl;
names.at(name_position).at(i) = key.at(key_position);
}
Should be
for (unsigned int i{ 0 }; i < name_size; i++)
{
key_position = alphabet.find(names.at(name_position).at(i));
cout << "Finding " << names.at(name_position).at(i) << " in key at position " << key_position << endl;
cout << "Found encryption value of " << key.at(key_position) << " at position " << key_position << endl;
cout << "Changing " << names.at(name_position).at(i) << " to " << key.at(key_position) << endl;
names.at(name_position).at(i) = key.at(key_position);
}
ie key_position should be name_position in 2 places.
There may be other bugs, but this should stop the crashing and do the encoding right.
EDIT: On request of OP have added a new code fragment.
int i = 0; // position counter
for (auto c: names.at(encrypt_input-1))
{
cout << "Converting " << c << " to ";
key_position = alphabet.find(c);
cout << key.at(key_position) << endl;
c = key.at(key_position);
cout << "C is now " << c << endl << endl;
names.at(name_position).at(i++) = c; // update the names variable.
}
This should solve the problem you mentioned for the auto loop.
You're accessing an invalid location of names vector and the error / exception is showing that.
When you do this:
names.at( key_position ).at( i )
// ^^^
// It should be name_position
in this statement,
cout << "Finding " << names.at( key_position ).at( i ) << " in key at position " << key_position << endl;
you're accessing an invalid index of names whereas it should be:
names.at( name_position ).at( i )
and, that'll work because it access a valid index.
You're making the same mistake in this statement as well:
cout << "Changing " << names.at( key_position ).at( i ) << " to " << key.at( key_position ) << endl;
Correct these and it should work!
Tip:
It's time you read How to debug small programs.
It'll help you figure out what's wrong with your program in a more systematic way.
A few points regarding your code organization in general:
You should divide your program in functions instead of cluttering the main function.
You may write functions corresponding to each of your case in switch statement e.g. addName(), encryptName(), decryptName(), etc.
This modularity will definitely help you and other people to read, debug, maintain and extend your code easily and efficiently. In your case, it would also help you write an Minimal, Complete, and Verifiable example in no time.
Hope that helps!
Best of luck!
Happy coding!

How to check If cin buffer contains the same value for 0.5sec (C++) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to make a program so that when I type in 1 it will print go ahead infinite times and like that 2-go left 3-go right. And when I enter 0 it should stop everything and print !!!stop!!!
I have tried many things but I can't even make the 1 command work. Please help.
#include "stdafx.h"
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int value;
cout << "Would you please enter the command " << endl;
cout << " _______________________" << endl;
cout << " 1- go ahead |" << endl;
cout << " _______________________" << endl;
cout << " 2- make a left turn |" << endl;
cout << " _______________________" << endl;
cout << " 3- make a right turn |" << endl;
cout << " _______________________" << endl;
cout << " 4- go back |" << endl;
cout << " _______________________" << endl;
cout << " 0- stop everything |" << endl;
cout << " _____________________________________________________" << endl;
cin >> value;
switch (value)
{
case 1:
while (int value == 1)
{
cout << " * go ahead " << endl;
if (value == 0)
{
break;
}
}
break;
case 2:
cout << " * turn left " << endl;
break;
case 3:
cout << " * turn right " << endl;
break;
case 4:
cout << " * reverse " << endl;
break;
case 0:
cout << " !!!Stop!!! " << endl;
break;
}
return 0;
}
for(value==1) is no valid C++ syntax and even if it would be, your code would print "go ahead" infinitely after 1 has been entered.
Besides that, your switch cases are checking for ASCII 1 instead of numeric 1. Change '1' to 1.
You need to surround your input switch statement with some kind of loop like this:
int value = 0;
do{
cin >> value;
switch(value){
case 1:
cout << "go ahead" << endl;
break;
case 2:
cout << "turn left" << endl;
break;
case 3:
cout << "turn right" << endl;
break;
case 4:
cout << "reverse" << endl;
break;
case 0:
cout << "stop" << endl;
break;
}
}while(value!=0);
This Code will ask for input and print the direction afterwards.
You have three problems. Firstly, '1' is a character literal and 1 is an integer literal (and I know of no implementation where they have the same value). You need to get rid of the quotes.
Secondly, for (int value == 1) is not valid C++. You need to change this to while (value == 1).
Finally, and most significantly, nothing inside the loop will allow value to change. It will be an infinite loop (and hence undefined behaviour). You will need to run the indefinite loop on one thread, and do input in a loop on the other thread.
(Also, please don't use using namespace std;)
remove '' in case statements
switch (value)
{
case 1:
for (value == 1)
{
cout << " * go ahead " << endl;
if (value == 0)
{
break;
}
}
break;
case 2:
cout << " * turn left " << endl;
break;
case 3:
cout << " * turn right " << endl;
break;
case 4:
cout << " * reverse " << endl;
break;
case 0:
cout << " !!!Stop!!! " << endl;
break;
}

How to use an array with a string

I'm creating a program that takes in a sentence or a paragraph. It then asks the user what they'd like to do.
- Covert to all caps
- Covert to all lowercase
- Delete white spaces
- Split words and remove duplicates
- Search for a word in the string
I got the all of them but I can't figure out how to split the words and remove duplicates..
When the 4th choice (“Split Words”) is selected, the words should be put into an array or a structure and each word should be displayed with a loop. After this, duplicate removal should be performed and the program must determine the duplicate words and eliminate them. After this, the word list should be printed again.
Any help with this would be greatly appreciated. Thank you
This is my code & I need help with case 'D'
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
int main() {
string s;
char selection;
string w;
cout << "Enter a paragraph or a sentence : ";
getline(cin, s);
int sizeOfString = s.length();
//cout << "The paragraph has " << sizeOfString << " characters. " << endl; ***Dummy call to see if size works.
//cout << "You entered " << s << endl; *** Dummy function !!
cout << "" << endl;
cout << " Menu " << endl;
cout << " ------------------------" << endl;
cout << "" << endl;
cout << "A -- Convert paragraph to all caps " << endl;
cout << "B -- Convert paragraph to all lowercase " << endl;
cout << "C -- Delete whitespaces " << endl;
cout << "D -- Split words & remove duplicates " << endl;
cout << "E -- Search a certain word " << endl;
cout << "" << endl;
cout << "Please select one of the above: ";
cin >> selection;
cout << "" << endl;
switch (selection) //Switch statement
{
case 'a':
case 'A':
cout << "You chose to convert the paragraph to all uppercase" << endl;
cout << "" << endl;
for (int i = 0; s[i] != '\0'; i++) {
s[i] = toupper(s[i]);
}
cout << "This is it: " << s << endl;
break;
case 'b':
case 'B':
cout << "You chose to convert the paragragh to all lowercase" << endl;
cout << "" << endl;
for (int i = 0; s[i] != '\0'; i++) {
s[i] = tolower(s[i]);
}
cout << "This is it: " << s << endl;
break;
case 'c':
case 'C':
cout << "You chose to delete the whitespaces in the paragraph" << endl;
cout << "" << endl;
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ')
s.erase(i, 1);
}
cout << "This is it: " << s << endl;
break;
case 'd':
case 'D':
cout << "You chose to split the words & remove the duplicates in the paragraph" << endl;
cout << "" << endl;
case 'e':
case 'E':
cout << "You chose to search for a certain word in the paragraph. " << endl;
cout << "" << endl;
cout << "Enter the word you want to search for: ";
cin >> w;
s.find(w);
if (s.find(w) != std::string::npos) {
cout << w << " was found in the paragraph. " << endl;
} else {
cout << w << " was not found in the paragraph. " << endl;
}
}
return 0;
}
You can use stringstream to split your string by whitespaces and set<string> to contain only unique words. Then your code should look like:
case 'D':
{
cout << "You chose to split the words & remove the duplicates in the paragraph" << endl;
string buf;
stringstream ss(s); // Insert the string into a stream
set<string> tokens; // Create vector to hold our words
while (ss >> buf)
tokens.insert(buf);
cout << "This is it: " << endl;
for (set<string>::iterator it = tokens.begin(); it != tokens.end(); ++it) {
cout << *it << " ";
}
cout << endl;
break;
}

Can someone show me an example?

I'm creating a program that takes in a string sentence or a paragraph.
I am not quite familiar with using std::
I'm not asking for someone to do it for me, I am just wanting to see if someone can show me an example of this.
When the 4th choice (“Split Words”) is selected, the words should be put into an array or a structure and each word should be displayed with a loop. After this, duplicate removal should be performed and the program must determine the duplicate words and eliminate them. After this, the word list should be printed again.
Any help with this would be greatly appreciated. Thank you
Code: (I need help with case 'D')
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
int main()
{
string s;
char selection;
string w;
cout << "Enter a paragraph or a sentence : " ;
getline(cin, s);
int sizeOfString = s.length();
//cout << "The paragraph has " << sizeOfString << " characters. " << endl; ***Dummy call to see if size works.
//cout << "You entered " << s << endl; *** Dummy function !!
cout << "" << endl;
cout << " Menu " << endl;
cout <<" ------------------------" << endl;
cout << "" << endl;
cout << "A -- Convert paragraph to all caps " << endl;
cout << "B -- Convert paragraph to all lowercase " << endl;
cout << "C -- Delete whitespaces " << endl;
cout << "D -- Split words & remove duplicates " << endl;
cout << "E -- Search a certain word " << endl;
cout << "" << endl;
cout << "Please select one of the above: " ;
cin >> selection;
cout << "" << endl;
switch (selection) //Switch statement
{
case 'a':
case 'A': cout << "You chose to convert the paragraph to all uppercase" << endl;
cout << "" << endl;
for(int i=0; s[i]!='\0'; i++)
{
s[i]=toupper(s[i]);
}
cout << "This is it: " << s << endl;
break;
case 'b':
case 'B': cout << "You chose to convert the paragragh to all lowercase" << endl;
cout << "" << endl;
for (int i=0; s[i] !='\0'; i++)
{
s[i]=tolower(s[i]);
}
cout << "This is it: " << s << endl;
break;
case 'c':
case 'C': cout << "You chose to delete the whitespaces in the paragraph" << endl;
cout << "" << endl;
for(int i=0; i<s.length(); i++)
if(s[i] == ' ') s.erase(i,1);
cout <<"This is it: " << s << endl;
break;
case 'd':
case 'D': cout << "You chose to split the words & remove the duplicates in the paragraph" << endl;
cout << "" << endl;
case 'e':
case 'E': cout << "You chose to search for a certain word in the paragraph. " << endl;
cout << "" << endl;
cout << "Enter the word you want to search for: ";
cin >> w;
s.find(w);
if ( s.find( w ) != std::string::npos )
{
cout << w << " was found in the paragraph. " << endl;
}
else
{
cout << w << " was not found in the paragraph. " << endl;
}
}
return 0;
}
Write code that parses words out of the string s (Here's one method: Split a string in C++?). Print the words as you parse them and then put them in some sort of set container. A set container does not allow duplicates. Once done parsing words, iterate over the set and print the words out (there won't be duplicates).
If you need to print out the de-duped words in the order they appeared in the string, then you could keep a vector alongside the set. Like before, as you parse the words add them to the set but inspect the returned pair from the set's insert() method (it can tell you whether the inserted item was new to the set or if the insert operation was denied because the set already contained a value that was equal to the one you tried to insert: http://www.cplusplus.com/reference/set/set/insert/).
std:vector<std:string> wordVector; // Assume populated with raw parsed words
std:set<std:string> deDupedSet;
std:vector<std:string> deDupedVector;
for (int i = 0; i < wordVector.size(); i++)
{
if (deDupedSet.insert(wordVector[i]).second())
{
deDupedVector.push_back(wordVector[i]);
{
}
// Print the deDupedVector afterwards