I am working on making a program that will take in an input parameter 'N' using argv. The value N will then allow the user to enter in N value of lines about a chemical element. For example, one line would look like
1 Hydrogen H other_nonmetals 1.008 1 0 0 0 0 0 0
and using a struct, I will scan and print the input in an organized matter.
I am having two main problems currently. The first problem is scanning in the electron values. In the Hydrogen example above, I would need to scan in 1 0 0 0 0 0 0
and reprint it out in my function print_element
. When I do so, only the value 1 is stored. I want all the zeros to be omitted, but if the electron values were 1 0 0 0 0 0 1
, then only the 1 would be printed in my function.
The next problem I am having is in my for loops. While looping the function print_element
, an extra element with no values will be printed. For example, if the user inputs values for Hydrogen and then Barium, it will print Hydrogen then a completely blank element with all zeros, and then Barium. I cannot figure out how to get rid of the blank element.
#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMENTS 20
typedef struct{
int num;
char name[MAX_ELEMENTS];
char symbol[MAX_ELEMENTS];
char class[MAX_ELEMENTS];
double weight;
char electrons[MAX_ELEMENTS];
} element_t;
void scan_element(element_t *uno){
scanf("%d %s %s %s %lf %20s", &uno->num, uno->name, uno->symbol, uno->class, &uno->weight, uno->electrons);
}
void print_element(element_t uno){
printf("---------------\n| %d\t%.4f\n| %s\t%s\n| %s\n---------------\n", uno.num, uno.weight, uno.symbol, uno.name, uno.electrons);
}
int main (int argc, char *argv[]){
int i;
if (argc != 2){
printf("ERROR: You must provide exactly one argument to this program.\n");
return 0; }
int N = (int)strtol(argv[1], NULL, 10);
if(N <= 0){
printf("ERROR: Your must provide an integer greater than 0 and less than or equal to 20.\n");
return 0; }
element_t uno[MAX_ELEMENTS];
for(i=0; i<=argc; i++){
scan_element(&uno[i]); }
printf("%d total elements.\n", N);
printf(" had the smallest atomic number.\n");
printf(" had the largest atomic number.\n");
for(i=0; i<=argc; i++){
print_element(uno[i]); }
return 0;
}