Originally Posted by
JonathanS
Right now my program is getting way to complex for me to handle it all on one .C file.
So I decided to clean it up with the knowledge I acquired since I made it.
I could make a function for it, which I have tried, but failed because I couldn't pass a string to a function for some reason that I do not know. I tried looking it up on google but... those people write their tutorials in a way I do not understand
Try some of our tutorials, maybe they'll help: Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy.
Your passing a string to a function issue is because your declared your function to take a char, not a char *. Also, your prototype is incorrect. You specify the type without a variable name, not the name without a type. The purpose of the prototype is so the compiler can check your usage: correct number and type of params, and correct return type.
You need to work on further breaking your code into functions. Your calc_valence function is a good start, but you could do a lot more. Your calc_valence function would benefit greatly from using an array to map element names to valence electrons. Also, you don't need compare or VEtemp:
Code:
int calc_valence(char *element) // note the * to signify pointer to character, which is typically how you pass strings in C
{
if (strcmp(element, "B") == 0)
return 3;
if strcmp(element, "C") == 0)
return 4;
...
return -1; // check the return value in main, and report "Invalid element" if it's -1
}
You have 3 while loops in your main that are essentially the same, make one into a function, and call it 3 times, one with max1, VE, a1, a2 and a3; another with max2, VE, b1, b2 and b3, etc. Move your input gathering code into one funciton and your output code into another function.
You guys help me so much I feel like I should pay you somehow :S
Pay it forward. None of us came here to make money from our posts. As you learn and become knowledgeable and an expert in your field, take some time to help out those who are struggling like you once were.