String is out of range.

This program converts string of Mayan number something like ".. .-- 0" to Arabic number. I am getting the "String is out of range" error. Please help!

#include <iostream>
#include <string>
#include <cmath>

using namespace std;
int main ()
{
string MayanNumber;
int counter=0;
int index=0;
double ArabicNumber =0;
int temp[50];

cout << "Enter Mayan number" << endl;
getline(cin,MayanNumber);

for(int q=0; q < 50; q++)
temp[q] = 0;

for(int t=0; t < 25; t++)
{

switch(MayanNumber[t])
{
cout << MayanNumber;
case '0':
counter += 0;
break;
case '.':
counter += 1;
break;
case '-':
counter += 5;
break;
default:
temp [index]=counter;
index++;
break;
}
}
counter = 0;
for (int n=index; n>=0; n--)
{
ArabicNumber += temp[n] * pow(20.0,counter);
counter++;
}

cout << ArabicNumber;


return 0;
}

You are probably out of the range of MayanNumber[t]
right. You are just assuming the string is exactly 25 characters long. Instead of using a fixed loop of 25, you should use the length of the string.
Thank you. I changed it, and the error is gone now, but my switch does not give me correct result for the Arabic number calculation. Any ideas?? Thank you!

#include <iostream>
#include <string>
#include <cmath>

using namespace std;
int main ()
{
char MayanNumber [50];
int counter=0;
int index=0;
double sum =0;
int temp[50];
int length;

cout << "Enter Mayan number" << endl;
cin.getline (MayanNumber,50);
length=strlen(MayanNumber);
cout << length << endl;
for(int q=0; q < 50; q++)
temp[q] = 0;

for(int t=0; t < length ; t++)
{

switch(MayanNumber[t])
{
cout << MayanNumber;
case '0':
counter += 0;
break;
case '.':
counter += 1;
break;
case '-':
counter += 5;
break;
default:
temp [index]=counter;
index++;
break;
}
}
counter = 0;
for (int n=index; n>=0; n--)
{
sum += temp[n] * pow(20.0,counter);
counter++;
}

cout << sum;


return 0;
}

Last edited on
I got it. thanks.
Topic archived. No new replies allowed.