Passing an array into a function
I've been trying to teach myself C off of the free tutorials here on the site. One of the things I keep running into in the Little practice programs I give myself is... well, passing an array into a function.
I kinda figured pointers would be the way to go on this, but in reading the tutorial on pointers I didn't see a clear explanation and my subsequent experiments with them have mostly just lead to more confusion.
Any pointers (pun semi-intended) on what I ought be doing for this specific problem, or insights as to bad habits I'm forming or better ways of displaying my code would be more than welcome.
The code I'm pasting was intended to be (someday) a quizzer. It could load the questions and answers from a text file, save the results to a different text file, jump to different questions, load the questions in a random order etc etc. As you will be able to see it's... in development with some of the variables and functions declared but un-coded and some of them not even yet declared. The parts of the code that pertain to this question are high-lighted in red. The code it's self is commented to all hell too.
I have /n mixed up with \n... I know it, I just haven't gone through and cleaned that out yet.
Some of the code lines word-wraped on me when copy pasting, it's fairly obvious where this happened though and it isn't frequent.
Code:
#include <iostream>
using namespace std;
/*Variables-------------------------------------------------------------------*/
int x = 0; //Keeps track of what problem number your currently working with.
int y = 0; //Keeps track of the total questions.
int l = 0; //Used to keep track of the second dimension in char answer[][]
char answer[10][2]; //Keeps track of correct answers and answers submitted by user.
For right now I'm just assuming there will be 10 questions with 10 corresponding answers etc etc.
It's going to be really messy to fix this later, but I figure that will be a learning experience for another day.
Probably when I try to tackle using classes.
string question[10]; //Stores the question text to display to the user.
string a[10]; //Stores the text of answer option a to display to the user.
string b[10]; //Stores the text of answer option b to display to the user.
string c[10]; //I think your getting it by now
string d[10]; //Sure hope so at east.
string input;
string help = " Type HELP to view a list of available commands./n Type OPEN to load a new quiz file./n
Type A, B, C, or D to answer the question./n";
int grade; //Stores the final score
/*Function Prototypes---------------------------------------------------------*/
void loader(int x, string question[x], string a[x], string b[x], string c[x], string d[x], char answer[x][2]);
void submit(string input, int x, int y, char answer[x][1]);
void score(int x, char answer[x][l], int grade);
void display(int x, int y, string question[x], string a[x], string b[x], string c[x], string d[x], char answer[x][1]);
/*Int Main--------------------------------------------------------------------*/
int main() {
cout<<help;
cin>>input;
cin.ignore();
if (input == "OPEN") {
loader(x, question[x], a[x], b[x], c[x], d[x], answer[x][2]);
y==10
}
else if (input == "HELP") {
cout<<help;
}
else if (input == "A" || input == "B" || input == "C" || input == "D" && y > 0) {
submit(input, x, y, answer[][1]);
display(x, y, question[x], a[x], b[x], c[x], d[x], answer[x][1]);
}
cin.get();
}
/*Function: Loader------------------------------------------------------------*/
void loader (int x, string question[x], string a[x], string b[x], string c[x], string d[x], char answer[x][2]) {
}
/*Function: Submit------------------------------------------------------------*/
void submit (string input, int x, int y, char answer[x][1]) {
answer[x][1] = answer
x = x++
}
/*Function: Score-------------------------------------------------------------*/
void score (int x, char answer[x][l]) {
for (x = 0; x < y; x++) {
if answer[x][1] == answer[x][2] {
grade = grade++
}
}
}
/*Function: Display-----------------------------------------------------------*/
void display (int x, int y, string question[x], string a[x], string b[x], string c[x], string d[x], char answer[x][l]) {
if x > y {
score(int x, char answer[x][l], int grade);
else {
}
}
As you can see the question at hand is actually used in a lot more places than are turned red, but that specific example seemed like it would be the simplest and once I understand that part I should be able to get the rest.
I thank you all much for any help you can offer.