Hello everyone!
First, I should state that I am recently new to C programming. I have dabbled with Python in the past, but nothing formal. I am taking a C programming class, but it is an introductory course. As such, the instructor is moving extremely slow through the material (which I understand), but I would like to start utilizing the information to help me in other areas.
Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char *str;
int line_num = 1;
int find_result = 0;
char temp[512];
printf("Search> "); /* Enter Search Request */
scanf("%c", str);
fp =fopen("ChemicalSymbol.txt", "r"); // Open text file
/* Search text file line by line for the entered data */
while(fgets(temp, 512, fp) != NULL)
{
if((strstr(temp, str)) != NULL)
{
printf("A match found on line: %d\n", line_num);
printf("\n%s\n", temp);
find_result++;
}
line_num++;
}
if(find_result == 0)
{
printf("\nSorry, couldn't find a match.\n");
}
//Close the file if still open.
if(fp)
{
fclose(fp);
}
return(0);
}
I should probably explain my overall goal with this program.
I am a chemistry major. Ultimately, I would like to write a program that will calculate the molecular weight of a compound. I expect the user entered molecular compound to be something like this: CH3COOH (An equivalent entry could be C2H4O2)
Also, it needs to be case sensitive because the chemical formulas are case sensitive.
The output will be "The molecular weight of CH3COOH is xxx.xxxxx grams"
I have two text files as well. One contains a list of all chemical symbols, and the second is a list of all atomic weights in the same order as the first.
I want to place the user inputted data into an array, then search for each element of that array in the chemical symbol text file. It will show which line it was found on, then I can use that line to grab the atomic weight from the second text file.
I know this is a lot of information, and I am probably making a ton of mistakes. But for now, I am focusing on the search function of the program, and would love any advice or recommendations.
Thank you very much.