Why is this not printing to screen?

This is a discussion on Why is this not printing to screen? within the C Programming forums, part of the General Programming Boards category; this is my code: Code: #include <stdio.h> #include <stdlib.h> #include<math.h> int main (void) { int n; FILE * input; input ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    33

    Why is this not printing to screen?

    this is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<math.h>
    
    int main (void)
    {
    	int n;
    	FILE * input;
    
    	input = fopen("input.txt", "r");
    	fscanf(input, "%d", &n);
    
    
    
    	 if(n == 2)
                       printf("%d is prime", n);
              else if(n % 2 == 0 || n < 2)
                       printf("no, %d is prime!", &n);
              else
              {
                       int x;
                       for(x = 0; x < (int)sqrt((double)n); x++)
                                    if(n % x == 0)
                                    {
                                             printf("no, %d is not prime", &n);
                                             return;
                                    }
                       printf("yes, %d is prime!", &n);
    		  }
    
    
    	if(n % 7 == 0)
    		printf("Yes, %d is a multiple of 7",&n);
    	if(n % 11 == 0)
    		printf("Yes, %d is a multiple of 11",&n);
    	if(n % 13 == 0)
    		printf("Yes, %d is a multiple of 13",&n);
    	else
    		printf("No, %d is not a multiple of 7, 11, or 13",&n);
    
    
    	if(n % 2 == 0)
    		printf("%d is even", &n);
    	else 
    		printf("%d is odd", &n);
    
    
    	fclose(input);
    	
    
    	return 0;
    
    
    }

    Let me know what you think thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You failed to initialize n with a value?

    You aren't checking that the file was opened at all, and you don't check fscanf() actually put a value into n. fopen and fscanf both have return values that should be checked.

    You need to work the logic into loops. It's OK to explicitly test a few values, but after that, have it all done within the loops.
    Last edited by Adak; 03-07-2011 at 02:28 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Read printf man page. You don't need &.
    printf is usually line buffered. You need to either print newline char or use fflush.
    Last edited by Bayint Naung; 03-07-2011 at 02:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 03-02-2011, 06:35 PM
  2. Faster way of printing to the screen
    By cacophonix in forum C Programming
    Replies: 16
    Last Post: 02-04-2009, 12:18 PM
  3. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 03:56 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 09:18 PM
  5. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM

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