I have a program that I'm working on for class, I have coded all but the last function. It compiles and links without any problem. Although it gives me a scanf and fopen warning. I'm able to get the menu to appear and get user input. Once I input number the black screen goes away. I put a scanf in it to keep open but it doesn't work. Can someone look it over to see if there are logical errors (I'm sure there are, or else it would work properly) I appreciate any input or advice you may have.
Code:/* This program has a report attached to it and asks user if they want to count lines, words, characters, sentences or all of the above.*/ #include <stdio.h> int countLines();//file declarations int countWords(); int countChars(); int countSents(); int all(); FILE* spReport;//report in resource file int main() { int selection, key; printf("Please select item from the menu:\n");//menu printf("1. count the lines \n"); printf("2. Count the words\n"); printf("3. Count the characters \n"); printf("4. Count the sentences \n"); printf("5. All of the above \n"); scanf("%d", &selection); spReport = fopen("Report1.txt", "r"); switch (selection) { case 1: printf("You have chosen count lines!"); int countLines(); break; case 2: printf("You have chosen to count words!"); int countWords(); break; case 3: printf("You have chosen to count characters!"); int countChars(); break; case 4: printf("You have chosen to count sentences!"); int countSents(); break; case 5: printf("You have chose all of the above!"); int countAll(); break; }//switch fclose(spReport); scanf("%c", &key); return 0; }//main /*++++++++++++++++countLines+++++++++++++++++*/ int countLines() { int curCh; //current character int countLine = 0; while (curCh = fgetc(spReport)!= EOF) {if (curCh == '\n' ) countLine++; }//while printf("The number of lines is %d", countLine); return(countLine); }//countLine /*===================CountWords=================*/ int countWords() { #define WHT_SPC\ (curCh == ' ' || curCh == '\n' || curCh == '\t') int curCh;//current character int countWord = 0; char word = 'O'; while(curCh = fgetc (spReport) != EOF) { if (WHT_SPC) word = 'O'; else if(word == 'o') {countWord++; word = 'I'; }//else }//while printf("The number of words are %d\n", countWord); return 1; }//count Words /*=================countchars===================*/ int countChars() { int countchar = 0; int curch, preCh; int countline = 0; while ((curch = fgetc(spReport)) != EOF) { if (curch != '\n') countchar++; else countline++; preCh = curch; }//while if (preCh != '\n') countline++; printf("The number of characters in the the report are %d\n", countchar); return 1; }//countchars /*================countsents==========================*/ int countSents() { int curch; int sentences = 0; while ((curch = fgetc(spReport)) != EOF) { if (curch == '.') sentences++; }//while printf("The number of sentances are %d.\n", sentences); return 1; }//countSents



LinkBack URL
About LinkBacks



