Problem with scanf, strings and structure

This is a discussion on Problem with scanf, strings and structure within the C Programming forums, part of the General Programming Boards category; I am trying to create a gift exchange list generator, but for whatever reason this won't compile. My uncle who ...

  1. #1
    Registered User teng40z's Avatar
    Join Date
    Dec 2010
    Posts
    2

    Problem with scanf, strings and structure

    I am trying to create a gift exchange list generator, but for whatever reason this won't compile. My uncle who programs for a living was unable to find a problem with it. Here is what I got at the moment..


    Code:
    #include <stdio.h>			
    #include <stdlib.h>
    #include <string.h>
    
    
    int main (int argc, const char * argv[]) {
    	
    	
    	int NumPeople;
    	int i;
    	int RandNum;
    
    	typedef struct _Names
    	{
    		char fromname[20];
    		char toname[20];
    		
    	} Names[100];
    	
        printf("How many people you would like on your list.\n");
    	scanf("%d", &NumPeople);
    	
    	if ( NumPeople > sizeof(Names)) 
    	{
    		printf("cannot have more than %d entries\n", sizeof(Names) );
    		return 20;
    	}
    	
    	for (i = 1; i <= NumPeople; i++)					
    	{
    		printf("Enter name number %d.\n", i);
    		scanf("%s", Names[i].fromname );
    	}
    	
    	for (i = 0; i <= NumPeople; i++) 
    	{
    		RandNum = rand() % NumPeople + 1;
    		if ( 0 != strcmp(Names[i].fromname, Names[RandNum].fromname ))
    		{
    			strcpy( Names[i].toname, Names[RandNum].fromname);
    		}
    	}
    	for (i = 0; i <= NumPeople; i++) 
    	{
    		printf("%s -> %s\n", Names[i].fromname, Names[i].toname);
        }
    	return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > My uncle who programs for a living was unable to find a problem with it.
    Programs what?


    The first problem is your typedef. You have a typedef called Names, not an array called Names.

    If you just remove the word 'typedef', then you should be OK on that point.

    The other problem is your use of sizeof to measure the size of an array.
    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.

  3. #3
    Registered User teng40z's Avatar
    Join Date
    Dec 2010
    Posts
    2
    He writes programs for computers using c and java. He works for a major car company.

    Thanks for the reply. I guess he had a few to many drinks tonight and missed that one. I am new to using structures and need to figure out exactly what is happening.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Array of strings in C Structure
    By Areal Person in forum C Programming
    Replies: 5
    Last Post: 05-29-2010, 07:27 PM
  2. Char array strings problem
    By towed in forum C Programming
    Replies: 1
    Last Post: 02-12-2010, 01:59 PM
  3. Problem with strings in Linked List
    By hellogamesmaste in forum C Programming
    Replies: 10
    Last Post: 08-23-2009, 10:06 AM
  4. Problem with seperating strings with delimiters..
    By hykyit in forum C Programming
    Replies: 2
    Last Post: 03-29-2006, 05:51 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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