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.
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.
[code]1>d:array passed to a functionmainmain.cpp(14) : error C2664: 'Counter' : cannot convert parameter 1 from 'char [2]' to 'char'
1>d:array passed to a functionmainmain.cpp(15) : error C2664: 'loadArray' : cannot convert parameter 2 from 'char [2]' to 'char [][2]' [/code]
Would someone be kind enough to examine the code and explain what error I am making and what is the correct code?
Thank you.
Cheers
[code][b]Main.cpp[/b]
#include #include #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]
[code][b]DataDef.h[/b]
struct Collectors
{
char code[2];
char Name[50];
float Total;
};
[/code]
[code][b]Counter.h[/b]
int Counter(char code, char Name)
{
FILE * CollectorsTxt;
int Ctr=0;
Collectors MyCollectors[50];
CollectorsTxt=fopen("Collectors.txt","r");
fscanf(CollectorsTxt,"%[^,],%[^
]%*c",MyCollectors[50].code[Ctr],MyCollectors[50].Name[Ctr]);
while(!feof(CollectorsTxt))
{
Ctr++;
fscanf(CollectorsTxt,"%[^,],%[^
]%*c",MyCollectors[50].code[Ctr],MyCollectors[50].Name[Ctr]);
}
Ctr=Ctr-1;
return Ctr;
}
[/code]
[code][b]loadArray.h[/b]
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,"%[^,],%[^
]%*c", MyCollectors[50].code[Idx],MyCollectors[50].Name[Idx]);
printf("%s, %s
",MyCollectors[50].code[Idx], MyCollectors[50].Name[Idx]);
for(Idx=0;Idx<Count;Idx++)
{
fscanf(CollectorsTxt,"%[^,],%[^
]%*c",
MyCollectors[50].code[Idx],MyCollectors[50].Name[Idx]);
printf("%s, %s
",MyCollectors[50].code[Idx], MyCollectors[50].Name[Idx]);
}
}
[/code]
Comments
[/color]
After reviewing my code, I've discovered the "beginner errors" I made and the program now works.
Thank you for your help.
Cheers