Thread: Getting a while/if loop to work with struct

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    10

    Getting a while/if loop to work with struct

    I want to get an if or while loop in this struct to keep recording data until terminated. Here's my code it keeps freezing after the first input session is completed.
    Code:
      #include <stdio.h>
    
    
      /* Define our account structure */
      struct account {
    
    
       int invoice_number;
       char *company_name;
       int invoice_date;
       float invoice_amount;
       
       };
    
    
    
    
      /* Define the variables that use the account structure type */
    
    
      struct account ledger;
    
    
    
    
      int main(void)
    
    
      {
        char *q;
        int foef;
        struct account ledger;
        FILE *newacct;
        newacct = fopen("robco.txt", "a+");
    
    
        printf("Enter Account Name:");
        scanf("%s", &ledger.company_name);
        printf("Enter Incoive Number:");
        scanf("%d", &ledger.invoice_number);
        printf("Enter Invoice Date:");
        scanf("%d", &ledger.invoice_date);
        printf("Enter Invoice Amount:");
        scanf("%f", &ledger.invoice_amount);
        fprintf(newacct,"\n%s %d %d %f", ledger.company_name,   ledger.invoice_number, ledger.invoice_date,     ledger.invoice_amount);
        fclose(newacct);
        
        do
     {
        fscanf(q,"%s %d %d %f", &ledger.company_name,   &ledger.invoice_number, &ledger.invoice_date,   &ledger.invoice_amount);
        printf("%s %d %d %f", ledger.company_name,   ledger.invoice_number, ledger.invoice_date,   ledger.invoice_amount);
     }
        while(!feof(q));
        
       
     }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    feof() is not something I use much; but, I do not think it wants a char pointer!

    I suggest reading the compiler warnings!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    10
    This code works while writing to text, the while loop is just never ending, how could I put an if loop to continue if yes and terminate if no.
    Code:
    #include <stdio.h>
    
    
    /* Define our account structure */
    struct account {
    
    
       int invoice_number;
       char *company_name;
       int invoice_date;
       float invoice_amount;
       
       };
    
    
    
    
    /* Define the variables that use the account structure type */
    
    
    struct account ledger;
    
    
    
    
    int main(void)
    
    
    {
        
        struct account ledger;
        FILE *newacct;
        newacct = fopen("robco.txt", "a+");
    
    
        printf("Enter Account Name:");
        scanf("%s", ledger.company_name);
        printf("Enter Incoive Number:");
        scanf("%d", ledger.invoice_number);
        printf("Enter Invoice Date:");
        scanf("%d", &ledger.invoice_date);
        printf("Enter Invoice Amount:");
        scanf("%f", &ledger.invoice_amount);
        fprintf(newacct,"\n%s %d %d %f", ledger.company_name, ledger.invoice_number, ledger.invoice_date, ledger.invoice_amount);
        fclose(newacct);
    
    
        
       
    }

    how ever with this code I get a never-ending loop.

    Code:
    #include <stdio.h>
    
    
    /* Define our account structure */
    struct account {
       int account_number;
       char *last_name;
       };
    
    
    
    
    /* Define the variables that use the account structure type */
    
    
    struct account ledger;
    
    
    int main(void)
    
    
    {
    	int q;
        struct account ledger;
        FILE *newacct;
        newacct = fopen("robco.txt", "a+");
    
    
        printf("Enter Last Name and Account Number:");
        scanf("%s %d", ledger.last_name, &ledger.account_number);
        fprintf(newacct,"%s %d", ledger.last_name, ledger.account_number);
        fclose(newacct);
        do
    {
    	fscanf(q,"%s %d", ledger.last_name, ledger.account_number);
        printf("%s %d", ledger.last_name, ledger.account_number);
    }
        while(!feof(q));
    }
    Last edited by mp59; 07-20-2018 at 06:34 PM. Reason: addition code

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    19
    Code:
     do{
        fscanf(q,"%s %d", ledger.last_name, ledger.account_number);
        printf("%s %d", ledger.last_name, ledger.account_number);
    }
        while(!feof(q)); // <--------
    }
    Your loop is waiting for an end of file inicator from the given file stream q. Sadly the q variable is not a file stream. Which would never give it an EOF (thus the infinite loop). I would suggest reading into strstr() from string.h to accomplish what you want.
    how could I put an if loop to continue if yes and terminate if no.
    I suggest reading the compiler warnings!
    Also main() should return an int.
    Last edited by Laerehte; 07-20-2018 at 10:07 PM.

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    10
    Quote Originally Posted by Laerehte View Post
    Code:
     do{
        fscanf(q,"%s %d", ledger.last_name, ledger.account_number);
        printf("%s %d", ledger.last_name, ledger.account_number);
    }
        while(!feof(q)); // <--------
    }
    Your loop is waiting for an end of file inicator from the given file stream q. Sadly the q variable is not a file stream. Which would never give it an EOF (thus the infinite loop). I would suggest reading into strstr() from string.h to accomplish what you want.

    Also main() should return an int.
    Right so I plug newacct where q is and it still goes continuously. However I get no warnings in dev +

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,493
    > Right so I plug newacct where q is and it still goes continuously.
    Presumably because you just closed it.

    Perhaps
    Code:
        newacct = fopen("robco.txt", "a+");
        printf("Enter Last Name and Account Number:");
        scanf("%s %d", ledger.last_name, &ledger.account_number);
        fprintf(newacct,"%s %d", ledger.last_name, ledger.account_number);
        fclose(newacct);
     
        newacct = fopen("robco.txt", "r");
        while ( fscanf(newacct, "%s %d", ledger.last_name, &ledger.account_number) == 2 ) {
            printf("%s %d", ledger.last_name, ledger.account_number);
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2018
    Posts
    10
    So I've made it into a switch case, and it works through expect for my first line skips. I used the switch case in a previous program and I like it. I've been at it for a while now so not sure what I am missing something simple i am sure...thanks for everyone help thus far.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    struct account 
    {
        char company_name[256];
        int invoice_number;
        int invoice_date;
        float invoice_amount;
        
    };
    
    
    int main() 
    {
        char *end;
        char buf[256];
        char y;
        char n;
        char a;
        FILE *newacct;
        struct account ledger;
        newacct = fopen("robco.txt", "a");
        
    
    
        printf("Enter Account Name:");
        fgets(ledger.company_name, sizeof(ledger.company_name), stdin);
        ledger.company_name[strlen(ledger.company_name)-1] = '\0';
        printf("Enter Incoive Number:");
        fgets(buf, sizeof(buf), stdin);
        ledger.invoice_number = strtol(buf, &end, 10);
        printf("Enter Invoice Date:");
        fgets(buf, sizeof(buf), stdin);
        ledger.invoice_date = strtol(buf, &end, 10);
        printf("Enter Invoice Amount:");
        fgets(buf, sizeof(buf), stdin);
        ledger.invoice_amount = strtol(buf, &end, 10);
        fprintf(newacct,"%s %d %d %f\n", ledger.company_name, ledger.invoice_number, ledger.invoice_date, ledger.invoice_amount);
    	fclose(newacct);
    
    
    	
    	printf("Would you like to enter more data?:");
        scanf("%s", &a);
    
    
    	switch (a){
    	
    	case'n':
    		
     	printf("Thanks for your entry!"); //should terminate
     	fprintf(newacct,"\n%s %d %d %f", ledger.company_name, ledger.invoice_number, ledger.invoice_date, ledger.invoice_amount);
        fclose(newacct);
        
     	return 0;
     
    	case'y':	
    	FILE *newacct;
        struct account ledger;
        newacct = fopen("robco.txt", "a");
     		
        printf("Enter Account Name:"); //the line that keeps skipping in every switch case.
        fgets(ledger.company_name, sizeof(ledger.company_name), stdin); //it prints however it skips this input line.
        ledger.company_name[strlen(ledger.company_name)-1] = '\0';
        printf("\nEnter Incoive Number:");
        fgets(buf, sizeof(buf), stdin);
        ledger.invoice_number = strtol(buf, &end, 10);
        printf("Enter Invoice Date:");
        fgets(buf, sizeof(buf), stdin);
        ledger.invoice_date = strtol(buf, &end, 10);
        printf("Enter Invoice Amount:");
        fgets(buf, sizeof(buf), stdin);
        ledger.invoice_amount = strtol(buf, &end, 10);
        fprintf(newacct,"%s %d %d %f\n", ledger.company_name, ledger.invoice_number, ledger.invoice_date, ledger.invoice_amount);
    	fclose(newacct);
        
     	break;
    }
    	printf("Would you like to enter more data?:");
        scanf("%s", &a);
    
    
    	switch (a){
    	
    	case'n':
    		
     	printf("Thanks for your entry!"); //should terminate
     	fprintf(newacct,"\n%s %d %d %f", ledger.company_name, ledger.invoice_number, ledger.invoice_date, ledger.invoice_amount);
        fclose(newacct);
        
     	return 0;
     
    	case'y':
    	FILE *newacct;
        struct account ledger;
        newacct = fopen("robco.txt", "a");
     		
        printf("Enter Account Name:");
        fgets(ledger.company_name, sizeof(&ledger.company_name), stdin);
        ledger.company_name[strlen(ledger.company_name)-1] = '\0';
        printf("Enter Incoive Number:");
        fgets(buf, sizeof(buf), stdin);
        ledger.invoice_number = strtol(buf, &end, 10);
        printf("Enter Invoice Date:");
        fgets(buf, sizeof(buf), stdin);
        ledger.invoice_date = strtol(buf, &end, 10);
        printf("Enter Invoice Amount:");
        fgets(buf, sizeof(buf), stdin);
        ledger.invoice_amount = strtol(buf, &end, 10);
        fprintf(newacct,"%s %d %d %f\n", ledger.company_name, ledger.invoice_number, ledger.invoice_date, ledger.invoice_amount);
    	fclose(newacct);
        
     	break;
    }
    }

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,457
    Quote Originally Posted by Laerehte View Post
    Also main() should return an int.
    Although sloppy the code given is fully compliant with the C standard and is returning an int (it returns 0 if you don't explicitly have a return). So, yes, it's sloppy and bad style IMO to omit the return for main but it's not undefined or anything

    ...reaching the } that terminates the main function returns a value of 0.

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,457
    Quote Originally Posted by mp59 View Post
    So I've made it into a switch case, and it works through expect for my first line skips. I used the switch case in a previous program and I like it. I've been at it for a while now so not sure what I am missing something simple i am sure...thanks for everyone help thus far.
    Why have you got return 0 on line 94?

  10. #10
    Registered User
    Join Date
    Jul 2018
    Posts
    10
    Quote Originally Posted by Hodor View Post
    Although sloppy the code given is fully compliant with the C standard and is returning an int (it returns 0 if you don't explicitly have a return). So, yes, it's sloppy and bad style IMO to omit the return for main but it's not undefined or anything
    I will agree it's sloppy I'm still new to it all, plus I've been getting a tad annoyed with it. Whenever I feel I get what I'd like, some small odd problem hits. Right now starting on line 65 and 66 it prints what is the account name however it skips over the input line and doesn't let me input my account name.

    Quote Originally Posted by Hodor View Post
    Why have you got return 0 on line 94?
    I have a return 0 on multiple lines in my case so if you input a 'n' when asked if you'd like to continue input it terminates the program.

  11. #11
    Registered User
    Join Date
    Jul 2018
    Posts
    10
    Solved thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does FILE struct work in stdio.h
    By MedicineMan25 in forum C Programming
    Replies: 9
    Last Post: 04-27-2017, 10:48 PM
  2. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  3. How does this while loop work?
    By johnmerlino in forum C Programming
    Replies: 4
    Last Post: 04-22-2014, 03:19 AM
  4. Replies: 6
    Last Post: 05-03-2013, 12:59 PM
  5. Can't get the loop to work
    By pass_prime in forum C Programming
    Replies: 10
    Last Post: 06-20-2005, 03:46 PM

Tags for this Thread