I wanted to write a program to check if string entered is a palidrome. I wrote a program but i get errors while compiling. Can anyone help me identify errors in the program.

#include<iostream>
#include<string>
int main(){
string input;bool flag=false;
		cout<<"Enter String: ";
		cin>>input;
		cout<<"You Entered "<<input;
		int length= input.size();
		for (int j=0; j<=length;j++){
			if ((input.at(j))==(input.at(length))){ 
length--;flag=true;
if (flag==false) break;
}
else {flag=false;break;}
		}
		if (flag==false) cout<<"Not A Palindrome";
		else cout<<"Palindrome";
return 0;

I wanted to write a program to check if string entered is a palidrome. I wrote a program but i get errors while compiling. Can anyone help me identify errors in the program.

Are we supposed to guess what the errors are? Or would you like to tell us?

Are we supposed to guess what the errors are? Or would you like to tell us?

I want you tell what is wrong in my code? Thank you!

WaltP is suggesting that you mention errors but do not provide them. This requires us to now copy, paste and compile your code - likely on a different platform and with a different compiler than your own. If you copied the errors you were getting it would be helpful.
Also, instead of using indexes (or .at ) you may want to look into using iterators or something like std::reverse to make your code less complex.

Edited 4 Years Ago by L7Sqr: n/a

WaltP is suggesting that you mention errors but do not provide them. This requires us to now copy, paste and compile your code - likely on a different platform and with a different compiler than your own. If you copied the errors you were getting it would be helpful.
Also, instead of using indexes (or .at ) you may want to look into using iterators or something like std::reverse to make your code less complex.

First-chance exception at 0x765cfc56 in MyProject.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0031f7f4..

Unhandled exception at 0x765cfc56 in MyProject.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0031f7f4..

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.


Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

Look here for an explanation of std::string.at() .
The problem you are having is that input.at(length) is an invalid location. You can only read up to input.at(input.size() - 1) - otherwise you will generate an exception.

Look here for an explanation of std::string.at() .
The problem you are having is that input.at(length) is an invalid location. You can only read up to input.at(input.size() - 1) - otherwise you will generate an exception.

Thank you so much @L7Sqr! NOw my program is running properly.

This question has already been answered. Start a new discussion instead.