invalid array subscript
May 26, 2014 at 5:53pm May 26, 2014 at 5:53pm UTC
I am creating a binary to ASCII converter. The way the program works is it accepts a string input, converts it to an array of ints, and converts it to ascii. The input part of the code isn't working, though:
1 2 3 4
string theString;
cin >> theString;
const int stringLength = strlen(theString.c_str());
int intString[stringLength];
My compiler (Visual Studio Express 2013) gives me these error messages:
error C2057: expected constant expression
error C2133: 'intString' : unknown size
error C2466: cannot allocate an array of constant size 0
May 26, 2014 at 6:10pm May 26, 2014 at 6:10pm UTC
The length of the array has to be known at compile time. Either allocate the array with new
or use std::vector
.
May 26, 2014 at 6:42pm May 26, 2014 at 6:42pm UTC
It works for me:
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
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
//Your stuff:
string theString;
cin >> theString;
int stringLength = strlen(theString.c_str());
int intString[stringLength];
int value=0;
//Inserting integer values into array:
for (int i=0;i<stringLength;++i)
intString[i] = theString[i]-'0' ;
//Converting string integer into single string value.
for (int i=stringLength-1;i>=0;--i)
if (intString[i])
value+=pow(2,stringLength-(i+1));
//Display value as integer:
cout<<"The integer value: " <<value<<endl;
//Display value as character:
cout<<"The ASCII character: " <<char (value)<<endl;
//Custom Exit:
cout<<"\nPress enter to continue: " ;
cin.ignore(255,'\n' );
cin.clear();
cin.get();
return 0;
}
10101010
The integer value: 170
The ASCII character: ¬
Press enter to continue:
But I do not believe this to be standard(C++) and will not work with all compilers. http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Variable-Length.html#Variable-Length
An alternative would be:
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
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
//Your stuff:
string theString;
cin >> theString;
int stringLength = strlen(theString.c_str());
int *intString = new int [stringLength];
int value=0;
//Inserting integer values into array:
for (int i=0;i<stringLength;++i)
intString[i] = theString[i]-'0' ;
//Converting string integer into single string value.
for (int i=stringLength-1;i>=0;--i)
if (intString[i])
value+=pow(2,stringLength-(i+1));
//Display value as integer:
cout<<"The integer value: " <<value<<endl;
//Display value as character:
cout<<"The ASCII character: " <<char (value)<<endl;
//Free memory:
delete []intString;
//Custom Exit:
cout<<"\nPress enter to continue: " ;
cin.ignore(255,'\n' );
cin.clear();
cin.get();
return 0;
}
11111111
The integer value: 255
The ASCII character:
Press enter to continue:
Last edited on May 26, 2014 at 7:01pm May 26, 2014 at 7:01pm UTC
Topic archived. No new replies allowed.