Reading String of Varied Sizes // String Manipulation C++ - c++

So I just started learning C++ and My professor briefly went over Address (&) and Dereference (*) Operators. I'm not fluent in C++ but i have been searching around for parts and using common knowledge to combine into this code. It fails to build so Please Help!
Assignment- Write a program that keeps reading in strings of varied sizes. If an input string has length greater than one store it in a vector. When an input string has length one (a single character) you will output the string stored in your vector that has the first letter matching the input character. Keep doing this while you read string "quit".
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string input;
char* output;
vector<string> name;
while (input != "quit") {
cin >> input;
if (input.length == 1) {
for (int i = 0; i < name.size; i++) {
output = &name[i].at(0);
if (input == output) {
cout << name[i];
}
}
}
else {
name.push_back(input);
}
}
//system("pause");
return 0;
}

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string input;
vector<string> name;
cin >> input;
while (input != "quit") {
if (input.length() == 1) {
for (int i = 0; i < name.size(); i++) {
if (input[0] == name[i][0]) {
cout << name[i] <<endl;
}
}
}
else {
name.push_back(input);
}
cin >> input;
}
system("pause");
return 0;
}

Related

C++ Using std::getline in place of cin >>

In a problem were i have to take n number of strings as input and count the ones containing a given substring(case insensitive).
Here's my code:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string>
using namespace std;
int main()
{
std::string str2 = "hello";
std::string str3 = "HELLO";
short int n,count=0,i;
cin>>n;
std::string str1[n];
for(i=0;i<n;i++)
{
std::getline(std::cin,str1[i]); //here getline is taking only n-1 inputs
std::size_t found1=str1[i].find(str2);
std::size_t found2=str1[i].find(str3);
if(found1!=std::string::npos || found2!=std::string::npos)
count++;
}
cout<<count;
return 0;
}
Since i cant use cin as string includes spaces or cin.getline() as have to use string type in place of char[].
Problem with my code is std::getline() is only taking n-1 inputs.Cant figure out why?
The first getline after cin reads the remainder of the line, which is probably empty. This is why when reading user input, it is usually better to use getline and process input using code.
After cin >> n the input stream is positioned just after the number n. You can use a getline just to read the newline and then throw it away to position to the start of the next line.
This code should work
#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 0, count = 0;
cin >> n;
do {
string str;
getline(cin, str);
if (str.find("HELLO") != string::npos || str.find("hello") != string::npos) {
count++;
}
} while (n-- && n >= 0);
cout << count << endl;
return 0;
}

C++ How to cin a string into a vector and then display the inputs

Im a little stuck on trying to figure out how to let the user enter in several strings and then display the strings when they enter this "*". Any help is appreciated! Thanks!
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string input;
cout<<"Enter in your shopping list. Enter in * to indicate you are done"<<endl;
vector<string> shoppingList();
while(cin>>input && input != *)
{
shoppingList.push_back(input);
}
if(cin>>input == *)
{
write_vector(shoppingList);
}
return 0;
}
There are two things wrong in this:-
vector<string> shoppingList(); //This would be treated as function declaration...
This should be
vector<string> shoppingList;
And then
if(cin>>input == *)
You should take the input in some string and then compare it with "*"
I think you are looking for this answer.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string input;
cout<<"Enter in your shopping list. Enter in * to indicate you are done"<<endl;
vector<string> shoppingList;
while(input != "*")
{
cin>>input;
shoppingList.push_back(input);
}
if(input == "*")
{
for(int i = 0 ; i<(shoppingList.size() -1);i++)
cout<<shoppingList[i]<<" " ;
}
return 0;
}

Presentation Error:Word Reversal

