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;
}
Related
I need to make a program in witch I have to read a text from an input file(ifstream fin("input.in")) and store it until the program meets the "#" character. I know it should be doable using fin.getline, but I can't make the "delim" parameter work. I would find useful an explanation of how doe it work, and an example. I already read this, but I couldn't find an example with fin.getline.
This is what I tried, but it doesn't work:
#include <fstream>
#include <string.h>
#include <string>
using namespace std;
ifstream fin("cod.in");
ofstream fout("cod.out");
char chr[100];
for(int i=0;i<n;i++)
{
fin.getline(chr,'#');
fout<<chr<<" ";
}
I've just now come across an error using arrays that seems odd, I've searched the web but it would appear this is typically something people run into when they are dealing with multidimensional arrays. The error occurs when I attempt to call a function that uses an array as a parameter. Here is the code:
#include <iostream>
#include <string>
#include <iomanip>
#include "header.h"
#include <fstream>
using namespace std;
int main(){
string make, model, licensePlate, address, name, phoneNumber;
int year, messageCode;
char choice;
bool ready[10];
string phoneNumber[10], name[10], address[10], licensePlate[10], make[10], model[10];
int year[10];
initializeArray(year[]);
The error occurs within the brackets of year[], expected an expression. Thanks in advance!
I apologize for my poor code!
Having both variables of int and an array with the same name was a terrible idea, as you do not use the brackets to pass an array. I removed the brackets and the int year variable.
Here is the revised work:
bool ready[10];
string phoneNumber[10], name[10], address[10], licensePlate[10], make[10], model[10];
int year[10];
initializeArray(year);
I am confused about the use of #include <string> at the start of a program. For example, in the code below, I don't use #include <string> but the function will still print out the string "Johnny's favorite number is" when it is run.
#include <iostream>
using namespace std;
void printVariable(int number){
cout << "Johnny's favorite number is" << number << endl
}
However, in this code below, it does contain #include <string>.
#include <iostream>
#include <string>
using namespace std;
class Var{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main(){
Var Classy;
Classy.setName("Johnny Bravo");
cout << Classy.getName() << endl;
return 0;
}
Do I only use #include <string> if a variable represents a string?
Do I only use #include <string> if a variable represents a string?
Yes.
Use #include <string> when you use a variable that has type std::string.
The code "text here", contrary to intuition, is not a std::string; it is a string literal, and a C-style string, and a const char[10] convertible to const char*. Welcome to C++ with its legacy oddities.
Your question arises from the fact that you know that something like "aabcd" is a string literal. So, its type should be string. Well, that's not quite true.
C++ has a lot of features from C. Including data types. So, that is a pointer to char (char*), not a string(an instance of the string class). You can create an instance of the string class from a char* (including a string literal) by passing it as argument to the constructor of string. But it is not a string, it's just some misleading terminology.
A similar case is calling things vectors when they are arrays.
If you use the type std::string in your code then you should include the <string> header. There are also a few other types and functions in that header, but std::string is the most commonly used one.
However, you do not need to include this header just to use string literals, which are built into the core language.
In your first case, library "string" is not needed. The object "cout" is supported by library "iostream", thus you have:
#include <iostream>
For the second case, you do explicitly use "string", thus library "string" is required:
#include <string>
I am experimenting with CGI in C++. I know that there are libraries which handle basic stuff, but in order to know whats going on under the hood i have been trying to parse the stdin using string datatype ---> tokenize using '= and &' then push_back into a vector. at the latter step, i am receiving segmentation fault. given below is the program where i am using cin>> to obtain user input and so on ..
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
vector <string> stuff(0);
vector<string>::iterator it;
char* bufferchar;
string buffer;
char str[size];
cout<<"Content-type: text/html\n\n"
<<"<html>"
<<"<head>"
<<"<title>CGI SCRIPT</title>"
<<"</head>"
<<"<body>"
fgets(str,20,stdin); //20 is expect size of content from html form
puts(str);
cout<<"<p>Now to break this guy apart";
int x=0;
bufferchar = strtok(str,"&=");
buffer+=bufferchar;
stuff.push_back(buffer);
while(bufferchar!=NULL){
cout<<bufferchar<<'\n'<<'\n';
bufferchar=strtok(NULL,"&=");
buffer+=bufferchar;
stuff.push_back(buffer);
}
cout<<"<br>ok does the vector iterate ?";
for (it=stuff.begin();it!=stuff.end();++it){
cout<<*it;
cout<<"<br> ok man, next <br><br>";
}
cout<<"</body>";
cout<<"</html>";
}
I want to replace all the occurances of ' in a string to ^, but i saw string.replace is not the right function for me, do I need to write my own? It's boring.
You can use std::replace from <algorithm> instead of using string::replace from <string>
Sample code
#include <iostream>
#include <algorithm>
int main()
{
std::string s = "I am a string";
std::replace(s.begin(),s.end(),' ',',');
std::cout<< s;
}
Output : I,am,a,string