Check word function - c++

i 've started learn c++ 1 week ago,i need an advice about how i can check entered word without function check_pass.How can i use if or while function for it please help.(Sorry for some mistakes )
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int enter_word;
cout<<"Hey bro what is your favourite color? ";
cin>>enter_word;
cout<<"So what is my favourite color? ";
if (enter_word="yellow"){ cout<<"Yep you are right bro!";}
system("pause");
return 0;
}

There are two major mistakes in the code you show: First is that enter_word is not a std::string object, it's an integer variable so can only contain integers. Secondly, you don't compare enter_word to "yellow" in the condition, you assign to the variable.
The first problem is solved by including <string> and declare enter_word as a string:
std::string enter_word;
The second problem can be solved by using the equality comparison operator == instead of assignment:
enter_word == "yellow"

Related

Reversing a string with C++

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s="PIZZA";
int le=s.length();
for(int i=le-1;i=0;i--){
cout<<s[i];
}
}
What is the error here? I am not getting any output.
You 'd mean i >= 0 in the for loop.
Otherwise you never enter it. i = 0 results to 0 which results to false.
Please do learn how to use the debugger, you will solve most of your problems with it. Unrelated: Don't use using namespace std globally, avoid reverse-iterating for loops.
the condition of the loop is wrong,we want loop from last index of string into first the whole range so you should use i>=0 instead of i=0
worked code :
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str = "PIZZA";
for(int i=str.length()-1;i>=0;i--)
cout<<str[i];
}
but you'd better know that with this code we only print string from end not Reverse IT !! to reverse we use this code :
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str = "PIZZA";
string rev="";
for(int i=str.length()-1;i>=0;i--)
rev+=str[i];
cout<<"Reverse = "<<rev;
}
concat items from last of the string into new one !
You would have gotten your answer without asking us if you had turned on compiler warnings:
Why should I always enable compiler warnings?
That would have given you:
<source>: In function 'int main()':
<source>:9:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
9 | for(int i=le-1;i=0;i--){
| ~^~
Compiler returned: 0
So the compiler is saying: "You are performing an assignment, then using the result as a boolean value/condition. Are you sure that's what you want to do?"
Now, other answers told you what to replace i=0 with (it's i>=0, or possibly i != -1). But - with the warning above, I'm pretty sure you could have reached this conclusion yourself.

Creating a cypher with a single replace function

Currently going thru a c++ course.
I had to create a word cipher using the strings: alphabet and key.
to cipher an inputted word with less code as possible I created this solution that gives the error:
no matching function for call to std::basic_string<char>::find(std::string&, int&, int)
I don't know how to solve it, neither do I know if my idea would work at all, would LOVE some help.
Thanks for your attention :)
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string word_to_encrypt {};
getline (cin,word_to_encrypt);
for (int i=0;i<word_to_encrypt.size;i++){
word_to_encrypt.replace (i,1,key,(alphabet.find(word_to_encrypt,i,1)),1);
}
cout<< word_to_encrypt;
}
Two problems:
First size is a function and not a variable. Therefore you need size().
Secondly std::string::find() has no overload which takes a std::string and two ints: https://en.cppreference.com/w/cpp/string/basic_string/find , but you can use the overload which takes a CharT instead by adding .c_str() or .data().
This compiles at least:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string word_to_encrypt {};
getline (cin,word_to_encrypt);
for (int i=0;i<word_to_encrypt.size();i++){
word_to_encrypt.replace(i, 1, key, (
alphabet.find(word_to_encrypt.c_str(), i, 1)),1);
}
cout<< word_to_encrypt;
}

C++ print palindromes from a text file

Check if a string is palindrome
I was using the link above to try to solve this problem (among many others, Ive been trying to solve it various ways all day with no dice, this is my first C++). All other examples are usually in an array format, and I can't make assumptions as to the length of a word.
I'm trying to make a program to detect if a word is a palindrome or not. I have a text file with one word per line, and want to test each word, line by line, if it is a palindrome, and if so to print it to the screen, and if not, to ignore it and not print it.
I figured the best way to locate the palindromes was to reverse the word line by line and match it to the original, and if they are the same (==), then to print it. Here is what I have so far:
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
std::string line;
std::ifstream infile("wordlist.txt");
}
string reverse(string line){
if (line == string(line.rbegin(), line.rend())) {
cout << string;
}
}
All help is appreciated
I guess your question is a homework question and you would like to get some information on how to complete the C++ coding.
You look not to know how to read file contents in C++.
Here's a link of how to do it:
Read file-contents into a string in C++
I am not very sure about what you specifically would like to be answered. If your question is a homework question, here's some info of how to ask:
https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions
#include<iostream>
#include<algorithm>
#include<string.h>
#include<fstream>
using namespace std;
int main() {
string line="", line_rev="";
ifstream infile;
infile.open("wordlist.txt");
do{
infile>>line;
line_rev=line;
reverse(line_rev.begin(), line_rev.end());
if(line==line_rev)
cout<<line<<endl;
}while(getline(infile, line));
//if(infile.is_open()){cout<<"open"<<endl;} //to check if file is open or not
//else{cout<<"unable to open"<<endl;}
return 0;
}
This is the solution. i dont know why you are writing "string reverse(string line)" out side the main() function.

substr() method in C++

I am trying to substring some expressions into individual tokens such as !, &, | (), etc. What I am having trouble with is the fact that when I try to make a sub-string of "!(S&B|H)&!(S&J|R)&!(P)" with the cout line below, I get: "(S&J|R)&!(P)", when I thought it should be: "(S&J|R)". It either is beyond what I have seen or just so simple that I just am not getting it. Any help would help a lot. Thanks.
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[]) {
string name = "!(S&B|H)&!(S&J|R)&!(P)";
cout<<name.substr(10,16)<<endl;
return 0;
}//Main
I did not understand your question well but if you want to get
(S&J|R)
You should do:
name.substr(10,7)
The second parameter is the length.

C++ STL remove error

I'm having trouble understanding where I went wrong with my code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string str = "";
cin >> str;
remove(str.begin(), str.end(), ' ');
cout << str;
cin.ignore();
}
The error says "'remove': function does not take 3 arguments (C2660)"
Try adding
#include <algorithm>
"algorithm" is an STL header containing a lot of functions, including std::remove, which the OP is trying to call. The error he got was because there is another function that takes a single argument, called "remove", which deletes a file.