Getting strange Error in my Palindrome Program

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2281 Views - Last Post: 25 October 2010 - 01:30 AM Rate Topic: -----

#1 Slims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 23-October 10

Getting strange Error in my Palindrome Program

Posted 23 October 2010 - 05:14 PM

Alright so for fun I thought I'd try to make a program to check if a word/group of numbers is a palindrome and I thought I had figured it out but I'm getting a strange error.

The error is as follows:

Debug Assertion Failed!

blah blah

Expression: string subscript out of range

blah blah..

-----

Here's my code:

http://pastebin.com/wVAb05jH

By the way, this is my first post here, but I've been lurking here for awhile getting help for many different programming problems. So thanks to anyone who has helped me through programming stuff the past couple months :)

P.S. I'm determined to figure out how to do this on my own, so don't necessarily give me the answer as to how to write code to determine if something is a palindrome. Thanks.

EDIT: Used pastebin instead because of readability issues.

EDIT2: Also, no matter how much I read I don't understand the issues with using getline() and why you have to use cin.ignore after it. So if someone could explain that as well, that'd be great. Thanks.

This post has been edited by Slims: 23 October 2010 - 05:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Getting strange Error in my Palindrome Program

#2 Ollie9  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 91
  • Joined: 10-May 10

Re: Getting strange Error in my Palindrome Program

Posted 23 October 2010 - 05:22 PM

problem here

		for (int update = string_length - 1; update >= 0; update--)//The update variable will be equal to the string length
		{														   //  and count down.  This will be used to determine the position
			test_string[temp_str_number] = word_init[update];      //  in the array set in the loop.
			temp_str_number++;
		}

Was This Post Helpful? 0
  • +
  • -

#3 Slims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 23-October 10

Re: Getting strange Error in my Palindrome Program

Posted 23 October 2010 - 05:23 PM

Yeah, I know it's in that spot, but not sure why there's a problem. I don't get why the array subscript is out of range.
Was This Post Helpful? 0
  • +
  • -

#4 Ollie9  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 91
  • Joined: 10-May 10

Re: Getting strange Error in my Palindrome Program

Posted 23 October 2010 - 05:27 PM

The index of the string is out of range

test_string[temp_str_number] = word_init[update];

This post has been edited by Ollie9: 23 October 2010 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 Slims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 23-October 10

Re: Getting strange Error in my Palindrome Program

Posted 23 October 2010 - 05:46 PM

View PostOllie9, on 23 October 2010 - 04:27 PM, said:

The index of the string is out of range

test_string[temp_str_number] = word_init[update];


Alright so let me walk through this to see if I'm understanding properly.

Let's say my word is bib.

word_init = bib
string_length = 3
update = 2

Lets say we are just starting the for loop, so temp_str_number is 0 and update is 2.

So:

test_string[0] = word_init[2]

word_init[2] = b, so we are stuffing 'b' in the first array position of string test_string.

Where is my understanding off?
Was This Post Helpful? 0
  • +
  • -

#6 Ollie9  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 91
  • Joined: 10-May 10

Re: Getting strange Error in my Palindrome Program

Posted 23 October 2010 - 06:00 PM

Ahh i think i found the problem

This only accepts 21 characters, if you input more than 21 it will crash
std::cin.ignore(1024, '\n');

This post has been edited by Ollie9: 23 October 2010 - 06:02 PM

Was This Post Helpful? 0
  • +
  • -

#7 Slims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 23-October 10

Re: Getting strange Error in my Palindrome Program

Posted 23 October 2010 - 06:07 PM

View PostOllie9, on 23 October 2010 - 05:00 PM, said:

Ahh i think i found the problem

This only accepts 21 characters, if you input more than 21 it will crash
std::cin.ignore(1024, '\n');


I took that line out and still had the same problem. I've been testing the program using short words anyway, nothing more than 5 or 6 characters.
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,664
  • Joined: 19-February 09

Re: Getting strange Error in my Palindrome Program

Posted 23 October 2010 - 06:11 PM

The string test_string is an empty string. So test_string[0], the first position, does not exist.
Was This Post Helpful? 1
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1392
  • View blog
  • Posts: 4,396
  • Joined: 20-March 10

Re: Getting strange Error in my Palindrome Program

Posted 23 October 2010 - 06:13 PM

Line 37

palindrome == true;



makes no sense ...... think equals
Was This Post Helpful? 1
  • +
  • -

#10 Slims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 23-October 10

Re: Getting strange Error in my Palindrome Program

Posted 23 October 2010 - 06:17 PM

Brilliant.

Thanks for the help guys. I see some other problems now but I think I can handle it from here. If I have more questions I'll come back though ;p

This post has been edited by Slims: 23 October 2010 - 06:18 PM

Was This Post Helpful? 0
  • +
  • -

#11 Slims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 23-October 10

Re: Getting strange Error in my Palindrome Program

Posted 24 October 2010 - 02:14 PM

Alright so I'm running into another bug and I'm not sure what's going on -- currently I am assuming it's something with getline() and cin.ignore(), but like I said, I'm not sure I understand what's going on conceptually with those two functions (are they even functions?).

Here's the code:

http://pastebin.com/CrRbD9PD (I'm using pastebin because the
 thing here is wrapping text around and making things hard to read)

Anyway, the bug is that as soon as I enter a word that is a Palindrome, every word after that comes out as a palindrome no matter what.  Before entering a Palindrome, it will correctly identify anything that isn't a Palindrome, however.

