Thread: Inputing values

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    40

    Angry Inputing values

    ok look i want to input something like this:

    Yellow_Card
    Blue_Dress
    Orange_Hat
    Purple_Noodle
    Green_Fan
    Lovely_Sam
    Cheesy_Freddy
    Marketing_Mark
    Formula_One
    ####

    and the "####" signals the end of what is supposed to be input, i want to store these names in the name field of a structure called Dealers:

    Code:
    typedef struct{
    	char name[21];
    	int num_cars;
    	int num_people;
    	Model quantity[4];
    	Salesperson employees[8];
        } Dealer;
    
    void init_dealers(Dealer *); 
    
    main()
    {
    	int i;
    	Dealer dealers[10];
    	
    	init_dealers(&dealers);
    	
    	for(i=0; i<10; i++)
    		printf("%s", dealers[i].name);
    	
    	
    	return 0;
    }
    
    void init_dealers(Dealer *dealer)
    {
    	int end=1;
    	while(end)
    	{
    		scanf("%s", dealer->name);
    		dealer++;
    		if(strcmp(dealer->name, "####"))
    			end=0;	
    	}	
    }
    whats wrong with my code?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    >init_dealers(&dealers);
    remove the '&'

    And strcmp just return 0 when the strings are the same... so change to if(!strcmp...) or if(strcmp(.....) == 0)
    Last edited by serruya; 05-07-2003 at 12:46 PM.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    ok here's the code now, and the errors that are with it:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct{
            long id;
            float quota;
            float sales_credit;
        } Salesperson;
    
    typedef struct{
            char trim[3];
            char color[21];
            float retail;
            float invoice;
        } Car;
    
    typedef struct{
            char name[21];
            int in_stock;
            Car cars[10];
        } Model;
    
    typedef struct{
            char name[21];
            int num_cars;
            int num_people;
            Model quantity[4];
            Salesperson employees[8];
        } Dealer;
    
    void init_dealers(Dealer *d);
    void stock_car();
    void hire_salesperson();
    void sell_car();
    void find_car();
    void car_match();
    void deliver_car();
    void print_report();
    
    main()
    {
            int i;
            Dealer dealers[10];
    
            init_dealers(dealers);
    
            for(i=0; i<10; i++)
                    printf("%s", dealers.name);
    
            return 0;
    }
    
    void init_dealers(Dealer *d)
    {
            int end=1;
            while(end)
            {
                    scanf("%s", &d->name);
                    dealer++;
                    if(!strcmp(d->name, "####"))
                            end=0;
            }
    }

    Code:
    cc: Error: p7.c, line 60: In this statement, "dealers" has a pointer type, but occur
    s in a context that requires a union or struct. (needstruct)
                    printf("%s", dealers.name);
    -----------------------------^
    cc: Error: p7.c, line 71: In this statement, "dealer" is not declared. (undeclared)
                    dealer++;
    ----------------^

    what is going on here? and what am i missing?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    > for(i=0; i<10; i++)
    printf("%s", dealers.name);
    you want to print the same name always? maybe dealers[i].name?

    >>Dealer *d .... dealer++;
    you know the problem? what the hell is dealer++ it's a type, not the variable, maybe d++?

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    ok i changed that but i dont know how to make it stop once it gets to "####" the code i have now isn't doing that, please can someone help?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    the d++ should be after the checking! not before...
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct{
            long id;
            float quota;
            float sales_credit;
        } Salesperson;
    
    typedef struct{
            char trim[3];
            char color[21];
            float retail;
            float invoice;
        } Car;
    
    typedef struct{
            char name[21];
            int in_stock;
            Car cars[10];
        } Model;
    
    typedef struct{
            char name[21];
            int num_cars;
            int num_people;
            Model quantity[4];
            Salesperson employees[8];
        } Dealer;
    
    void init_dealers(Dealer *d);
    void stock_car();
    void hire_salesperson();
    void sell_car();
    void find_car();
    void car_match();
    void deliver_car();
    void print_report();
    
    main()
    {
            int i;
            Dealer dealers[10];
    
            init_dealers(dealers);
    
            for(i=0; i<10; i++)
                    printf("%s", dealers[i].name);
    
            return 0;
    }
    
    void init_dealers(Dealer *d)
    {
            int end=1;
            while(end)
            {
                    scanf("%s", d->name);
                    if(!strcmp(d->name, "####"))
                          end=0;
                     d++;
            }
    }

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    Crap here's my output:
    Code:
    €õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ
    €õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ
    €õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ
    €õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ
    €õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ
    €õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ
    €õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ€õÿ
    what the hell is wrong now?

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What does your code look like now?

    Also:
    >>void init_dealers(Dealer *d)
    >>d++;
    This is a sure case for a buffer overflow in the future. You do best by passing in the number of elements in the dealer array, and ensuring that you don't load more than that number into it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    here's my code now:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct{
    	long id;
    	float quota;
    	float sales_credit;
        } Salesperson;
    
    typedef struct{
    	char trim[3];
    	char color[21];
    	float retail;
    	float invoice;
        } Car;
    
    typedef struct{
    	char name[21];
    	int in_stock;
    	Car cars[10];
        } Model;        
    
    typedef struct{
    	char name[21];
    	int num_cars;
    	int num_people;
    	Model quantity[4];
    	Salesperson employees[8];
        } Dealer;
        
    void init_dealers(Dealer *d); 
    void stock_car();
    void hire_salesperson(); 
    void sell_car();
    void find_car(); 
    void car_match(); 
    void deliver_car(); 
    void print_report(); 
    
    main()
    {
    	int i;
    	Dealer dealers[10];
    	
    	init_dealers(dealers);
    	
    	for(i=0; i<10; i++)
    		printf("%s", dealers[i].name);
    		
    	return 0;
    }
    
    void init_dealers(Dealer *d)
    {
    	int end=1;
    	while(end)
    	{
    		scanf("%s", d->name);
    		
    		if(strstr(d->name, "####"))
    			end=0;	
    	    d++;
    	}	
    }

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Using your original input, here's the output I get:
    Code:
    Yellow_CardBlue_DressOrange_HatPurple_NoodleGreen_FanLovely_SamCheesy_FreddyMark
    eting_MarkFormula_One####
    (It's actually all on one line)

    What is the problem again?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void init_dealers(Dealer *d)
    {
    	int end=1;
    	while(end)
    	{
    		scanf("%s", d->name);
    		
    		if(strstr(d->name, "####"))
    			end=0;	
    	    d++;
    	}	
    }
    Eh. That's definately not the preferred method of doing this.

    For starters, your 'end' variable isn't really needed. You can just use "break;" instead of your "end=0;" line.

    Next, you should really be checking the return value of scanf if you're going to use it.

    You should really be using something like fscanf so I don't blow past the end of your array.

    Furthermore, as already suggested, you should be passing the number of elements so I don't blow past the end of your structure array by entering more than 10 values.

    In short, your program gets the "highly crashable" rating. The ony thing off hand I could see to make it worse, would be to use gets to really deserve the 'amazingly crashable' rating.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  4. Struct Values
    By Muphin in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2005, 09:24 PM
  5. Replies: 1
    Last Post: 02-03-2005, 03:33 AM