Cannot Pass String to Main to Print

This is a discussion on Cannot Pass String to Main to Print within the C Programming forums, part of the General Programming Boards category; Hi guys, I read the FAQs and searched and could not find anything I think about this problem. If I ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    Cannot Pass String to Main to Print

    Hi guys, I read the FAQs and searched and could not find anything I think about this problem. If I did please forgive me and send me a link to it

    Basically I am trying to enter an address into the program for which it will save into a file (I chose a csv) then when reran the program is suppose to print the content. Problem is when I print the string sAddress after I enter it it comes out correct (123 Elm Street). When I reenter the program and call it from the saved file I get "123" as the address and "Elm" as the phone number.

    How can I make it so that I get the whole string in the address display?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void newEntry (char fName[20], char lName[20], char sAddress[35], char pNumber[15]) {
    
        FILE *pWrite;
        FILE *pRead;
        printf("Enter First Name: ");
        scanf("%s", fName);
        printf("Enter Last Name: ");
        scanf("%s", lName);
        fflush(stdin);
        printf("Enter Address: ");
        gets(sAddress);
        printf("Enter Phone Number (9 Digits, No Symbols): ");
        scanf("%s", pNumber);
        printf("%s", sAddress);
        pWrite = fopen("address.csv", "a");
        
        if ( pWrite != NULL ) {
             fprintf(pWrite, "%s %s %s %s", fName, lName, sAddress, pNumber);
             fclose(pWrite);
        } 
        else
            printf("\nFile Cannot Be Opened\n");     
    }
    
    
    int main (void) {
      //variable declarations
      int selection;
          char fName[20] = {'\0'};
        char lName[20] = {'\0'};
        char sAddress[35] = {'\0'};
        char pNumber[15] = {'\0'};
          FILE *pWrite;
        FILE *pRead;
      //variable initialization
    
      // Get selection
      printf("Please Enter Selection: ");
      printf("\n1. New Entry: ");
      printf("\n2. Display Entries: ");
      scanf("%d", &selection);
      
      if ( selection == 1 ) {
         newEntry (fName, lName, sAddress, pNumber);
    }
      else if ( selection == 2 ) {
           pRead = fopen("address.csv", "r");
           
           if ( pRead != NULL ) {
                printf("\n Address Book\n");
                
                while ( !feof(pRead) ) {
                      fscanf(pRead, "%s %s %s %s", fName, lName, sAddress, pNumber);
                      
                      if ( !feof(pRead) )
                         printf("\n%s %s %s %s", fName, lName, sAddress, pNumber);
                }
                printf("\n");
           }
      else
          printf("\nFile Not Opened\n");
    }
    else
        printf("\nInvalid Selection\n");
    
                      
        system("pause");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i will continue reading the code, but you must have missed this in the FAQ.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    324
    1) Delete any old datafiles you created from previous program runs
    2) Re-compile with debugging symbols ON
    3) Set a breakpoint at main()
    3) Run the debugger
    4) Check questionable variables
    5) Examine the datafile after the run
    6) Make sure everything is what you expect it to be
    7) Don't expect me to do all that stuff for you.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    I actually read that FAQ off of another post I found while searching, I just did not clear that line of code from my program.

    This IS a homework assignment and I am not looking for it to be rewritten by anyone (I saw that FAQ too ), I just am having a problem with strings after they get put into a text (or csv) file then called to be displayed (only first word printing).

    My workaround that I am doing is making the user use underscores instead of spaces but that is not what I want to do, although it will work I question how it will get graded.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Quote Originally Posted by MacNilly View Post
    1) Delete any old datafiles you created from previous program runs
    2) Re-compile with debugging symbols ON
    3) Set a breakpoint at main()
    3) Run the debugger
    4) Check questionable variables
    5) Examine the datafile after the run
    6) Make sure everything is what you expect it to be
    7) Don't expect me to do all that stuff for you.
    I think I did what you said correctly. My output file does this
    "john doe 6 oak drive 1231231234"

    So my address would be "6" and Phone Number will be "Oak".

    I added the printf sAddress line before the program terminates on entry and it does display "6 oak drive". But like I said when I reopen I get seperate entries for the address cause of the spaces.

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    324
    You could write each entry to a new line or write each entry using a width parameter equal to the size of your arrays (make a constant for this).

    Otherwise I see no way for scanf to know when the end of the address is reached.

    Putting each entry on a new line probably the easiest solution.

    If you must have each entry on a single line, you'll have to somehow set the delimiter to a tab char instead of a single space. Alas, I cannot help you here.
    Last edited by MacNilly; 07-09-2007 at 11:21 PM.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Thanks for the help. I will have to find a way to make the strings on different lines I guess.

    Thanks again!

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    ...
        FILE *pWrite;
        FILE *pRead;unused
    ...
        fflush(stdin); faq
        printf("Enter Address: ");
        gets(sAddress); faq
    ...
          FILE *pWrite;unused

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Quote Originally Posted by robwhit View Post
    Code:
    ...
        FILE *pWrite;
        FILE *pRead;unused
    ...
        fflush(stdin); faq
        printf("Enter Address: ");
        gets(sAddress); faq
    ...
          FILE *pWrite;unused
    I took out the unused declarations which was just as a result of me cutting and pasting everywhere panicing.

    I also removed the flush and went back to the fgets(sAddress, 35, stdin);
    Do I use fgets with a scanf? If I dont my program just goes to the enter phone number part.

  10. #10
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    324
    Ok more suggestions for you:

    1) Don't mix input functions.
    2) Use fprintf and fscanf
    3) Google "man fprintf" and "man fscanf"
    4) Learn about field width specifiers

    This way you can include all data for one entry on one line in your file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Replies: 8
    Last Post: 03-31-2006, 07:15 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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