Thread: Program crashes, why?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    11

    Program crashes, why?

    I'm writing a program to read in user intered values for a country (read into a 2D character array) and 2 different types of values (into a 2D float array) and calculate a third value. I wrote out my code and it compiles fine and whatnot, but when I run it, it crashes after the first entered float value.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #define NCHARS 15
    #define NCOLS 3
    #define MAXCOUNTRY 10
    
    int collectdata(char country[][NCHARS], float stats[][NCOLS]);
    void printdata(char country[][NCHARS], float stats[][NCOLS], int count);
    void printheader();
    
    int main()
    {
    	int count;
    	float stats[MAXCOUNTRY][NCOLS];
    	char country[MAXCOUNTRY][NCHARS];
    	
    	count = collectdata(country, stats);
    	printdata(country, stats, count);
    	
    	return 0;
    	
    }
    
    int collectdata(char country[MAXCOUNTRY][NCHARS], float stats[MAXCOUNTRY][NCOLS])
    {
    	char ans;
    	int count=0;
    	
    	ans = 'Y';
    	
    	while (ans != 'N' && toupper(ans) != 'N')
    	{
    		printf("Enter the name of the country:");
    		gets(country[count]);
    		printf("Enter the number of units:");
    		scanf("%f", stats[count][0]);
    		printf("Enter the MWe:");
    		scanf("%f", stats[count][1]);
    		
    		stats[count][2] = (stats[count][1])/(stats[count][0]);
    		
    		printf("\nDo you want to enter another country? (Y or N)");
    		scanf(" %c", ans);
    		count++;
    	}
    	
    	return count;
    }
    
    void printdata(char country[MAXCOUNTRY][NCHARS], float stats[MAXCOUNTRY][NCOLS], int count)
    {
    	int i;
    	
    	printheader();
    	
    	for (i=0; i<count; i++)
    	{
    			printf("%s %4f %4f %4f", country[i], stats[i][0], stats[i][1], stats[i][2]);
    	}
    	
    	
    }
    
    void printheader()
    {
    	printf("World Nuclear Power Generation \n");
    	printf("      As of January 2007       \n");
    	printf("Country     Units  Total  MWe  \n");
    }
    so I'm assuming its something wrong with the collectdata function, but I can't figure it out - anyone wanna give me any tips/point it out for me?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Mahdi123 View Post
    Code:
    scanf("%f", stats[count][0]);
    Should be:

    Code:
    scanf("%f", &stats[count][0]);
    And the same problem here:

    Code:
    scanf(" %c", &ans);
    Also, you use gets(), which is bad, but forgivable. Check the FAQ, switch to fgets().

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    Breakpoint 1, main () at try.c:18
    18 count = collectdata(country, stats);
    (gdb) next
    Enter the name of the country:hello
    Enter the number of units:3

    Program received signal SIGSEGV, Segmentation fault.
    0xff2a9818 in __inrange_single () from /usr/lib/libc.so.1
    (gdb) where
    #0 0xff2a9818 in __inrange_single () from /usr/lib/libc.so.1
    #1 0xff2aa384 in decimal_to_single () from /usr/lib/libc.so.1
    #2 0xff3102c4 in number () from /usr/lib/libc.so.1
    #3 0xff30ffc4 in __doscan_u () from /usr/lib/libc.so.1
    #4 0xff30f6a0 in _doscan () from /usr/lib/libc.so.1
    #5 0xff315804 in vscanf () from /usr/lib/libc.so.1
    #6 0xff3145fc in scanf () from /usr/lib/libc.so.1
    #7 0x000107e4 in collectdata (country=0xffbffc20, stats=0xffbffcb8)
    at try.c:37
    #8 0x000106cc in main () at try.c:18
    (gdb)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > gets(country[count]);
    See the FAQ as to why gets() is evil.

    > while (ans != 'N' && toupper(ans) != 'N')
    Can't you simplify this to
    while ( toupper(ans) != 'N')

    > scanf(" %c", ans);
    You forgot the all important &
    If you're using gcc, then add these flags to your compiler command line
    -W -Wall -ansi -pedantic
    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.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    11
    ahhh ok thanks, I knew it would be something stupid like that I would forget.

    Also, gets() is just what they told me to use in class, but I shall promptly inform myself otherwise.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Mahdi123 View Post
    Also, gets() is just what they told me to use in class, but I shall promptly inform myself otherwise.
    Excellent. It's not your fault, by the way. If an instructor tells you to use something, you're not going to second guess him. The problem is that people who should know better, are telling students that they should use gets(). These people need at least a stern talking to, more preferably a couple of slaps with a two-by-four.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple login program - Crashes :(
    By spadez in forum C Programming
    Replies: 1
    Last Post: 03-23-2009, 04:16 PM
  2. Replies: 3
    Last Post: 02-29-2008, 01:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. program crashes on closure.
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2005, 04:55 PM
  5. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-28-2002, 12:28 AM