Feb 16, 2012 at 6:23pm UTC
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;
}
Feb 16, 2012 at 6:37pm UTC
You are probably out of the range of MayanNumber[t]
Feb 16, 2012 at 6:40pm UTC
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.
Feb 16, 2012 at 7:55pm UTC
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 Feb 16, 2012 at 7:58pm UTC