Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I disseminate user input? Ex. NaOH. How do I make it Na O H?

Status
Not open for further replies.

pomidor

Programmer
Jun 17, 1999
2
US
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 &lt;stdio.h&gt;<br>
#include &lt;string.h&gt;<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>

 
What follows is a quick-n-dirty parser. I could have used a few ctype routines [isupper() and isdigit(), for instance].<br>
<br>
-------- BEGIN --------<br>
void parse (b)<br>
<br>
char *b;<br>
<br>
{<br>
float am = 0.0;<br>
float an = 0.0;<br>
<br>
char e[3];<br>
int n;<br>
<br>
char *s = b;<br>
<br>
while (*s != '\0')<br>
{<br>
e[0] = e[1] = e[2] = 0;<br>
if (*s &lt; 'A' ¦¦ 'Z' &lt; *s) return;<br>
e[0] = *s++;<br>
<br>
if ('a' &lt;= *s && *s &lt;= 'z') e[1] = *s++;<br>
<br>
if ('0' &lt;= *s && *s &lt;= '9')<br>
{<br>
n = 0;<br>
while (*s != '\0' && '0' &lt;= *s && *s &lt;= '9')<br>
{<br>
n *= 10;<br>
n += (int) (*s++ - '0');<br>
}<br>
}<br>
else n = 1;<br>
<br>
am += atomicmass(e) * (float) n;<br>
an += atomicnumber(e) * (float) n;<br>
}<br>
<br>
print ("%b: atomicmass %f, number: %f\n", b, am, an);<br>
return;<br>
}<br>
--------- &lt;B&gt;END&lt;/B&gt; ---------
 
You need to read the input in a string. Now read the string one character at a time . Check it for the small letter .If yes , store the string in another sting till that point. Also check for the numeric value . If yes then multiply the previous element by that number. <br>
In this way, you can separate the NaOH to Na, O , H.<br>
<br>
Does it answer your question ?<br>
Thanx<br>
Siddhartha Singh<br>
<A HREF="mailto:ssingh@aztecsoft.com">ssingh@aztecsoft.com</A>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top