Thread: C Homework Program Problem

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    C Homework Program Problem

    Here is my code so far: (this is by no means a complete program)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAX 81
    
    
    FILE *fileOpen(char fileName[]);
    int readFile(FILE* inputFile);
    int checkCreditCard();
    
    int digitsOnly(char *strng);
    int checkLastDigit(char *strng);
    
    
    int main (void)
    {
    
    FILE *inputFile;
    
    char fileName[MAX];
    
    
    inputFile = fileOpen(fileName);
    readFile(inputFile);
    
    fclose(inputFile);
    return 0;
    }
    
    FILE * fileOpen(char fileName[]) //function A
    {
    FILE *inputFile;
    
    
           printf("Please enter a file name.\n");
           scanf("%s", fileName);
    
    
    
            inputFile = fopen(fileName, "r");
            if (!inputFile){
            printf("\aCould not open the input file.\n");
    
            exit (1);
           }
    
           return inputFile;
    
    }
    int readFile(FILE* inputFile) //function B
    {
    
     char strng[MAX];
     int len;
    
           fgets(strng, sizeof(strng), inputFile);
           len = strlen(strng);
           if (strng[len - 1] == '\n'){
                   strng[len - 1] = '\0';
           }
           while(fgets(strng, sizeof(strng), inputFile)){
    
               fgets(strng, len, stdin);
    
           }
           return 0;
    
    
    
    }
    


    -The issue is that when I run it and type in a file name and press enter, it just goes into the next line continuously, never exits the program just keeps going to the next line and looking for input. Hope that makes sense. Please help me out! I don't see the problem.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Code:
    fgets(strng, len, stdin);
    What did you expected it to do? "stdin" is the input buffer.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Thank you! I changed stdin to inputFile and it works! THANK YOU!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help with a Simple Program (I'm new to C++)
    By ambellina in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2011, 11:55 AM
  2. need help with a Homework program on polynomials
    By m&m in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2010, 12:42 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Help with program homework!!!
    By skitt12 in forum C++ Programming
    Replies: 4
    Last Post: 10-09-2002, 04:59 PM
  5. Help Me For My Homework Program
    By paul16 in forum C Programming
    Replies: 8
    Last Post: 10-10-2001, 06:43 PM