Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

C struct glitch? (I am new to programing in C)

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. 
");
    scanf("%d", &number);

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

    return 0;
}

When I input "2": When I input 2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 
", element.num);
    printf("Atomic Mass: %.3f 
", element.mass);
    printf("Atomic Symbol: %s 
", element.symbol);
    printf("Atomic Name: %s 
", 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. 
");
    scanf("%d", &number);

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...