Okay, well, for an example here's a cut-down of a random name generator I was trying to make- I was using a txt file called namlib.txt which was full of names, separated by headers like &&SURNAME or &&NICKNAME, which the program picked up on as 'nametype's.
Code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdarg>
using namespace std;
int GetRand(int min, int max);//random numbers in a range
void fillpool(char** pool,char* nametype);//fills pool with the right kind of names
int number_names(char* nametype);//returns how many of a certain type of name in a pool in the txt file.
char* generateName(int args,...);
int main(){
while(1){
cout<< generateName(3,"&&GIVEN","&&NICKNAME","&&SURNAME");
cin.get();
}
}
char* generateName(int args,...){
va_list arguments;//stores arguments passed to function.
va_start ( arguments, args );// va_start is a macro to initialise all arguments after args.
//initialise pool and string.
char** pool;//pool for names
char* namestring;//holds final name.
namestring = new char[256];
namestring[0]='\0';
int i;//to access elements in pool
for (int arg=0; arg<args; arg++){
char* a= va_arg ( arguments, char* ); // va_arg is an argument. of type char*.
pool=new char*[number_names(a)];//create space in pool.
fillpool(pool,a);//fill space in pool.
i= GetRand(0,number_names(a)-1);//access random element from pool
strcat(namestring,pool[i]);//add element to namestring.
//delete pool;//clean up memory left in pool.
}
//end:
//va_end ( arguments ); // Cleans up the list
return namestring;
}
void fillpool(char** pool,char* nametype){
//variables:
ifstream library; //accesses the name bank txtfile
int range =0; //determine whether to read in names.
int e=0; //access elements of the pool array.
char* name; //to hold the name from namlib.
name = new char[256]; //assign bytes to name.
//ensure name bank is open and ready.
if ( !library.is_open() ){
library.open("namlib.txt");
}
library.clear();//reset flags
library.seekg(0,ios::beg);//reset get pointer
while (!library.eof()){
//when out of range:
if (!range){
library.getline(name,256); //buffer name
//test name against nametype.
if (strcmp(name,nametype)==0){
range=1;//now in range
}
}
//when in range:
if (range){
library.getline(name,256); //buffer name
//check if still in range:
if (strstr(name,"&&") ){//all nametypes will begin with "&&" so this is the check.
range=0;
}
else {
//add a new string element to the pool.
pool[e]=new char[256];//assign space.
char* pname;//create a pointer
pname=pool[e];//point to assigned space
strcpy(pname,name);//fill required space
e++;//point to next element in pool.
pname=0;//reset stray pointer
}
}//end of in range block
}//end of !library.eof() block
}//end of fillpool function
int number_names(char* nametype){
//variables:
ifstream library; //accesses the name bank txtfile
int range =0; //determine whether to read in names.
int e=0; //count names
char* name; //to hold the name from namlib.
name = new char[256]; //assign bytes to name.
//ensure name bank is open and ready.
if ( !library.is_open() ){
library.open("namlib.txt");
}
library.clear();//reset flags
library.seekg(0,ios::beg);//reset get pointer
while (!library.eof()){
//when out of range:
if (!range){
library.getline(name,256); //buffer name
//test name against nametype.
if (strcmp(name,nametype)==0){
range=1;//now in range
}
}
//when in range:
if (range){
library.getline(name,256); //buffer name
//check if still in range:
if (strstr(name,"&&") ){//all nametypes will begin with "&&" so this is the check.
range=0;
}
else {
//count names
e++;
}
}//end of in range block
}//end of !library.eof() block
return e-1;//arrays start at 0.
} //end of function number_names
In this example, the program hangs. When I comment out
Code:
library.open("namlib.txt");
in the function fillpool(), it runs without any problem.