I have problem with this question I don't know what is wrong with my code that I get Presentation Error every time I don't know what is the format of output can you help me to solve this question I am sorry that my code is a little confusing
here is the link of question http://sharecode.ir/section/problemset/problem/1208
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int main()
{
string temp=" ";
bool cheak3=false,cheak4=false;
int n,num;
cin>>n;
while(n != 0)
{
if(cheak4 == true)
cout<<endl;
cheak4=true;
cin>>num;
cheak3=false;
string cheak1,cheak;
while(1)
{
if(num ==-1)
break;
getline(cin,temp);
for(int i=0 ; i<temp.size() ; i++)
{
if(temp[i] != ' ')
cheak.push_back(temp[i]);
else
{
reverse(cheak.begin(),cheak.end());
cheak1+=cheak;
cheak.clear();
if(cheak3 == true)
cheak1.push_back(' ');
}
}
reverse(cheak.begin(),cheak.end());
cheak1+=cheak;
cheak.clear();
num--;
if(cheak3 == true)
{
cheak1.push_back(' ');
cout<<cheak1<<endl;
cheak1.clear();
}
cheak3=true;
}
n--;
}
}
I believe the tricky part is you should print a blank line between the output blocks.
Your code has too complicated logic! Here is my solution to this problem:
#include <iostream>
#include <string>
using namespace std;
int main() {
int N, lines;
string word;
cin>>N;
while (N--) {
cin>>lines;
while (lines--) {
char end;
do {
cin >> word;
end = cin.get();
for (int i=word.length()-1;i>=0;i--) cout<<word[i];
cout << end;
} while (end != '\n');
}
if (N) cout << endl;
}
return 0;
}
The line if (N) cout << endl; makes sure you print a newline character for every output block except the last one (when N equals to 0).
After reading each word in a line, you can use cin.get(); in order to determine the next character. If it is a space, then print it and read the next word. Else if it is a \n print it and go to next line! :)

c++ find repeated substring within string

Im trying to find the number of times a substring repeats within a string input but for some reason when I call the function it gives me a weird number. I have already tested the function within main and it works fine but when I make a standalone function it doesn't work.
Thank you in advance
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int checkHope(string word1)
{
int answer;
int counter;
for(int i = 0; word1[i] != '\0'; i++)
{
answer = word1.find("h", i);
if ((word1.find("o", (answer+1)) == i+1) && (word1.find("e", (answer+3)) == i+3)) counter++;
}
return counter;
}
int main()
{
string word1;
cout << "Please enter a word to check how many times the word \"hope\" appears. You can also have any letter instead of p.: ";
getline(cin, word1);
cout << checkHope(word1);
return 0;
}
//This will work,
//Changes initialize counter to 0, add condition to check alphabet 'p' also in if condition
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int checkHope(string word1)
{
int answer;
int counter=0;
for(int i = 0; word1[i] != '\0'; i++)
{
answer = word1.find("h", i);
if ((word1.find("o", (answer+1)) == i+1)
&& (word1.find("p", (answer+2)) == i+2)
&& (word1.find("e", (answer+3)) == i+3))
counter++;
}
return counter;
}
int main()
{
string word1;
cout << "Please enter a word to check how many times the word \"hope\" appears. You can also have any letter instead of p.: ";
getline(cin, word1);
cout << checkHope(word1);
return 0;
}

Using the getword function c++

Am I using the getword function wrong here? The compiler keeps telling me that there is no member function.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;
int OccuranceOfString(ofstream & Out)
{
string Occur;
string Temp;
int OccurLength;
int count;
cout << "please enter to string to search for";
cout << endl;
cin >> Occur;
OccurLength = Occur.length();
while(Out.getword(Temp))
{
if (Temp == Occur)
{
count ++;
}
}
return count;
}
Whats wrong with my code? I'm trying to find all occurances of a string with this function
std::ofstream has no getword function: see here.
Perhaps you're thinking of std::getline.
There is no function getword in the header files listed. You simply must construct a function that will extract words from a line. capture a line by
getline(out,line);
line will have your line of string and use line[index] to get continuous characters to be equal to a word.
You can use this
std::string::find
do something like this..
int pos = 0;
int occurrences = 0
string input = "YAaaaAH";
string find = "a";
while(pos != -1){
pos = input.find(find,pos);
occurrences++;
}
text file :
code :
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream file("DB_name_age.txt");
int index;
string name;
int age;
if(file.is_open())
{
while(file >>index >> name >>age)
{
cout << index <<" "<<name <<" "<<age << endl;
}
}else{
cout<< "file open fail" <<endl;
}
return 0;
}
visual explanation: