1) setup an int as a counter... int userchar = 0;
2) get one character from your input file.
3) on the very next line, do this...
That's it... all done.Code:if (ch == let)
userchar++;
Printable View
Well, if you took the advice to heart, you'd write a small program that allowed you to play with ch and let and learn what's what.
I guess I don't know exactly what you are wrestling with...
What is this doing? Or, better, what do YOU want it to do? What's the ampersand for?Code:if(ch>='a' && ch==&let)
That's exactly what I've been doing since yesterday afternoon.
Where nl has already been decalred as an int and = 0. I just ran into a coursemate who suggested creating another two ints, moving ch and let into them to compare the ints instead of chars. Gave that a go too and it didn't work, so I assume there's something wrong somewhere else. Unless of course my while loop is wrong, but the code is running through it.Code:while((ch=fgetc(fpout))!=EOF)
{
if(ch == let)
nl++;
}
I appreciate the amount of help you've given me by the way, I noticed my thread's quite a lot longer than most of the others here ^^
Ummm... fgetc returns an int... check it out in your library documentation.
There should be no reason to make copies of the existing variables, C is perfectly capable of comparing characters. A character is simply a short short int... usually 8 bits wide, or one byte. You can add, subtract, multiply and divide with chars just like you do with shorts, ints, long ints and long long ints. There's no magic...
Now let me tell you this one more time... stop trying to reopen files to do single tasks, it's a disaster waiting to happen... First consider that your output file is all upper case... what happens if the user types a lower case character? Yeah that's right, the test fails...
Look closely at where I am doing this.... (from your original code posting)
The problem here is that you are adding something new... but ignoring the chance to integrate it into your *existing* code...Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void main()
{
FILE *fpin, *fpout, *fprec; // declares two file streams, in and out
int tl, f, i=0;
int nl=0;
char ch, let=0; // ch is variable to be used in conversion
char input[80], output[80], record[80]; // declared arrays to store file paths
printf("Input input file name: \n");
gets(input); // input file location
printf("Input output file name: \n");
gets(output); // output file location
printf("Input record file name: \n");
gets(record); // record file location
printf("Which letter would you like the frequency of?\n");
scanf("%c", &let);
if((fpin = fopen(input,"r")) == NULL) // confirms that file has been opened
{
printf("Unable to open input file");
exit(0);
}
if((fpout = fopen(output,"w")) == NULL) // confirms that file has been created
{
printf("Unable to open output file\n");
exit(0);
}
tl=0;
while((ch=getc(fpin))!=EOF) // read input file until the end of the file
{
if (let == ch)
nl++;
if((ch>='a' && ch<='z') | (ch>='A' && ch<='Z')) // ensures that only alphabetic characters are transferred
{
putc(toupper(ch), fpout); //function to change all the characters to upper case
tl++;
}
}
fclose(fpin); // close input file
fclose(fpout);
fopen(output,"r");
if((fprec = fopen(record,"w")) == NULL) // confirms that file has been created
{
printf("Unable to open record file\n");
exit(0);
}
printf("Total number of letters in the file: %d\n", tl);