string Subscript out of range

Jul 8, 2013 at 12:01am
Hi there, I am new to coding and have run into a problem where I see the above error message and my programme is unable to run.
The code where I recieve this error is my attempt at http://projecteuler.net/problem=4

I am unsure what I am doing wrong, so if you could possibly have a look through my code and see if there are any errors I have made and suggest a working method of getting around these problems, it would be greatly appreciated.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// EulerProblemFOUR.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string product;
int productResult;
short firstNumber = 999;
short secondNumber = 999;
const short maxPalindromes = 3;
	int highestPalindromes[maxPalindromes];
short currentPalindrome=0;
int Answer;

void checkPalindrome() {
	if (product[0] == product[5] && product[1] == product[4] && product[2] == product[3]) {
		cout << product << " is a Palindrome number!" << endl;
		highestPalindromes[currentPalindrome] = productResult;
		currentPalindrome++;
	}
	else if (firstNumber - secondNumber >= 10) {
		firstNumber -= 1;
		secondNumber = firstNumber;
	}
	else {
		secondNumber-= 1;
	}
}

void answerPalindrome() {
	if (highestPalindromes[0] >= highestPalindromes [1] && highestPalindromes[0] >= highestPalindromes[2])
	{
cout << "the highest Palindrome is " << highestPalindromes[0];
	}
	else if (highestPalindromes[1] >= highestPalindromes [0] && highestPalindromes[1] >= highestPalindromes[2])
	{
		cout << "the highest Palindrome is " << highestPalindromes[1];
	}
	else 
	{
		cout << "the highest Palindrome is " << highestPalindromes[2];
	}
}

int main() {
	do {
		productResult = firstNumber * secondNumber;
	product = productResult;
	checkPalindrome();
	}
	while (currentPalindrome <= 2);
	answerPalindrome();
	return 0;
}
Jul 8, 2013 at 12:06am
For one thing, at line 52 you're trying to assign an integer to a string.
That's not going to work.

Jul 8, 2013 at 1:12pm
Is there a way I could retrieve a certain number from an integer, e.g. the second number of my integer 'productResult'?
Jul 8, 2013 at 1:19pm
closed account (3qX21hU5)
Is there a way I could retrieve a certain number from an integer, e.g. the second number of my integer 'productResult'?


You can use stringstreams to convert the integer to a string and then grab the second "digit" from productResult and then convert it to a integer again if you need to do more calculation on it.

Or better yet just use simple mathmatics to grab a second digit from your number.


Also I would highly suggest you get away from declaring all your variables globally since that will cause you major problems in the future and is considered a very bad practice. You also should learn about function parameters and passing your variables into your functions as arguments instead of having them globally declared.
Last edited on Jul 8, 2013 at 1:20pm
Jul 8, 2013 at 4:24pm
Thanks very much!

With stringstreams, I was able to get my programme to work correctly.
I will look into your suggestion for avoiding global variables... thanks!!
Last edited on Jul 8, 2013 at 4:24pm
Topic archived. No new replies allowed.