whats wrong with the short code

This is a discussion on whats wrong with the short code within the C Programming forums, part of the General Programming Boards category; Hi all this is Alan, I am new to be here, and also new to learn C language. I met ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    whats wrong with the short code

    Hi all
    this is Alan, I am new to be here, and also new to learn C language.
    I met a little problem that did not fixed yet, can u help me ?

    the code below:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        	FILE *fp;
        	int num[100],i;
    	int orinArray[45]={0};
    	
    	//make sure txt file existence
        	if((fp = fopen("L1.txt","r"))==NULL)
        	{
            	printf("Error!");
            	exit(1);
        	}
    	//read data from the txt file
        	for(i=0;i<100;i++)
    	{
            	fscanf(fp,"%d",&num[i]);
    		switch(num[i])
    		{
    			case 1: {orinArray[0]++; break;}
    			case 2:	{orinArray[1]++; break;}
    			case 3:	{orinArray[2]++; break;}
    			case 4:	{orinArray[3]++; break;}
    			case 5: {orinArray[4]++; break;}
    			case 6: {orinArray[5]++; break;}
    			case 7: {orinArray[6]++; break;}
    			case 8: {orinArray[7]++; break;}
    			case 9: {orinArray[8]++; break;}
    			case 10: {orinArray[9]++; break;} 
    			case 11: {orinArray[10]++; break;} 
    			case 12: {orinArray[11]++; break;}
    			case 13: {orinArray[12]++; break;}
    			case 14: {orinArray[13]++; break;}
    			case 15: {orinArray[14]++; break;}
    			case 16: {orinArray[15]++; break;}
    			case 17: {orinArray[16]++; break;}
    			case 18: {orinArray[17]++; break;}
    			case 19: {orinArray[18]++; break;}
    			case 20: {orinArray[19]++; break;}
    			case 21: {orinArray[20]++; break;}
    			case 22: {orinArray[21]++; break;}
    			case 23: {orinArray[22]++; break;}
    			case 24: {orinArray[23]++; break;}
    			case 25: {orinArray[24]++; break;}
    			case 26: {orinArray[25]++; break;}
    			case 27: {orinArray[26]++; break;}
    			case 28: {orinArray[27]++; break;}
    			case 29: {orinArray[28]++; break;}
    			case 30: {orinArray[29]++; break;}
    			case 31: {orinArray[30]++; break;}
    			case 32: {orinArray[31]++; break;}
    			case 33: {orinArray[32]++; break;}
    			case 34: {orinArray[33]++; break;}
    			case 35: {orinArray[34]++; break;}
    			case 36: {orinArray[35]++; break;}
    			case 37: {orinArray[36]++; break;}
    			case 38: {orinArray[37]++; break;}
    			case 39: {orinArray[38]++; break;}
    			case 40: {orinArray[39]++; break;}
    			case 41: {orinArray[40]++; break;}
    			case 42: {orinArray[41]++; break;}
    			case 43: {orinArray[42]++; break;}
    			case 44: {orinArray[43]++; break;}
    			case 45: {orinArray[44]++; break;}
    			default: break;
    		}
    		
        	}
       	for(i=0;i<45;i++)
            	printf("%d ",orinArray[i]);
        	printf("\n");
    
        	return 0;
    }
    the code purpose is calculating the times of integer number shown in text file in the range 1-45.
    the answer should be fourty five 1, but the actual answer is not, the error should be in switch case statement. because this code can read all number and output.

    the text file data is the numbers from 1-45 like this:
    1 2 3 4 5 6 7 8 ... 44 45


    cheers
    Alan

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    You didn't explain your problem very well; please explain what exactly you expect to happen, and what exactly happens; perhaps by copying and pasting the actual text file and the output.

    On a side note:
    Code:
    case 1: {orinArray[0]++; break;}
    case 2:	{orinArray[1]++; break;}
    case 3:	{orinArray[2]++; break;}
    do you not see the pattern here?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Greetings Alan! Wow, you've actually indented/formatted your code and used code tags... well, you're already ahead of the pack. Congratulations!

    Now, are there guaranteed to be at least 100 values in the file? Also, the whole switch-case statement can be removed and replaced:
    Code:
    if( num[i] >= 1 && num[i] <= 45 )
    {
        orinArray[num[i]-1]++;
    }
    Maybe you can post the input file as an attachment. Also, you can try checking the return value of the fscanf call to make sure it returns 1.
    Last edited by hk_mp5kpdw; 08-12-2010 at 08:02 AM. Reason: Replaced and with &&... too much PL/SQL
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    22
    Quote Originally Posted by szp_895 View Post
    Hi all

    the text file data is the numbers from 1-45 like this:
    1 2 3 4 5 6 7 8 ... 44 45

    cheers
    Alan

    check that your text file has correct input. It should print out 45 1's.

    It's good practice to also close the file pointer at the end:

    fclose(fp);
    Last edited by dnj23; 08-12-2010 at 08:19 AM.

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Hey there, wecolme to the forum.
    I looked at your code and noticed that you wrote: int num[100]. So your num is 100 elements, but your data is only 45 elements long. For i>44 , num[i] will take whatever values there are in the memory. Some of them will fall into the range of [1,45] some will be outside. So you can either limits num[] to 45 elements, or only run your first 'for' loop to i<45.
    Last edited by nimitzhunter; 08-12-2010 at 02:01 PM.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,485
    I just read today in some C/C++ tips page that fscanf should normally be replaced by fgets followed by sscanf. This might be a case where that will help fix the problems if you are getting a lot of 0 counts.

    Tim S.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by rags_to_riches View Post
    On a side note:
    Code:
    case 1: {orinArray[0]++; break;}
    case 2:	{orinArray[1]++; break;}
    case 3:	{orinArray[2]++; break;}
    do you not see the pattern here?
    Or put another way: Would you do the same thing if there were 10000 possible values?
    Learn about the One, Two, Many rule
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 02:55 AM
  2. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM
  3. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  4. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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