Reputation: 1
#include <iostream>
using namespace std;
int main()
{
string message;
cout << "Ahlan ya user ya habibi." <<endl;
cout <<"what do you like to do today?" <<endl;
cout <<"please enter the message:" <<endl;
getline(cin,message);
for(int i=0;i<message.size();i++)
{
if(string(message[i]==32))
{
cout<<char(message[i]);
}
else if(string( message[i])>=110)
{
int x = int(message[i])-13;
cout<<char(x);
}
else
{
int x = string (message[i])+13;
cout<<char(x);
}
}
return 0;
}
E:\my programe\quiz\main.cpp|20|error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(char&)'|
E:\my programe\quiz\main.cpp|20|error: invalid conversion from 'char' to 'const char*' [-fpermissive]|
E:\my programe\quiz\main.cpp|27|error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(char&)'|
E:\my programe\quiz\main.cpp|27|error: invalid conversion from 'char' to 'const char*' [-fpermissive]|
Upvotes: 0
Views: 62
Reputation: 596111
std::string::operator[]
returns a char&
reference. You are trying to construct temporary std::string
objects with single char
values as input, but std::string
does not have any constructors that take only a single char
as input. That is why you are getting errors.
Even if you could construct a std::string
from a single char
, you can't compare a std::string
to an integer anyway.
You don't need all of those string()
(and char()
) casts at all (BTW, your first string()
cast is malformed anyway). char
is a numeric type. You can compare a char
value directly to an integer, and add/subtract and integer directly to/from a char
value to produce a new char
value.
Try this instead:
#include <iostream>
using namespace std;
int main()
{
string message;
cout << "Ahlan ya user ya habibi." << endl;
cout << "what do you like to do today?" << endl;
cout << "please enter the message:" << endl;
getline(cin, message);
for(int i = 0; i < message.size(); i++)
{
if (message[i] == 32)
{
cout << message[i];
}
else if (message[i] >= 110)
{
char x = message[i] - 13;
cout << x;
}
else
{
char x = message[i] + 13;
cout << x;
}
}
return 0;
}
Upvotes: 1