-
Messed up output...
Here's my code:
int InputCase(char f[19],char stuid[7],char key[19],int Score){
int y=0;
char e;
int n=0;
int r=0;
FILE *in;
in = fopen(stuid,"r");
if (in!=NULL){
while(fscanf(in,"%c",&e)!=EOF){
f[n]=e;
n++;
}
close(in);
}
else{
printf("Student answer file does not exist.\n");
}
}
int main(){
int x=0;
int n=0;
char a[19];
char s[7];
char sa[19];
printf("Please enter a student id to test: ");
scanf("%s",&s);
InputKey(a);
InputCase(sa,s,a,x);
printf("The Student ID was: %s\n",s);
printf("The key answers are: %s\n",a);
printf("The student answers are: %s\n",sa);
printf("The number of correct answers is: %d\n",x);
}
Here's the output:
[engr26@boole engr26]$ grader
Please enter a student id to test: STU08583
The Student ID was: STU08583
The key answers are: abcdefghijklmnopqrstuvwxyz
@4®@Lk@„üÿ¿
The student answers are: abcdefghijklmnopqrstuvwxyz
@Ès@STU08583
The number of correct answers is: 0
Now, the prog is loading the data from file correctly, but it's printing the wierd bolded stuff that isn't supposed to be there... why??
thanx in advance for your help...
Squirrelly1:p
-
What is InputKey(a)?
Other than that, I expect that it is the way you pass parameters to InputCase().
You are filling the parameters with info, which will only be done locally to the function. Once the function returns sa will no longer contain anything useful. You need to pass a pointer to the buffer so that the data can be stored.
HD
-
First of all, thnx for replying....
Ok...
InputKey(a) is where I called the function to load the data from a file called "key.dat" into the array named a...
How do I pass a pointer so that it would retain the data...
I was looking for information on how to make public variables (like I would in VB)... but I'm not sure that this would be the way to go...
Could you post some code for me... I really apreciate ur help...
thnx,
Squirrelly1:p
P.S. This project has to be turned in today in aprox. 5 hours... You see my dilema???? thnx
-
Dunno if I'm too late - I've been at work.
To pass a reference or pointer you have to dereference it:
Code:
int myFunction(char **myArray, int aNumber)
{
// Do something with the string
strcpy(*myArray, "hello");
return 1;
}
int main(void)
{
// Have a char *
char *myArray;
// Declare array size
myArray = (char *)malloc(sizeof(char) * 8);
// Call a function with this as a parameter
myFunction(&myArray, 7);
printf("String is: %s", myArray);
}
Or something like that. I've been doing lots of C++ and Java recently so I'm a bit rusty (even though I've programmed in C for years :rolleyes: ).
Anyway, the point is that unlike VB, C passes variables by value to functions - not by reference. Therefore any manipulation is lost when the function returns, hence the rubbish in the variable.
In C++ you can declare function parameters as by ref by using an '&':
Code:
int myFunction(char *&myArray)
{
// Some code
}
In C however, you need to do it yourself - pass the reference to the variable, which means dereferencing.
It is more strange with strings as you dont think of them in the same was as other data types, but you'll notice that you have to use an '&' when using scanf:
Code:
scanf("%d %d", &intA, &intB);
This is the same thing.
Anyway, hope this helps
HD
-
No no no, stop that.
I fear I'm much too late now, but for future reference, you've done everything right, except one thing: strings in C are terminated by a NUL character (ASCII code 0). You fill in the strings character by character, and that's the problem.
Code:
while(fscanf(in,"%c",&e)!=EOF){
f[n]=e;
n++;
}
The string doesn't end with 0. You have to add one line:
Code:
while(fscanf(in,"%c",&e)!=EOF){
f[n]=e;
n++;
}
f[n] = 0;
Now f is a valid string.
You should declare the buffers larger, you try to read 26 characters into a 18 character buffer (char[19]). Make them at least char[27].
But as I said, I fear it's too late now.
-
Ahhhh!
I hope I haven't f*&ked things up too badly. Didn't notice that, which is pretty bad for a professional programmer!
Cheers CB for putting me right....again :rolleyes: .
Anyway, a quick lesson in dereferencing anyway!
HD