This is my code so far. I have an external DAT file where the values for the elements are stored. What I want is for the user to input like CaCO3 and have the program take the value for Ca x 1; value for C x 1; and value of O x 3. All it can do is get the value for one element (ex. O (oxygen) or He (Helium)). Thanks.<br>
<br>
#include <stdio.h><br>
#include <string.h><br>
#define BUFFER 400<br>
<br>
main(void)<br>
{<br>
float atomic_mass;<br>
char symbol[BUFFER], search[BUFFER], name[BUFFER];<br>
int atomic_number;<br>
<br>
FILE *fp = fopen("chem.dat","r");<br>
<br>
printf("Enter An Elements Symbol: ");<br>
scanf("%s", &symbol);<br>
<br>
while(1)<br>
{<br>
fscanf(fp, "%s", search);<br>
fscanf(fp, "%s", name);<br>
fscanf(fp, "%d", &atomic_number);<br>
fscanf(fp, "%f", &atomic_mass);<br>
if (strcmp(search, symbol)==0)<br>
break;<br>
}<br>
<br>
<br>
printf("%s, is %s\n", search, name);<br>
printf("The atomic mass is %.4f\n", atomic_mass);<br>
printf("The atomic number is %d\n", atomic_number);<br>
<br>
<br>
fclose(fp);<br>
<br>
return 0;<br>
}<br>
<br>
<br>
#include <stdio.h><br>
#include <string.h><br>
#define BUFFER 400<br>
<br>
main(void)<br>
{<br>
float atomic_mass;<br>
char symbol[BUFFER], search[BUFFER], name[BUFFER];<br>
int atomic_number;<br>
<br>
FILE *fp = fopen("chem.dat","r");<br>
<br>
printf("Enter An Elements Symbol: ");<br>
scanf("%s", &symbol);<br>
<br>
while(1)<br>
{<br>
fscanf(fp, "%s", search);<br>
fscanf(fp, "%s", name);<br>
fscanf(fp, "%d", &atomic_number);<br>
fscanf(fp, "%f", &atomic_mass);<br>
if (strcmp(search, symbol)==0)<br>
break;<br>
}<br>
<br>
<br>
printf("%s, is %s\n", search, name);<br>
printf("The atomic mass is %.4f\n", atomic_mass);<br>
printf("The atomic number is %d\n", atomic_number);<br>
<br>
<br>
fclose(fp);<br>
<br>
return 0;<br>
}<br>
<br>