Thread: What's Wrong With My Code?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    32

    Question What's Wrong With My Code?

    Could you help me with my code?

    Code:
    #include<stdio.h>
    
    int main()
    {
    	
    	struct names 
    	{
    		char name [j];
    		int cars [20];
    		int planes [20];
    	}
    
    	printf("Please enter name, number of cars, & number of planes.");
    
    	for(int i=1; i<=6; i++)
    	{
    		scanf("%s", names[i].name);
    		scanf("%d", names[i].cars);
    		scanf("%d", names[i].planes);
    	}
    
    	for(int i =1; i<=6; i++)
    	{
    		printf("Mr/Ms" %s "has" %d "and" %d "\n", names[i].name, names[i].cars, names[i].planes);
    	}
    	
    	return 0;
    }
    Thanks.
    Last edited by javacvb; 06-13-2003 at 09:20 PM.
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    printf("Mr/Ms" %s "has" %d "and" %d "\n", names[i].name, names[i].cars, names[i].planes)
    This is one thing, you need to have the conversion specifiers inside the quotes.
    ex: printf("blah %s blah %s", blah, blah);

    And what is 'j' in this
    >>>char name [j];

    It would be more efficient to put the structure outside of main and having 'j' replaced with a constant. ex: 20. Like the rest of the char arrays in the structure.
    Last edited by stumon; 06-13-2003 at 09:41 PM.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Banned
    Join Date
    May 2003
    Posts
    124
    Code:
    #include <stdio.h>
    
    struct names
    	{
    		char name [20];
    		int cars ;
    		int planes ;
    	} person[6];
    
    int main()
    {
    
    	printf("Please enter name, number of cars, & number of planes.\n");
    
    	for(int i=0; i<=5; i++)
    	{
    		scanf( "%s", person[i].name);
    		scanf( "%d", &person[i].cars);
    		scanf( "%d", &person[i].planes);
    	}
    
    	for( i=0; i<=5; i++)
    	{
    		printf("Mr/Ms %s has %d and %d \n", person[i].name, person[i].cars, person[i].planes);
    	}
    
    	return 0;
    }
    Let's see now:
    Structures are intermediate so $10
    But, many of your mistakes are beginner so $5
    But, because you really murdered the C language i want $1000 ( not only for me, but for C's pride... )
    See my price list for further details

  4. #4
    Banned
    Join Date
    May 2003
    Posts
    124
    Ouu....!!!
    Never use i for an array, that's italic..
    Code:
    #include <stdio.h>
    
    struct names
    	{
    		char name [20];
    		int cars ;
    		int planes ;
    	} person[6];
    
    int main()
    {
    
    	printf("Please enter name, number of cars, & number of planes.\n");
    
    	for(int j=0; j<=5; j++)
    	{
    		scanf( "%s", person[j].name);
    		scanf( "%d", &person[j].cars);
    		scanf( "%d", &person[j].planes);
    	}
    
    	for( j=0; j<=5; j++)
    	{
    		printf("Mr/Ms %s has %d and %d \n", person[j].name, person[j].cars, person[j].planes);
    	}
    
    	return 0;
    }
    Don't forget my money !

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    32
    Originally posted by AProg
    Ouu....!!!
    Never use i for an array, that's italic..
    Don't forget my money !

    Thank you very much for all the helps offered.

    I will not forget if one day I'll be someone like Bill Gates. But it is unlikely that I'll become one as I am having problems understanding pointers, stack, queue etc...
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  6. #6
    Banned
    Join Date
    May 2003
    Posts
    124
    >having problems understanding pointers..
    How did you proceed to structures without understanding pointers?

  7. #7
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    >having problems understanding pointers..
    How did you proceed to structures without understanding pointers?
    You can learn them both ways with or without pointers.
    You do not need pointers to learn structs.
    Mr. C: Author and Instructor

  8. #8
    Banned
    Join Date
    May 2003
    Posts
    124
    Sure... I agree with you, but because all tutorials i have seen introduce pointers at first and then go to structures, i guessed that he would know to use pointers before structures.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    18
    if your code is exact then Why didn't you notice the fact that you initialized i twice
    I need MONEY more than help with My C++ so yeah you get the idea

    C notes preferably LOL

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    18
    if your code is exact then Why didn't you notice the fact that you initialized i twice wow what a bone head
    I need MONEY more than help with My C++ so yeah you get the idea

    C notes preferably LOL

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by PuhFENDanT
    if your code is exact then Why didn't you notice the fact that you initialized i twice wow what a bone head
    Who cares how many times they initialized i? They are supposed to initialize twice. It's called 'variable reuse'. There is no point in having seperate variable names when all you're doing is using it in two consecutive loops, neither of which have any effect on the other.

    For that matter, this also has no harm:
    Code:
    int foo = 0;
    for( foo = 0; foo < bar; foo++ )
    It doesn't matter that I've initialized it in two places. All that matters is that when I want to use it in the loop, I initialize it to whatever it needs to be. Since nothing else in the code is dependant on the value of foo going into the loop, it doesn't matter what it contains before I initialize it the second time.

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

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    32
    Originally posted by AProg
    Sure... I agree with you, but because all tutorials i have seen introduce pointers at first and then go to structures, i guessed that he would know to use pointers before structures.
    You are right. All my C & C++ books started with pointers then go to structures. They say that pointers is what makes C a poweful language & different from other languages.

    I have to learn structures even though I hardly understand what pointers are really for.

    The next thing I need to learn is stacks & queues. Any good complicated examples or web sites that you guys can show me? I will appreciate it very much.
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  13. #13
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You don't need to know pointers before learning to work with structures. Since pointers are such an important concept in C and C++, books often treat them in one of the first chapters.

    Using the combination of pointers and structs you can create datatypes like stacks, trees, graphs and many others.

  14. #14
    Banned
    Join Date
    May 2003
    Posts
    124

    Hmmm...

    Well... i have a/some suggestion/s for you javacvb.
    Leave the structures aside and go back to the pointers. Learn very good the pointers and then come again to structures. I think that a nice hint is not to leave something unfinished. BE PROFESSINAL. Whatever you do, do it perfect. Find some good exercises that might help you with pointers. I used to have some problems with pointers, but now i see that they are not that difficult. And one day you will say the same. If you still have problems, this forum is always open for you ( i guess so ).
    You gave me the sense that you are trying to learn C in a day, and that time chases you. And as a result, you try to learn many many things - only the basics ( that's wrong ) - so fast and in conclusion you know nothing! Always remember that smattering is worst than ignorance. That's the way to top!
    So, finish pointers and then go on. Take your time with structures and then procceed to stacks & queues. Meanwhile solve some exercises.

    Eeeer... I wish you good luck...and hard performance ( take it as entertaining performance ).

  15. #15
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I agree with AProg. Take the time to learn the language and learn how to apply the language well. Don't just learn about the language features, but especially how to apply them and how you shouldn't apply them. Before learning concepts like stacks, queues and other datastructures, you should understand the underlying concepts, like pointers and structures, to be able to really understand how datastructures work and to be able to apply them well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  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