Here is my Code. I am posting below the expected output and my output. Can you t
ID: 3847622 • Letter: H
Question
Here is my Code. I am posting below the expected output and my output. Can you tell me what I am missing?
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
Explanation / Answer
The issue with your program was the way you were taking the input ,cin>> input;
then cin >> reads only a single word of your input stream so we use getline(cin,input) as it reads a whole line from input stream
In other words to read space separated words you can use getline() function
CORRECTED CODE:-
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
//method to split string into words
vector<string> split(string str, char delimiter) {
vector<string> internal;
stringstream ss(str); // Turn the string into a stream.
string token;
while(getline(ss, token, delimiter)) {
internal.push_back(token);
}
return internal;
}
int main(){
//declaring variables
string input;
cout<<"Enter text: ";
getline(cin,input); //Corrected code
//prinitng entered text
cout<<" You entered: "<<input;
cout<<" Expanded: ";
//calling split function
vector<string> res = split(input, ' ');
for(int i=0;i<res.size();i++){
if(res[i] == "BFF"){
cout<<"Best friend forever ";
}
else if(res[i] == "IDK"){
cout<<"I don't know ";
}
else if(res[i] == "JK" || res[i] == "JK,"){ // It depends on the way in which you are tokenizing the word , see here its done JK, is also acceptable
cout<<"just kidding ";
}
else if(res[i] == "TMI"){
cout<<"too much information ";
}
else if(res[i] == "TTYL"){
cout<<"talk to you later ";
}
else{
cout<<res[i]<<" ";
}
}
return 0;
}
PLEASE RATE!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.