I have been checking on others letter count programs and none so far are like mine so I'm now making my own thread. My assignment, which is due tomorrow! *Yikes* Is for me to make a text file in notepad and then important that to my code. Then, I has to ask the user to input a letter, then have my code count how many times that letter came up in my text file. My text file that I have attached is test.txt. Keep in mind, this is my first, and sorry, attempt at this. Here's what I have so far..

#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[]) {
printf("ECE 15 Assignment 2 \n");
printf("My name \n");

FILE *fin;
fin = fopen(argv[1], "r");
int numlet, n;
int h;
char c;
printf("Which letter would you like me to count in my text?: ");
	c = getchar();

fin = fscan(argv[1], c) = h;

printf("The letter inputed: %c happened %d times", c, h);
return 0;
}

Also, heres the skeleton to what my prof said I should have.

Comment "skeleton" for Assignment 2:
// Print: ECE 15 Assignment 2
// Print: Names of partners; end each name with \n
// Loop
// Open argv[1]
// Prompt user and read character
// If no character, break out of loop
// Initialize counter to 0
// Loop until end of file
// if characters match add 1 to counter
// Close file
// Print output line to user
// End of program


Thanks for any help guys, I'll be checking this site every 5 mins or so.

Well the first step is to being with the program which simply copies the file to somewhere else. If you can't manage that, the rest of the assignment is out of reach.

FILE *in = fopen("file.txt","r");
if ( in != NULL ) {
  int ch;
  while ( (ch=fgetc(in)) != EOF ) {
    fputc( ch, stdout );
  }
  fclose( in );
}

Wait I'm a little lost here, the code you just posted is only to retrieve the text file? I'm using Pelles C if that makes a differnce. The hardest part I'm having is having the program search for the letter in my text.

Yes, now add your code to compare each letter with the one you've been asked to search for.

Okay so this is now my code...

#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[]) {
printf("ECE 15 Assignment 2 \n");
printf("My name \n");

FILE *in = fopen("file.txt","r");

int numlet, n;
int h;
char c;
printf("Which letter would you like me to count in my text?: ");
	c = getchar();
	
	if ( in != NULL ) {
  int ch;
  while ( (ch=fgetc(in)) != EOF ) {
    fputc( ch, stdout );
  }
  fclose( in );
}



printf("The letter inputed: %c happened %d times", c, in);

	return 0;
}

I feel as if im getting warming but still no cigar, I'm having troulbles find out how to search for the letter... any ideas or tips?

So add something like
if ( ch == c )
to compare, and then increment your count accordingly.

Okay, I get the idea, but what does the int ch stand for in this code though? Is that the number of characters in the text?

In that case, I was thinking going with a loop that if ch==0 then maybe maybe another int that counted how many times like i and go

if (ch ==0)
++i

What do you think?

> while ( (ch=fgetc(in)) != EOF )
Have you run a program containing only this line (and the fputc which followed it)?
Did you attempt to understand what it was doing?

ch in this context is every single character of the input file, one at a time.

Okay awsome, I took what you've said and I finally got the program to work, at least partially... I also need the program to only check the letter if one is entered, if one is not entered I need the program to close. Also in the program I need to set text.txt as a variable or something becasue tat my final statement I need to give what letter it was, how many times it came up, and what text file I searched so I'm assuming if I just type in test.txt in the printf thats not what he wants... Heres my working code so far, I havent put in my attempts to add in those two new things because everytime I do the code doesn't work so I just need some advice where to put the loop and how to structure it for the check EOF if the user doesn't put anything in.

#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[]) {
printf("ECE 15 Assignment 2 \n");
printf("My name \n");

FILE *in = fopen("test.txt","r");
int n;
char c;
printf("Which letter would you like me to count in my text?: ");
	c = getchar();

	n=0;
	if ( in != NULL ) {
  int ch;
  while ( (ch=fgetc(in)) != EOF ) {
    fputc( ch, stdin );
		if (ch == c)
			++n;
		
  }
  fclose( in );


}



printf("\nThe letter inputed: %c happened %d times in the file \n ", c, n);

	return 0;
}

> Also in the program I need to set text.txt as a variable or something
Like argv[1] as per your original post.

Without seeing some kind of attempt (working or not), it's hard to suggest what you might be doing wrong, either in terms of the approach, or merely from a syntax point of view.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.