#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.
Related
i keep getting this error. i know this is a c++ 11 function but it still isnt working with code blocks c++ compiler. am i using this function correctly of is it a problem with the codeblocks compiler. i tried changing the compiler. using the "have g++ follow the c++11 iso standard" i still keep getting this error. or getting the "stoi() does not exist in the current scope" error
#include <iostream>
#include <string>
using namespace std;
int main()
{
int test = 34;
cout << stoi(test);
}
stoi means "String To Int". It will read an int from a std::string (or std::wstring). See also the reference.
You were probably looking for the reverse std::to_string (reference). But you don't need either, there is no need to convert to string before printing:
#include <iostream>
int main()
{
int test = 34;
std::cout << test;
}
stoi means string to int. So it takes a string as an input.
This should work:
string test = "34"; cout << stoi(test);
Whats the problem with the code below, and why I'm getting this error?
#include <iostream>
#define A 1
using namespace std;
int main()
{
cout <<A++;
return 0;
}
#define A 1 doesn't make a variable called A.
It tells your computer to replace all utterances of A with 1 before compiling.
So, your program is actually:
#include <iostream>
using namespace std;
int main()
{
cout <<1++;
return 0;
}
And you cannot increment the literal 1.
You may read more about preprocessor directives in your C++ book.
#define A 1
A is not valid c++ left value, so gave us "lvalue required as increment operand"
int A;
is valid c++ left value and will work, also of other simple number type float, unsigned char etc
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;
}
I keep getting this error (it's a really long one but I think the most important part is this):
main.cpp:9:30: note: mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>' and 'const char [2]'
While compiling this bit of code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x = getline(cin, " ");
return 0;
}
The lines in the error won't match with the ones in the code I brought up here because I don't know how to make a new line whilst writing code in the Stack Overflow editor; I'm new here ;) Anyways, the error points to the line with the declaration of string x.
Basically what I want this code to do is to get a line from the user until he/she hits space. Maybe I'm doing something wrong from the beginning, so I'm open for suggestions of fixing this problem. (I'm not very experienced in C++, it's just that my teacher required to complete a task using this language.) Thanks,
Anthony
The second parameter of std::getline() is a reference to a std::string variable that accepts the read data. The string is not outputted in the function's return value.
Also, std::getline() does not accept a string for the delimiter. It takes only a single character.
Try this instead:
#include <iostream>
#include <string>
using namespace std;
int main() {
string x;
getline(cin, x, ' ');
return 0;
}
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"