1

I am learning how to create struct's and I am stuck on a program I made. Everything works fine until I try to input "2". When the program prints the symbol it's supposed to be "He" but prints "HeHelium" instead. I can't figure out what's wrong and why it's printing he.symbol and he.name all in one line. Link to image below.

#include <stdio.h>
#include <stdlib.h>

struct Element {
    int num;
    double mass;
    char symbol[2];
    char name[20];
};


int main()
{
    struct Element h;
    h.num = 1;
    h.mass = 1.008;
    strcpy(h.symbol, "H");
    strcpy(h.name, "Hydrogen");

    struct Element he;
    he.num = 2;
    he.mass = 4.0026;
    strcpy(he.symbol, "He");
    strcpy(he.name, "Helium");

    int number;

    printf("Please enter the number of the element you want info on. \n");
    scanf("%d", &number);

    if (number == 1 /*&& !(number > 1)*/) {
        printf("Atomic Number: %d \n", h.num);
        printf("Atomic Mass: %.3f \n", h.mass);
        printf("Atomic Symbol: %s \n", h.symbol);
        printf("Atomic Name: %s \n", h.name);
    } else if (number == 2) {
        printf("Atomic Number: %d \n", he.num);
        printf("Atomic Mass: %.3f \n", he.mass);
        printf("Atomic Symbol: %s \n", he.symbol);
        printf("Atomic Name: %s \n", he.name);
    } else {
        printf("Invalid number! \n");
        printf("Or that element hasn't been added to the date base yet. \n");
        printf("Try back later \n");
    }

    return 0;
}

When I input "2": When I input 2

the busybee
  • 10,755
  • 3
  • 13
  • 30
  • 9
    `symbol[2]` is too short, you forgot to account for the string null terminator. – Mat Jan 19 '21 at 21:32
  • 5
    `symbol` isn't big enough to hold `"He"`, which requires three chars (two for the string data, and one for the terminator). With that, your program invokes *undefined behavior*. – WhozCraig Jan 19 '21 at 21:33
  • Thanks! I increase symbol[5] and it work now. – Nightmare Sky Jan 19 '21 at 21:35
  • 3
    After fixing the length, a more concise way to make the definition is `struct Element he = { 2, 4.0026, "He", "Helium" };` or even better, make an array of `struct` indexed by the number. – Weather Vane Jan 19 '21 at 21:38
  • 2
    _Side note:_ In your `switch`, you are _replicating_ the `printf` for hydrogen and helium. Better to move the `printf` to a "display" function that takes a `const struct Element *` as an argument and have each `case` call it (e.g.) `display(&h);` and `display(&he);` This would be more obvious if (e.g.) you had to print _all_ 92 elements. – Craig Estey Jan 19 '21 at 21:48
  • For future questions please show your text output as formatted text in your question instead of an image. – Gerhardh Jan 20 '21 at 11:37

1 Answers1

1

You have assigned Element.symbol with only 2byte which can only store string with only one character as the other character will be used for null character. You should write "symbol[3]" as the symbol of elements in periodic table are no longer than 2 characters. Also, your code can become quite messy if you want to assign values for all 118 elements. You can write your code like below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Elements {
    int num;
    double mass;
    char symbol[3];
    char name[20];
};

void print(struct Elements element)
{
    printf("Atomic Number: %d \n", element.num);
    printf("Atomic Mass: %.3f \n", element.mass);
    printf("Atomic Symbol: %s \n", element.symbol);
    printf("Atomic Name: %s \n", element.name);
}

void assignElement(struct Elements *givenElement, int num, float mass, char symbol[3], 
char name[20])
{
    givenElement->num=num;
    givenElement->mass=mass;
    strcpy(givenElement->symbol,symbol);
    strcpy(givenElement->name,name);
}

int main()
{
    struct Elements element[119];
    assignElement(&element[1], 1, 1.008, "H", "Hydrogen");
    assignElement(&element[2], 2, 4.06, "He", "Helium");
    assignElement(&element[3], 3, 6.09, "Li", "Lithium");

    int number;

    printf("Please enter the number of the element you want info on. \n");
    scanf("%d", &number);

    if (number < 119 && number > 0) {
        print(element[number]);
    } 
    else {
        printf("Invalid number! \n");
        printf("Or that element hasn't been added to the date base yet. \n");
        printf("Try back later \n");
    }
    return 0;
}
Miraz
  • 343
  • 3
  • 15