Thread: Question

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    6

    Question Counting positive and negative numbers from a file

    Hey guys,
    I am stuck on a question in my C Programming Book and i was wondering if you guys can help anyways...

    I need to write a program using a while/do-while/for loop command that counts positive and negative numbers in a list of integers from a file

    eg.
    5 (total number of integers in the list)
    2
    -3
    4
    7
    -9

    Ok I have...
    Code:
    	int a, b, c, d, e, numbers;
    	int positives, negatives;
    	FILE *input;
    	
    	input=fopen ("C:\\hw4-1in.dat.txt.", "r");
    	fscanf(input, "\r%d\n", &numbers);
    	printf(	"%d", numbers);
    	fscanf(input, "%d\n", &a);
    	printf(	"%d", a);
    	fscanf(input, "%d\n", &b);
    	printf(	"%d", b);
    	fscanf(input, "%d\n", &c);
    	printf(	"%d", c);
    	fscanf(input, "%d\n", &d);
    	printf(	"%d", d);
    	fscanf(input, "%d\n", &e);
    	printf(	"%d", e);
    
    	
    	
    	fclose(input);
    
    
    
    	if (a>0)
    		{
    		positives=1+positives;
    		}
    	else if (a<0)
    		{
    		negatives=1+negatives;
    		}
    	
    
    	if (b>0)
    		{
    		positives=1+positives;
    		}
    	else if (b<0)
    		{
    		negatives=1+negatives;
    		}
    		
    
    	if (c>0)
    		{
    		positives=1+positives;
    		}
    	else if (c<0)
    		{
    		negatives=1+negatives;
    		}
    
    
    	if (d>0)
    		{
    		positives=1+positives;
    		}
    	else if (d<0)
    		{
    		negatives=1+negatives;
    		}
    
    
    	if (e>0)
    		{
    		positives=1+positives;
    		}
    	else if (e<0)
    		{
    		negatives=1+negatives;
    		}
    
    printf ("The total number of the positive integers is \n %d", positives);
    printf ("The total number of the negative integers is \n %d", negatives);
    
    }

    It keep saying windows encountered a problem when i run it...

    Any help would be appreciated thanks!!
    Last edited by Detrigan; 10-19-2003 at 05:48 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What part are you stuck on? Post what you're tried so far, so we can see where you're up to.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    If that's all of your code then it looks like you're miising your 'main' function, which is what most of your code should probably be in, and is defined as an int. Take a look at some of the other examples in your programming book.
    Demonographic rhinology is not the only possible outcome, but why take the chance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM