Home > Software design >  Logfile game from array to file text is not writing anything c
Logfile game from array to file text is not writing anything c

Time:07-09

I developed a game and everything went well except I want to make a logfile (means data from array to text file) but nothing is coming out for the array. My code:

void writeToFile(ofstream &outputfile, string name, string s2, string s3, string s4, string s5)
{   
    outputfile << "HI" << endl;
    
    outputfile << endl << endl << "The card that you get is: " << name<<" "<<s2<<" "<<s3<<" "<<s4<<" "<<s5<<endl <<endl;    
}
int main ()

{
ofstream outputfile;
                outputfile.open("program3data.txt");
                
                    int randomnumber=random_number();
                    count=findwords();
                    k=count;
                    int *sr;  sr= new int[count];
                    string *name;  name= new string[count];
                    string *s2;  s2= new string[count];  
                    string *s3;  s3= new string[count];
                    string *s4 ;  s4 = new string[count];
                    string *s5 ;  s5 = new string[count];
                    string *s6 ;  s6 = new string[count];
                    writeToFile(outputfile, name[randomnumber],s2[randomnumber],s3[randomnumber],s4[randomnumber],s5[randomnumber]);
}

And my error: enter image description here

I dont know what to do. It should display the array of the card. If anyone wants the full code, please head to https://github.com/infaddil/beyblade/blob/main/newassver2.cpp

CodePudding user response:

Look at what your code actually does

  1. Allocate arrays of empty strings

    string *name;  name= new string[count];
    string *s2;  s2= new string[count];  
    string *s3;  s3= new string[count];
    string *s4 ;  s4 = new string[count];
    string *s5 ;  s5 = new string[count];
    string *s6 ;  s6 = new string[count];
    
  2. Call a function with a randomly chosen strings, but because all the strings are empty it is inevitable that empty strings will be chosen

    writeToFile(outputfile, name[randomnumber],s2[randomnumber],s3[randomnumber],s4[randomnumber],s5[randomnumber]);
    
  3. From that function write out the empty strings

    outputfile << "HI" << endl;
    outputfile << endl << endl << "The card that you get is: " << name<<" "<<s2<<" "<<s3<<" "<<s4<<" "<<s5<<endl <<endl;    
    

Now clearly you did not mean to write out empty strings, but that is what the code you wrote actually does. No doubt there are some other strings somewhere else in your code which are the strings you actually want to write out. But without seeing more of the code I can't really help with that.

EDIT

OK I had a look at the rest of your code. It seems that the strings would want to write out are declared on lines 162 to 167. But for some reason you have declared a completely different set of strings on lines 280 to 285. Even though the variable names are the same they are still a new set of strings. So it seems that all you need to do is delete the declarations on lines 280 to 285 and you will start writing out the correct strings.

I also wonder whether you want to generate a new random number? Surely you want to use the random number that was previously generated.

  •  Tags:  
  • c
  • Related