Reputation: 139
I figured this question had been asked a million times but I can't find the answer anywhere. Should be simple. Ok so I have a struct in my .h file:
struct question{
string programNum;
string programDesc;
string programPoints;
string programInput;
char programQuestion;
};
And then I have function initiated in the .h and arguments in .cpp:
void setQuestionFileName(question q, char fileName){
q.programQuestion = fileName;
}
Ok all is well so far. Now in main I have the issue of trying to store argv[1] in programQuestion:
char* fileName = argv[count+1];
followed by:
setQuestionFileName(questions[count],fileName);
cout << questions[count].programQuestion << endl;
I'm not real good with pointers so if anyone could help me store the char* argv into the char questions[count].programQuestion that would be amazing. Thank you!
Upvotes: 3
Views: 4309
Reputation: 139
I'm going to post the code, I think it may work better that way. This is the .h:
using namespace std;
// Data
struct question{
string programNum;
string programDesc;
string programPoints;
string programInput;
char* programQuestion;
};
void setQuestionFileName(question* q, char* fileName);
void display(question* q);
void display(question* q);
This is the .cpp
using namespace std;
void setQuestionFileName(question* q, char* fileName){
strcpy(q->programQuestion, fileName);
}
void display(question* q){
cout << "Description = " << q->programDesc << endl;
cout << "Number of Points = " << q->programPoints << endl;
cout << "Name of Question File = " << q->programQuestion << endl;
}
// Not used or tested yet
int myCompare (const void * a, const void * b ) {
const char *pa = *(const char**)a;
const char *pb = *(const char**)b;
return strcmp(pa,pb);
}
And main.cpp:
using namespace std;
int main(int argc, char* argv[]){ //or char** argv
question* questions[argc-1]; //Array of questions to be filled by loop.
int sizeOfQuestions = argc; //number of questions passed in at run time
int numLines = 0; //number of lines in file
for(int i=0;i<argc;i++){ //Test loop to make sure the command line file names are read in
std::cout << argv[i] << " says hello" << std::endl;
}
for(int count=0;count<sizeOfQuestions-1;count++){ //This loop places the information from the files into structs
//char fileName = argv[count+1];
char* fileName = argv[count+1];
cout << "Problem number: " << count+1 << "\t Working with file " << fileName << endl;
std::fstream questionFile (fileName, std::fstream::in); //Open the file
if(questionFile.good()){
cout << "File Opened" << endl;
setQuestionFileName(questions[count],fileName);
cout << questions[count]->programQuestion << endl;
getline(questionFile,questions[count]->programNum);
getline(questionFile,questions[count]->programDesc);
getline(questionFile,questions[count]->programPoints);
getline(questionFile,questions[count]->programInput);
display(questions[count]);
questionFile.close();
}else{
cout << "Could not open file!!!" << endl;
}
}
return 0;
}
The way it is now, I'm getting a segmentation fault.
Upvotes: 1
Reputation: 2185
Change your programQuestion
from char
to char*
And, this function
void setQuestionFileName(question q, char fileName){
q.programQuestion = fileName;
}
I think it should be
void setQuestionFileName(question& q, char* fileName){
strcpy(q.programQuestion, fileName);
}
Hardly there is a file with only 1-character name.
Upvotes: 0