Problems Outputting File Data To Text File

This is a discussion on Problems Outputting File Data To Text File within the C Programming forums, part of the General Programming Boards category; I'm having problems outputting the information inside case 2, this piece of code here have the information show up in ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    5

    Problems Outputting File Data To Text File

    I'm having problems outputting the information inside case 2, this piece of code here have the information show up in the txt file.
    Code:
    for (i=0;i<26;i++){//Displaying The Letter/Number Comination in Alphabetical Order
                    printf("%c %d\n", codeLetter[i],codeNumber[i]);
    Heres everything
    Code:
    #include <stdio.h>
    #include "string.h"//Includes
    
    
    void addCode(char, int,char[],int[]);//Prototypes
    int getLetterIndex(char letter);
    char letterToUpper(char letter);
    
    
    
    
    int main (void)//Beginning Main Function
    {
    
    
        int input=0;//Declaring Variables
        char codeLetter[26] = {'\0'};
        int codeNumber[26]={0};
        char temp;
        int tempNumber;
        int index = 0;
        int i;
        int quit=1;
        int pass; 
        char letter;
    
    
    
    
        while (quit != 0){//Start While Loop
            printf("************************************************************\n");//Menu
            printf("'1' = Enter CODE/UNIT Combination\n'2' = Display The Contents of ALL CODE/UNIT Values Entered\n");
            printf ("'3' = Look Up Single Code\n'4' = Quit");
            printf("\n************************************************************\n");
            scanf("%d", &input);
    
    
            switch (input){//beginning switch
            case 1:
                fflush(stdin);
                printf("Please Enter CODE/UNIT Combination\n");
                scanf ("%c%d", &temp,&tempNumber);
                addCode(temp, tempNumber, codeLetter, codeNumber);//function
    
    
                break;
    
    
            case 2: 
                  
                for (i=0;i<26;i++){//Displaying The Letter/Number Comination in Alphabetical Order
                    printf("%c %d\n", codeLetter[i],codeNumber[i]);
                
        FILE * Output;
        Output = fopen("Output File.txt", "w");
        fprintf(Output, "%c %d", codeLetter[i],codeNumber[i]);
        fclose(Output);
        }//end for loop
                break;
            case 3:
                fflush(stdin);//Clearing Input Data
                
                printf("Please Enter The Letter You Want To Look Up\n");
                scanf ("%c", &letter);
                letter=letterToUpper(letter);//Forcing Letter Variable To Uppercase
                index=getLetterIndex(letter);
                printf("%c%d\n", letter, codeNumber[index]);//Displaying Letter and Number
                break;
            case 4:
                printf ("\nGoodbye\n");
                quit = 0;
                break;
    
    
            default:
    
    
                printf("\nThat is not a valid entry:\n"); 
    
    
    
    
            }//End Switch
            system ("pause");
        system ("cls");//Clears Screen
        
        }//end do while
    
    
    
    
        system ("pause");
        return 0;
    
    
    
    
    } 
    
    
    void addCode(char toAdd, int numAdd, char letter[],int numbers[]){//Function 1
        int index;
    
    
        toAdd=letterToUpper(toAdd);
        index = getLetterIndex(toAdd);
        letter[index]=toAdd;
    
    
        numbers[index]+=numAdd;
    
    
    }
    char letterToUpper(char letter){//Function 2
    
    
        if(letter>90){
            letter=letter-32;
        }
        return letter;
    
    
    }
    int getLetterIndex(char letter){//Function 3
        int index;
        letter=letterToUpper(letter);
        index=letter-65;
        return index;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,559
    Some issues first:


    We need more information to debug this properly. What input are you giving it? What incorrect output do you get? What correct output should you get? You may be having trouble because scanf can pick up newlines and space characters with the %c specifier if you don't tell it to skip them, e.g. scanf(" %c"...). Note the space before the %c.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-25-2011, 05:54 PM
  2. outputting a variable into a text file
    By Will_rookwood in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2006, 08:49 AM
  3. Outputting to a File Modified Text
    By alpha in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2003, 07:39 PM
  4. Replies: 1
    Last Post: 10-30-2002, 04:45 AM
  5. create a text file with data using text editor
    By fried egg in forum C Programming
    Replies: 3
    Last Post: 03-14-2002, 08:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21