#include <iostream>
#include "randword.h"
#include <fstream>
#include <time.h>
#include <cstdlib>
using namespace std;
bool hangmachine(char a[],string randi,char v){ //the Problem is in this function
bool retus=0;
for (int cc=0;cc<randi.length();cc++){
if(v==randi[cc]){
retus=1;
char a[cc]=v; //Error: variable-sized object 'a' may not be initialized.....
}
}
return retus;
}
int main()
{
InitDictionary();
string tixaio=Randomword();
int m = tixaio.length();
int guesses=8;
char guess;
char *charptr= new char[m];
for(int aa=0;aa<m;aa++){
charptr[aa]='-';
}
charptr[m]='\0';
cout << "The word now looks like this: "<<charptr;
cout<< endl;
while (guesses>0){
cout<<"Give me your guess: ";
cin>> guess;
cout<<endl;
if(hangmachine(charptr,tixaio,guess)){
cout <<"the string has now become"<<charptr<<endl;
cout <<"You have "<< guesses<<" remaining guesses"<< endl;
}
else{
guesses-=1;
cout<<"you made a wrong guess .Now you have "<< guesses<<" remaining tries"<< endl;
}
}
}
The problem seems to be that i am not passing the dynamically allocated array to the function in the right way. I have made comments to the line i am getting this error.But the compiler doesn’t give me any error when i call the function in the main() so it must be something wrong in the way i define this function.Any help would be appreciated.
Sometimes i feel so stupid……Sorry for not being carefull enough.
This line:
is a variable declaration, which doesn’t even compile, as the array size should be fixed at compilation time, that is, it has to be either a number or a const integer. It isn’t, so you gcc tries to create a variable length array, but then you try to initialize it which will fail.
You just want to set a value in the array: