hello guys
i want to ask a question
i am newbie in C proggramming
so, this is my code
Code:
#include<stdio.h>
#include<stdlib.h>
#include"boolean.h"
boolean ceksama(char c[20])
{
FILE *f;
int retval;
boolean same=false;
char ccarray[20];
int i,j;
char cc;
f=fopen("customerfile.txt","r");
j=0;
while(c[j]!='\0')
{
j=j+1;
}
retval=fscanf(f,"%c",&cc);
while((retval!=EOF)&&(!(same)))
{
i=1;
while(cc!='\0');
{
retval=fscanf(f,"%c",&cc);
ccarray[i]= cc;
i=i+1;
}
if(i!=j)
{
same=false;
}
else
{
if(ccarray[i]=c[j])
{
same=false;
}
else
{
same=true;
}
}
}
fclose(f);
return same;
}
int main()
{
FILE *f;
char nama[20];
int retval;
char c;
int i=1;
/*algoritma*/
f=fopen("customerfile.txt","a");
printf("\nenter your name:");
scanf("%c",&c);
/*
if (!(ceksama))
{*/
while (c=='\n')
{
scanf("%c",&c);
}
while (c!='\n')
{
nama[i]=c;
i=i+1;
scanf ("%c",&c);
}
nama[i]=c;
i=1;
while(nama[i] != '\n')
{
retval=fprintf(f,"%c",nama[i]);
i=i+1;
}
fprintf(f,"%c",'\0');
fprintf(f,"%c",'\n');
/*}
else
{
printf("data has been registered");
}*/
fclose(f);
}
i hope you can see my code clearly
in my code, i just want to input a name and save it to external file,
in case, when inputting name, the program will check it first, is it the name are already exist in external file or not
so, main is my main program, and above it the function to check if the name is already exist or not.but now the checking function i make in comment mode so the program can work properly.
the problem is, when i active the checking function, the program still can work
BUT
every time i try to input any name, even though it is already registered or not. IT ALWAYS said registered, therefore, i can not store that name in external file, because it says that it has been registered
please i need your help
thank you before
ow yeah, my algorithm to make this program is, when inputting name, it will read by character(because my teacher forbid us to use string or string.h). therefore i must store it to external file by each character. when checking, i also check it by character, but didn't work
PS: sorry for my bad English
-and, i declare boolean.h from my own
-i cant use string.h, therefore i cant use strcmp, strlen etc(m teacher forbid this in this task)
-i use windows