I have just started to learn C at a local college and we are using Visual Studio 2005 as the compiler/debugger.
When I write my program with the variable array declared in Main.cpp, the array passes to the function and displays the results of a file read to the screen(The program worked).
However, when I declare the variable array in a struct header(DataDef.h), and make the necessary changes in the Main.cpp, Counter.h and loadArray.h, the program debugs with 2 error messages that I cannot resolve.
1>d:\array passed to a function\main\main.cpp(14) : error C2664: 'Counter' : cannot convert parameter 1 from 'char [2]' to 'char'
1>d:\array passed to a function\main\main.cpp(15) : error C2664: 'loadArray' : cannot convert parameter 2 from 'char [2]' to 'char [][2]'
Would someone be kind enough to examine the code and explain what stupid error I am making and what is the correct code?
Thank you.
Code:
Main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "DataDef.h"
#include "Counter.h"
#include "loadArray.h"
void main()
{
FILE * CollectorsTxt;
Collectors MyCollectors[50];
int Count=0;
Count=Counter(MyCollectors[50].code,MyCollectors[50].Name);
loadArray(Count, MyCollectors[50].code,MyCollectors[50].Name);
scanf("%*c");
}
Code:
Counter.h
int Counter(char code, char Name)
{
FILE * CollectorsTxt;
int Ctr=0;
Collectors MyCollectors[50];
CollectorsTxt=fopen("Collectors.txt","r");
fscanf(CollectorsTxt,"%[^,],%[^\n]%*c",MyCollectors[50].code[Ctr],MyCollectors[50].Name[Ctr]);
while(!feof(CollectorsTxt))
{
Ctr++;
fscanf(CollectorsTxt,"%[^,],%[^\n]%*c",MyCollectors[50].code[Ctr],MyCollectors[50].Name[Ctr]);
}
Ctr=Ctr-1;
return Ctr;
}
Code:
loadArray.h
void loadArray(int Count, char code[10][2], char Name[10][20])
{
FILE * CollectorsTxt;
int Idx=0;
Collectors MyCollectors[50];
CollectorsTxt=fopen("Collectors.txt","r");
fscanf(CollectorsTxt,"%[^,],%[^\n]%*c", MyCollectors[50].code[Idx],MyCollectors[50].Name[Idx]);
printf("%s, %s\n",MyCollectors[50].code[Idx], MyCollectors[50].Name[Idx]);
for(Idx=0;Idx<Count;Idx++)
{
fscanf(CollectorsTxt,"%[^,],%[^\n]%*c",
MyCollectors[50].code[Idx],MyCollectors[50].Name[Idx]);
printf("%s, %s\n",MyCollectors[50].code[Idx], MyCollectors[50].Name[Idx]);
}
}