Lights Out Posted May 25, 2006 Share Posted May 25, 2006 (edited) #include <iostream.h>#include <fstream.h>#include <string.h>void results(int num); //prototype results()int lookup(void); //prototype lookup()struct element_info { float mass; //atomic mass int num; //atomic number char symbol[3]; //element symbol char name[20]; //element name} elements[6];int main(){ char *filename = "Elements.txt"; //file used for input int i; //counter in while loop ifstream infile; //file handle infile.open(filename, ios::in); //open file i = 1; while (!infile.eof()) { infile >> elements[i].mass; //get element atomic mass infile >> elements[i].num; //get element atomic number infile >> elements[i].symbol; //get element chemical symbol infile >> elements[i].name; //get element name i += 1; } //while loop continues until EOF is reached results(lookup()); //show the results depending on lookup()'s return value return 0;}int lookup(){ int i; //counter in loops int num = 0; //used to display correct element group int type; //type of search //user input float atom_num; //atomic number char sym[5]; //symbol char name[20]; //name cout << "What type of search would you like to do?" << endl; cout << "Symbol - 1\nName - 2\nAtomic Number - 3" << endl; cin >> type; cout << "Make sure all strings are properly capitalized." << endl; cout << "--------------------------------------------" << endl; switch(type) //just using a switch to make sure i get the right variable *type* { case 1: cout << "Please enter the chemical symbol: "; cin.ignore(80, '\n'); cin.get(sym, 25); break; case 2: cout << "Please enter the element name: "; cin.ignore(80, '\n'); cin.get(name, 25); break; case 3: cout << "Please enter the atomic number: "; cin >> atom_num; break; default: cout << "You did not enter a correct choice." << endl; break; } for(i = 1; i < 6; i++) { if (!strcmp(sym, elements[i].symbol)) { num = i; } else if (!strcmp(name, elements[i].name)) { num = i; } else if (atom_num == elements[i].mass) { num = i; } } return num;}void results(int num){ if(num) { cout << "\nYour search results:" << endl; cout << "---------------------" << endl; cout << "Element Name: \t" << elements[num].name << endl; cout << "Chemical Symbol: \t" << elements[num].symbol << endl; cout << "Atomic Number: \t" << elements[num].num << endl; cout << "Atomic Mass: \t" << elements[num].mass << endl << endl; } else { cout << "Your search string did not match any of our records.\n" << endl; }} Edited May 25, 2006 by aspnetguy Link to comment Share on other sites More sharing options...
aspnetguy Posted May 25, 2006 Share Posted May 25, 2006 nice code!So are we supposed to guess what you want. Does it return errors? Does it behave in a way you don't want?Do you have a question?BTW, this is a forum for WEB languages...which C++ isn't, so you may find it hard to find help on that subject.I have been learning c++ for a couple months now, so I might be able to help. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now