So either my for loop is wrong or something is going on with getline() as I said above -- at least those are my prime suspects right now.

Help appreciated, thanks.

Per request:

[code]

#include <iostream>
#include <string>
#include <sstream>
#include <cstring>

int main ()
{
	std::string word_init;
	bool palindrome = false;
	bool run = true;

	/////////////////////
	//Main Program Loop//
	/////////////////////
	while (run)
	{
		std::cout << "Please enter a word or series of numbers: ";
		
		getline(std::cin, word_init);								//User Input to be checked
		std::cin.ignore(1024, '\n');

		/////////////////////////////////
		//     Check for Palindrome    //
		/////////////////////////////////
		
		int string_length = word_init.size();					   //Finds size of the user-inputed word or number set.
		std::string test_string = word_init;                       //Will store the characters of the inputed data in a string array.
		int temp_str_number = 0;                                   //Used in the loop to determine the position in the array set of the test_string.
		

		for (int update = string_length-1; update >= 0; update--)  //The update variable will be equal to the string length
		{							   //  and count down.  This will be used to determine the position
			test_string[temp_str_number]=word_init[update];    //  in the array set in the loop.
			temp_str_number++;
		}

		if (test_string == word_init){
			palindrome = true;
		}

		//////////////////////////////////
		//         End of Check         //
		//////////////////////////////////

		//Output
		if (palindrome == true){
			std::cout << word_init << " is a Palindrome!" <<std::endl;
		} else
			std::cout << word_init << " is not a Palindrome." << std::endl;

			
		std::cout << std::endl << std::endl << "-------------------------------------------------------------" << std::endl <<std::endl <<
			"Press ENTER to check another word or number series\n" <<
			"or press the 'X' at any point to quit.";
		std::cin.get();
		std::cin.ignore(1024, '\n');

		std::cout << std::endl << std::endl << "-------------------------------------------------------------" << std::endl << std::endl;
	}

	return 0;
}


This post has been edited by Slims: 24 October 2010 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12178
  • View blog
  • Posts: 45,247
  • Joined: 27-December 08

Re: Getting strange Error in my Palindrome Program

Posted 24 October 2010 - 02:21 PM

Regardless, please post your code in the body of your post using code tags. Code pasted on another site can be taken off, making parts of this thread ambiguous to those who may view it in the future. :)
Was This Post Helpful? 0
  • +
  • -

#13 Slims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 23-October 10

Re: Getting strange Error in my Palindrome Program

Posted 24 October 2010 - 02:26 PM

Well that failed and it's not letting me edit it.

Correction to post:

Alright so I'm running into another bug and I'm not sure what's going on -- currently I am assuming it's something with getline() and cin.ignore(), but like I said, I'm not sure I understand what's going on conceptually with those two functions (are they even functions?).

Here's the code:

#include <iostream>
#include <string>
#include <sstream>
#include <cstring>

int main ()
{
	std::string word_init;
	bool palindrome = false;
	bool run = true;

	/////////////////////
	//Main Program Loop//
	/////////////////////
	while (run)
	{
		std::cout << "Please enter a word or series of numbers: ";
		
		getline(std::cin, word_init);								//User Input to be checked
		std::cin.ignore(1024, '\n');

		/////////////////////////////////
		//     Check for Palindrome    //
		/////////////////////////////////
		
		int string_length = word_init.size();					   //Finds size of the user-inputed word or number set.
		std::string test_string = word_init;                       //Will store the characters of the inputed data in a string array.
		int temp_str_number = 0;                                   //Used in the loop to determine the position in the array set of the test_string.
		

		for (int update = string_length-1; update >= 0; update--)  //The update variable will be equal to the string length
		{														   //  and count down.  This will be used to determine the position
			test_string[temp_str_number]=word_init[update];        //  in the array set in the loop.
			temp_str_number++;
		}

		if (test_string == word_init){
			palindrome = true;
		}

		//////////////////////////////////
		//         End of Check         //
		//////////////////////////////////

		//Output
		if (palindrome == true){
			std::cout << word_init << " is a Palindrome!" <<std::endl;
		} else
			std::cout << word_init << " is not a Palindrome." << std::endl;

			
		std::cout << std::endl << std::endl << "-------------------------------------------------------------" << std::endl <<std::endl <<
			"Press ENTER to check another word or number series\n" <<
														"or press the 'X' at any point to quit.";
		std::cin.get();
		std::cin.ignore(1024, '\n');

		std::cout << std::endl << std::endl << "-------------------------------------------------------------" << std::endl << std::endl;
	}

	return 0;
}




Anyway, the bug is that as soon as I enter a word that is a Palindrome, every word after that comes out as a palindrome no matter what. Before entering a Palindrome, it will correctly identify anything that isn't a Palindrome, however.

So either my for loop is wrong or something is going on with getline() as I said above -- at least those are my prime suspects right now.

Help appreciated, thanks.
Per request:

This post has been edited by Slims: 24 October 2010 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#14 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Getting strange Error in my Palindrome Program

Posted 24 October 2010 - 03:20 PM

Try this out.
http://project-melon...is-a-palindrom/
Was This Post Helpful? 0
  • +
  • -

#15 Slims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 23-October 10

Re: Getting strange Error in my Palindrome Program

Posted 24 October 2010 - 03:22 PM

I'm more interested in what conceptually I am doing wrong with my own program than just getting an answer to the problem.

Thanks though.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2