Splitting long line using two fprintf doesn't display proper output

This is a discussion on Splitting long line using two fprintf doesn't display proper output within the C Programming forums, part of the General Programming Boards category; The program writes a number to a text file correctly if it is a prime number, but it doesn't if ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Splitting long line using two fprintf doesn't display proper output

    The program writes a number to a text file correctly if it is a prime number, but it doesn't if the number is not prime. Well, it knows that it is not prime, but it doesn't display the correct highest divisor, always just 0.
    I think it has to do that I used fprintf twice, because it does work if I put it all on one line, but then it exceeds the page margins.
    Would anyone know a way around this problem?

    Code:
    #define LinesPerScreen 12
    
    #include <stdio.h>
    #include <process.h>      
    #include <conio.h>        
    
    int write_prime_numbers();
    int read_prime_numbers();
    
    int main()
    {
       int reply = 0;
       int items = 3;
    
       clrscr();
       printf("Welcome to PRIME NUMBER program");
       while (reply < items)
       {
          printf("\n\nChoose from the following menu: ");
          printf("\nSelect %d to write data to a text disk file", 1);
          printf("\nSelect %d to read data from a text disk file", 2);
          printf("\nSelect %d or a higher number to exit from the program", items);
          printf("\nYour choice: ");
          scanf("%d", &reply);
    
          if (reply == 1)
          {
             write_prime_numbers();
          }
          if (reply == 2)
          {
             read_prime_numbers();
          }
          if (reply < 1)
          {
             printf("\nChoice %d out of range. Type in 1 or 2 or a higher number.");
          }
       }
       printf ("\n\nThank you for using this program.\n");
       getch();
       return 0;
    }
    
    int write_prime_numbers()
    {
       FILE *write_prime_file;
       int number, i, remainder;
       int not_divisible = 1;
       int biggest = 1;
       char reply;
       int rec_no = 0;
       write_prime_file = fopen ("prime_io.txt", "w");
       fprintf(write_prime_file, "This is the content of your text file.\n");
       fprintf(write_prime_file, "-1 indicates user typed in terminating value.\n");
       while ( number != -1)
       {
          printf("\n\nCollecting data for the file Record Number %d\n", rec_no);
          printf("\nEnter a number to see if its prime(e.g. 1990) OR\n");
          printf("Enter -1 to quit writing to a text file and return to the main");
          printf(" menu\n");
          printf("Your choice: ");
          scanf("%d", &number);
             for( i = 2; i < number - 1; i++ )
             {
                remainder = number % i;
                if(remainder == 0 )
                {
                   not_divisible = 0;
                   biggest = i;
                }
             }
    
             if(not_divisible == 1)
             {
             fprintf(write_prime_file, "\nThe number %d is a prime number", number);
             }
             else
             {
           fprintf(write_prime_file, "\nThis number's highest divisor is %d");
             fprintf(write_prime_file, " and thus is not prime", biggest);
             }
       }
    
       fclose(write_prime_file);
       return 0;
    }
    
    int read_prime_numbers()
    {
       FILE *read_prime_file;
       char content_in[60];
       int rec_no = 0;
       printf ("\nInside of read a record function\n");
       read_prime_file = fopen ("prime_io.txt", "r");
       while (!feof(read_prime_file))
       {
          fgets (content_in, 60, read_prime_file);
          printf (content_in);
          if (rec_no >= LinesPerScreen)
          {
             printf ("\n");
             getch();
             return 0;
          }
       }
       fclose(read_prime_file);
    }

  2. #2
    Banned
    Join Date
    Aug 2004
    Posts
    128
    Code:
    fprintf(write_prime_file, "\nThis number's highest divisor is %d");
    fprintf(write_prime_file, " and thus is not prime", biggest);

    it can go past the page margins without a problem
    you can write your whole program in one line,
    the c/c++ compiler will not care, but you could try this

    Code:
    fprintf(write_prime_file, "\nThis number's highest divisor is %d", biggest);
    fprintf(write_prime_file, " and thus is not prime");
    I did not compile your code or check to see if that fixed your
    problems its just an item i noticed, if your still having a problem
    i may compile your code and see what me thinks!

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Yes, thanks, this worked, though in Borland C++ Builder 6, it still exceeds the margin by 5 characters.

    Another problem I have though is, when the user enters the "sentinal value"(-1), it is also written to the txt file, and the program prints the same output for this value as the last "proper" value entered in. So when I entered in 8 as my last value, it doesn't just display 4 is the highest divisor for value 8, but also for -1.

    I tried using something like, would you like to enter another number? and the user had to enter 'n' || 'N' or 'y' || 'Y' but when the user entered in any other letter than n or N, the program would prompt for a new number anyway.

    Would you know any better methods?

  4. #4
    Banned
    Join Date
    Aug 2004
    Posts
    128
    Code:
    fprintf(write_prime_file, "This is the content of your text file.\n");
    fprintf(write_prime_file, "-1 indicates user typed in terminating value.\n");
    im not sure but i think you ment for this to be :

    Code:
    printf("This is the content of your text file.\n");
    printf("-1 indicates user typed in terminating value.\n");

    as for it exceeding the margin, you could move more of the first
    content from the first fprintf, to the 2nd fprintf, but if you move
    then %d, make sure you mvoe your variable to the 2nd fprintf.


    for the problem with prompting for a new number, perhaps
    you could simply :

    Code:
    bool quit = false;
    while(quit == false)
    {
         int mynumber;
         cout << "Please enter a number? :  ";
         cin >> mynumber;
    
         //do stuff here
    
    
    
         char newnumber;
         cout << "Would you like to enter a new number?  : ";
         cin >> newnumber;
    
         if(newnumber == 'n' || newnumber == 'N')
         {
                quit == true;
         }
    }
    return 0;
    Last edited by JarJarBinks; 10-10-2004 at 12:28 AM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    You can also do this, since the compiler will join adjacent strings for you.
    Code:
    fprintf(write_prime_file, "This number's highest divisor is %d"
                              " and thus is not prime\n", biggest);
    By the way, put the newline at the end, not the start of the line.
    Otherwise your file ends up with a blank line at the start, and no newline at the end.

    > Another problem I have though is, when the user enters the "sentinal value"(-1), it is also written to the txt file,
    Check the value you input then
    Code:
    scanf("%d", &number);
    if ( number == -1 ) break;
    > while (!feof(read_prime_file)) {
    > fgets (content_in, 60, read_prime_file);
    > printf (content_in);
    Bad for two reasons
    1. The feof() is discussed in the FAQ
    2. NEVER call printf with a control string you read from the outside world. All anyone has to do is put a % character in there and then your code is off converting non-existent parameters.
    Code:
       while (fgets (content_in, sizeof content_in, read_prime_file)) {
          fputs(content_in,stdout);
    Using sizeof instead of the literal value 60 means you have one less thing to change if you decide on a different buffer size.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 08:17 PM
  2. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  3. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM
  4. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 05:21 PM
  5. How to display 23 bytes long double answer in C?
    By CLIE_ZETA in forum C Programming
    Replies: 3
    Last Post: 11-18-2001, 11:11 AM

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