Odd issue with program

This is a discussion on Odd issue with program within the C Programming forums, part of the General Programming Boards category; So I first wanna point out that I'm extremely green when it comes to programming. But Im trying to write ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    Odd issue with program

    So I first wanna point out that I'm extremely green when it comes to programming.

    But Im trying to write this program that collects Atomic Element Information and prints it out. It works just fine until the last statement.

    Atom is an integer, and Weight is a double. Everything else are just characters.

    Code:
    /*Program that collects and Displays Atomic Information
    Written by Ben Freedgood
    4-26-09
    Language C: (gcc target)
    */
    
    #include <stdio.h>
    #include <conio.h>
    
    typedef struct {
            char name[20],symbol[5], member[100], shell[7];
            int atom;
            double weight;
            }element_t;
    
    int main(void)
    {
        element_t atom;
        element_t name, symbol, member, shell;
        element_t weight;
        int h;
        
        printf("\nWhat is the name of the element?");
        gets(name.name);
            
        printf("\nWhat is the symbol of the element?");
        gets(symbol.symbol);    
        
        printf("\nWhat class is the element in?");
        gets(member.member);
        
        printf("\nWhat is the number of electrons in each shell?");
        gets(shell.shell);
        
        printf("\nWhat is the atomic number?");
        scanf("f", &atom);
        
        printf("\nWhat is the atomic weight?");
        scanf("f", &weight);
        
        printf("\n Name: %c", name.name);
        printf("\n Symbol: %c", symbol.symbol);
        printf("\n Class: %c", member.member);
        printf("\n Electrons in Shell: %c", shell.shell);
        printf("\n Atomic Number: %d", atom); 
        printf("\n Atomic Weight: %d", weight);
        
        getch();
        return(0);
    }
    The program compiles fine until it gets to weight. I tried switching out atom and weight, but after each prompt it prints out weird, yet strangely consistent symbols.

    Example:
    Code:
    What is the atomic weight?
    Name: P
    Symbol: (Some symbol that looks like a crushed k)
    Class:I
    Electrons in Shell: (Two arrows pointing left and right)
    Atomic Number: 2293536
    Atomic Weight: 12
    I have no freakin clue whats wrong here. Any help would be appreciated, I gotta have this in by tomorrow night.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     printf("\n Atomic Number: %d", missingsomething.atom); 
        printf("\n Atomic Weight: %d", missingsomething.weight);
    And I'll advise against your use of gets.


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

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    If this were a server application, listening on some port and waiting for someone to send details about atoms to it, I would be able to exploit a bug in your program that would give me full control of your computer.

    Please, don't use gets. It's bad, and if your teacher says anything else, tell him that I will come find him and beat him into submission with a compsec book.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    5
    Haha, guys, honestly, I don't have a choice with the gets. I wish I did, but my teacher is a nutcase.

    And I added what you said was missing and it's still giving me those weird damn results.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your program is pretty much all wrong. You should just have one structure. It works like this:
    Code:
    struct foo {
        int a;
        int b;
        int c;
    };
    
    struct foo onestructure;
    
    readinto( &onestructure.a );
    readinto( &onestructure.b );
    readinto( &onestructure.c );
    
    displaystuff( onestructure.a );
    displaystuff( onestructure.b );
    displaystuff( onestructure.c );
    You have something more along the lines of:
    Code:
    struct foo hereisone;
    struct foo hereisanother;
    struct foo yetanother;
    
    readinto( &hereisone.a );
    readinto( &hereisanother.b );
    readinto( &yetanother.c );
    You should be using one structure, and filling each element in that structure. Not making a structure for each element and only using one element per structure.


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

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > scanf("f", &atom);
    There are 3 things wrong here
    - No % for the format
    - The 'atom' is a whole struct, not a member of the stuct
    - It should have been %d anyway, given your description

    Ditto on the printf's, watch your use of formats carefully.
    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.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    39

    Talking

    And I'll advise against your use of gets.
    Use fgets() instead...

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Quote Originally Posted by TigerTank77 View Post
    Code:
        
        printf("\n Name: %c", name.name);
        printf("\n Symbol: %c", symbol.symbol);
        printf("\n Class: %c", member.member);
        printf("\n Electrons in Shell: %c", shell.shell);
    Please change your format specifiers as below

    printf("\n Name: %s", name.name);
    printf("\n Symbol: %s", symbol.symbol);
    printf("\n Class: %s", member.member);
    printf("\n Electrons in Shell: %s", shell.shell);


    To all


    Why using gets() function bad? Any particular reason?
    Is it becoz it reads upto the NULL character?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > Is it becoz it reads upto the NULL character?
    No, read the manual page for fgets(), and then read the manual page for gets(), paying attention to what gets() does NOT do, which fgets() does do.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some weird issue.
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 04-12-2009, 04:10 PM
  2. Help C program that reads Odd numbers and evens
    By Cyberman86 in forum C Programming
    Replies: 4
    Last Post: 02-27-2009, 10:59 AM
  3. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  4. matrix program issue, recursion
    By sweener2001 in forum C Programming
    Replies: 1
    Last Post: 04-28-2005, 04:28 AM
  5. Loging Program issue
    By Keiyentai in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2005, 07:31 PM

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