Page 1 of 1

BEGINNER C++ STL MAP AND MULTIMAP CONTAINER CLASSES TUTORIAL BEGINNER C++ STL MAP AND MULTIMAP CONTAINER CLASSES TUTORIAL Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 16 June 2009 - 03:27 AM

BEGINNER C++ STL MAP AND MULTIMAP CONTAINER CLASSES TUTORIAL

WHAT YOU WILL LEARN IN THIS TUTORIAL

1. You will learn the basic C++ map and multimap vocabulary.

2. You will learn how to instantiate a map and multimap object.

3. You will learn how to insert elements into a STL map and multimap.

4. You will learn how to find elements in a STL map and multimap.

5. You will learn the basic member functions used by a map and multimap.

• I. ASSUMPTIONS

I assume you have never programmed using C++ STL map and multimap functionality.
I assume you have a C++ compiler.

• II. INTRODUCTION – C++ MAP AND MULTIMAP VOCABULARY

What is STL? STL is the Standard Template Library. The STL contains six kinds of components: containers, container adapters, iterators, algorithms, function objects, and function adapters. To understand and use the STL, you must have a complete understanding of the C++ language, including pointers, references, and templates.

What are containers? Containers are objects that hold other objects.
What are iterators? Iterators are objects that act, more or less, like pointers. They give you the ability to cycle through the contents of a container in much the same way that you would use a pointer to cycle through an array.

What are map and multimap? The map and multimap are key-value pair containers that allow for a lookup on the basis of a key. The difference between map and multimap is that only the latter allows for duplicates, whereas the former can store only unique keys. Map and multimap are container classes that help with applications that require frequent and quick searches. Most functions in a map and multimap work in a similar fashion; accept similar parameters; and return similar value types. To use the STL map and multimap classes you need to use #include <map>.

III. INSTANTIATING A MAP AND MULTIMAP OBJECT

STL map and multimap are template classes that need to be instantiated before you can use any of their member functions. A STL map and multimap instantiation has three parameters: a key type, value type, and an optional binary predicate.

Any data type for which a comparison operator can be defined can be used as a key. Each element in a multimap is identified by its key value; and multiple elements can have the same key value.

A predicate is a special type of function. There are two types of predicates: unary and binary. A unary predicate takes one argument while a binary predicate takes two. A binary predicate is the most common map and multimap instantiation syntax parameter.

The attached sample program contains the following two instantiations.
multimap<string, int> words; is a multimap instantiation of the object words with a key of type string, a value type int, using the default sort criterion. A multimap can have duplicate keys. This means using a multimap each word is a key and each line is a value. Therefore, the words are sorted upon insertion alphabetically by line number. Duplicate words are in alphabetical groups sorted by line number.

map<int, string> lines; is a map instantiation of the object lines with a key type of int, and a value type string, using the default sort criterion. A map has to have unique keys, no duplicates allowed. This means using a map each line is a key and each string of words is a value. Therefore, the lines are sorted upon insertion numerically, and each matching strings of words is sorted alphabetically. Duplicate words can appear alphabetically within a string on one line; and on more than one line, alphabetically within strings.

• IV. INSERTING ELEMENTS INTO A STL MAP AND MULTIMAP

As with all the containers, you can use a map or a multimap to store objects of types you create. Values can be inserted into a map or a multimap using the insert() operation. Note that the argument must be a key-value pair. The first field in each pair is taken to be a key, while the second field is a value.

The attached program creates a file named example.txt; and inserts six sentences into example.txt.

The int SearchExampleTxtFile(); function uses the insert(); function to insert example.txt into a map and a multimap. Example.txt consists of lines and words. The line numbers are unique. The words in each line are not unique.

The map can not have duplicate keys. Each line number is unique; therefore, it becomes the key for the map. The words in each line are not unique; however, as a string of words they become a value. This provides the compiler with ordered pairs of lines and strings of words for the map.

The multimap can have duplicate keys. Each word becomes a key for the multimap. Each line becomes a value for the multimap. This provides the compiler with ordered pairs of words and lines for the multimap.

• V. FINDING ELEMENTS IN A STL MAP AND MULTIMAP

Maps are sorted containers. Elements inserted into a map or multimap are sorted on insertion. Associative containers such as map and multimap feature a member function called find() that allows you to find a value given a key. The attached sample program int SearchExampleTxtFile(); function uses the member function find() to find words requested by the user. The iterator returned by the find() member function points to the first element found. The member function called count() is used to determine the number of pairs in the map that have the same key. A map or multimap arrange all elements of a value in sequential positions. When using a multimap, we simply move the iterator that many elements forward to access other pairs containing the same key. The bounds of the range are supplied by functions lower_bound() and upper_bound().

• VI. BASIC MAP AND MULTIMAP MEMBER FUNCTIONS

The following basic map and multimap member functions are used in the attached sample program.

1. count(); with a key as an argument, will return a count of the number of entries found corresponding to the key. A map count() return value can only be 0 or 1 because a map only allows unique keys. A multimap allows duplicates; therefore, the count() return value depends on the number of duplicates.

2. find(); function returns an iterator to the matching element or to the end of the map if the key is not found. Once the map has been initialized with keys and values, you can search for a value given its key by using the find(); function. When a match is found, the value associated with the key is contained in the second member of pair.

3. insert(); function allows you to insert one or more pairs into a map.

4. iterator(); function is any object that, pointing to some element in a range of elements such as a container, has the ability to iterate through the elements of that range using a set of operators, at least, the increment (++) and dereference (*) operators. In the multimap class template, there are bidirectional iterators.

5. lower_bound(); function finds the first point in the sequence that is not less than a specified value.

6. make_pair(); function is defined as a template function, so it automatically deduces the type for the pair from the argument types you supply. The advantage of make_pair is that the types of the objects being stored are determined automatically by the compiler rather than being explicitly determined by the programmer.

7. pair(); function, key/value pairs are stored in a map as objects of type pair. The pair template class is used to construct the key/value pairs. The data types specified by pair must match those of the map into which the pairs are being inserted. Pair(); returns a pair of iterators that point to the first and last elements in the map that contain the specified key.

8. upper_bound(); function finds the last point in a sequence that is not greater than some value.

• VII. PROGRAM ANALYSIS

Please copy and paste the attached program into a file your compiler can read. Then build, compile, and debug the program. Following along, back and forth, between the tutorial and your copy of the program will help you understand the map and multimap functionality.


//************************************
// BEGINNER C++ STL MAP AND MULTIMAP CONTAINER CLASSES TUTORIAL
//************************************
#include <fstream>				// Needed for file I/O.
#include <iomanip>				// Needed for format manipulation. 
#include <iostream>
#include <map>					// Needed for STL classes map and multimap.
#include <sstream>
#include <stdio.h>
#include <stdlib.h>				// Needed for converter functions.
#include <string>
using std::istringstream;		// Needed for STL classes map and multimap.
using std::ifstream;			// Needed for STL classes map and multimap.
using std::string;				// Needed for STL classes map and multimap.
using std::cout;				// Needed for STL classes map and multimap.
using std::cin;					// Needed for STL classes map and multimap.
using std::cerr;				// Needed for STL classes map and multimap.
using std::map;					// Needed for STL classes map and multimap.
using std::multimap;			// Needed for STL classes map and multimap.

using namespace std;
//************************************
//In general, C++ strings are preferred over C strings.
//************************************
int UserLoop();					//This function is the user prompted loop.
void UserPause();				//This function prompts the user to continue.
int UserMenu();					//This function prints a menu to the console.
void UserSwitch(int choice);	//This function is a switch.
void CreateExampleTxtFile();		//This function creates a file named example.txt.
void PrintExampleTxtFile();		//This function reads and prints a file named example.txt.
void SizeExampleTxtFile();		//This function computes and prints the size of the example.txt file.
int SearchExampleTxtFile();		//This function searches example.txt file for a user inputted string.
//************************************
//This is the main function.
int main(int argc, char* argv[])
{
	UserLoop();
	return 0;
}

//************************************
//This function is the user prompted loop.
int UserLoop()
{
	int choice = 0;			//Instantiates choice as an object of class type int, initializing it's value to 0.
	bool exitt = false;		//Instantiates exitt as an object of class type bool, initializing it's value to false.

	do
	{
		UserMenu();
				
		cout << endl << endl << endl;
		cout << "  Type the number 1 and press return to run this program again." << endl << endl << endl;
		cout << "  Type any key and press return to exit ==> ";

		cin >> choice;

		if (choice == 1)
			exitt = false;
		else
			exitt = true;
	}while(exitt == false);

	UserPause();

	return 0;
}

//************************************
//This function prompts the user to continue.
void UserPause()
{
	cout << endl << endl << endl;
	cout << "  ";
	system("PAUSE");
}

//************************************
//This function prints a menu to the console.
int UserMenu()
{
	int choice = 0;	//Instantiates choice as an object of class type int, initializing it's value to 0.

	system("CLS");

	cout << endl << endl << endl;
	cout << "		   MENU" << endl;
	cout << "  ************************" << endl << endl;
	cout << "  1  Create a new file." << endl;
	cout << "  2  Print a new file." << endl;
	cout << "  3  Size a new file." << endl;
	cout << "  4  Search a new file." << endl << endl;
	cout << "	 Enter 0 to exit." << endl << endl;

	cout << "  Please type a number from 0 to 4 and then press enter ==> ";

	//Get customer input.

	cin >> choice;

	UserSwitch(choice);

	//Test validity of user input data.

	return 0;
}

//************************************
//This function is a switch.
void UserSwitch(int choice)
{
		switch(choice)
		{
			//Call class member functions.

		case 0:
			break;

		case 1:
			CreateExampleTxtFile();		//Create new file.
			break;

		case 2:
			PrintExampleTxtFile();		//Print new file.
			break;

		case 3:
			SizeExampleTxtFile();		//Size new file.
			break;

		case 4:
			SearchExampleTxtFile();		//Search new file.
			break;

		default:
			cout << endl << endl << endl;
			cout << "  Please select again." << endl << endl;
			break;
		}
}

//************************************
//This function creates a file named example.txt
//and inserts six sentences into example.txt.

void CreateExampleTxtFile()
{
	ofstream myfile;	//ofstream class initialization of object myfile.

	myfile.open ("example.txt");	//Opens myfile initialized with example.txt.
	
	if (myfile.is_open())	//Checks to see if example.txt file is open.
	{

			system("CLS");
			cout << endl << endl << endl;
		myfile << "one two three four five\n";	//Extracts text line to example.txt line at a time.
			cout << "  ";	
			cout << "one two three four five\n";	
		myfile << "two three four five six\n";
			cout << "  ";	
			cout << "two three four five six\n";
		myfile << "three four five six seven\n";
			cout << "  ";	
			cout << "three four five six seven\n";
		myfile << "four five six seven eight\n";
			cout << "  ";	
			cout << "four five six seven eight\n";
		myfile << "five six seven eight nine\n";
			cout << "  ";	
			cout << "five six seven eight nine\n";
		myfile << "six seven eight nine ten\n";
			cout << "  ";
			cout << "six seven eight nine ten\n";

		myfile.close();		//Closes example.txt.
	}
	else cout << "  Unable to open file";

	
}

//************************************
//This function reads and prints a file named example.txt.
void PrintExampleTxtFile()
{
	
	string line;	//Instantiates line as an object of class type string.

	ifstream myfile ("example.txt");
	system("CLS");
	if (myfile.is_open())			//Checks to see if example.txt file is open.
	{
		cout << endl << endl << endl;
	
		while (! myfile.eof())		//eof(); returns true when end of file has been reached,
		{							//otherwise it returns false.
			getline (myfile, line);
			cout << "  " << line << endl;
		}
		myfile.close();				//Closes example.txt.
	}

	else cout << "  Unable to open file"; 

	
}

//************************************
//This function computes and prints the size of the example.txt file.
void SizeExampleTxtFile()
{
	long begin;							//Instantiates begin as an object of class type long.
	long end;							//Instantiates end as an object of class type long.
	ifstream myfile ("example.txt");	//Determine whether the stream object is currently associated with a file, if so, file is open.
	begin = myfile.tellg();				//tellg(); is used to get the beggining position in the stream 
	myfile.seekg (0, ios::end);			//after it has been moved with seekg(); to the end of the stream;
										//therefore, determining the size of the file.
	end = myfile.tellg();				//tellg(); is used to get the ending position in the stream 
	myfile.close();						//closes file.					
	system("CLS");
	cout << endl << endl << endl;
	cout << "  File size is: " << (end - begin) << " bytes.\n";		//end - begin determines size of the file.
}

//************************************
//This function searches example.txt file for a user inputted string.
int SearchExampleTxtFile()
{
	const int SIZE = 200;				// Constant for size of character string.
	char fileName[SIZE];				 // Holds user file name.
	ifstream inputFile;				  // Input file.
	int counter = 0;					 // Counts string occurences.

	system("CLS");

	//************************************
	// Get user's file name.
	cout << endl << endl << endl;
	cout << "  Enter the name of the file you want to search.\n\n";
	cout << "  For example; example.txt \n\n";
	cout << "  Then press return ==> ";
	cin >> fileName;		//FileName is example.txt.

	//************************************
	// Open user's file.
	inputFile.open(fileName);
	if(!fileName)
	{
		cout << endl << endl << endl;
		cout << "  Cannot open " << fileName << endl;
		cout << endl << endl << endl;
		cout << "  ";
		system("pause");
	}

	//************************************
	// Loads example.txt into multimap and map
	multimap<string, int> words;	//Key words and Value lines; multimap can take duplicate key values.
	map<int, string> lines;			//Key lines and Value words; map can not take duplicate key values.
	string str;						//Instantiates str as an object of type string.
	ifstream input(fileName);		//ifstream provides an interface to read data from files as input streams.
	if(input.fail())				//Test if example.txt opened.
	{
		cerr << "\n  The file could not be opened.";	//Error message if example.txt not opened.
		return -1;					//Returns -1 if example.txt not opened.
	}

	//************************************
	// Close user's file.
	inputFile.close();				//Closes example.txt.

	//************************************
	//Insert into words and lines
	int i = 1;						//Instantiates i as an object of class type int, initializing it value to 1.
	while(getline(input, str))
	{
		istringstream in(str);
		string s;					//Instantiates s as an object of class type string.
		while(in >> s)
		{
			words.insert(make_pair(s, i));	//Insert into object words
		}
		lines.insert(make_pair(i, str));	//Insert into object lines
		i++;
	}

	//************************************

	string search;
	cout << endl << endl << endl;
	cout << "  Enter the word or string of words you are searching for.\n\n";
	cout << "  For example; six \n\n";
	cout << "  Then press return ==> ";

	cin >> search;

	system("CLS");

	//************************************

	cout << "\n  The number of matches = " << words.count(search) << '\n';	//count() function returns number pairs with same key; for multimap key is a word.
	multimap < string,int>::iterator it1 = words.lower_bound(search);	//lower_bound() function returns start of range of pairs with same key; for multimap key is a word.
	multimap < string,int>::iterator it2 = words.upper_bound(search);	//upper_ bound() function returns end of range of pairs with same key; for multimap key is a word.
	while(it1 != it2)
	{
		int x = it1->second;	//example.txt line; second contains the value; for map value is string of words
		map<int, string>::iterator iter = lines.find(x);
		cout << '\n' << "  Key => Line " << x << "	Value => " << iter->second << '\n';	//example.txt line; x is line number; second contains the value; for map value is string of words.
		it1++;
		while(true)
		{
			if(it1 != it2 && it1->second == x)	//example.txt line; second is the value; for map value is string of words.
			{
				it1++;

			}
			else
			{
				break;
			}
		}
	}
	return 0;
}

//************************************





The attached sample program consists of the following eight functions.

1. int UserLoop(); function is the only function in the main() function. It is called when the program begins. It is used to contain a user prompted loop to exit the program or for repeated use of the program. This function calls the int UserMenu(); function.

2. void UserPause(); function contains system(“PAUSE”); which prompts the user to press any key to continue. It is used to hold information on the console long enough for the user to read the information. This function does not call any other functions.


3. int UserMenu(); function is called by the int UserLoop(); function when the program begins. It is used to contain the program menu. The menu provides the user with five options: create a new file; print the new file; size the new file; search the new file; or exit the program. This function prompts the user to pick one of the five options. After the user makes a selection, this function calls the UserSwitch(choice); function.

4. void UserSwitch(int choice); function is called by the int UserMenu(); function. It is a switch with five cases: 0 to exit the program; 1 to create a new file; 2 to print the new file; 3 to size the new file; and 4 to search the new file. Choice 1 calls the void CreateExampleTxtFile(); function. Choice 2 calls the void PrintExampleTxtFile(); function. Choice 3 calls the void SizeExampleTxtFile(); function. Choice 4 calls the int SearchExampleTxtFile(); function.

5. void CreateExampleTxtFile(); function is called by the void UserSwitch(int choice); function. It creates a file named example.txt. Opens the file, inserts six sentences into example.txt one line at a time. As each line is inserted into the file it is echo printed to the console. Then it closes the file. This function does not call any other functions.

6. void PrintExampleTxtFile(); function is called by the void UserSwitch(int choice); function. Opens the file, prints the file to the console; and, closes the file. This function does not call any other functions.

7. void SizeExampleTxtFile(); function is called by the void UserSwitch(int choice); function. Opens the file, sizes the file, prints the size to the console; and, closes the file. This function does not call any other functions.

8. int SearchExampleTxtFile(); function is called by the void UserSwitch(int choice); function. It prompts the user for a file name. Opens the file the user requests. Reads the file and inserts the files contents into a map and multimap. Closes the user requested file. Prompts the user for a word or string of words to search for. Finds the word or string of words per the user’s request. Prints the word or string of words on the console.

• VIII. CONTACT ME

Leave constructive comments and I will make up to date revisions to the tutorial based on your comments.

WHAT YOU HAVE LEARNED IN THIS TUTORIAL

1. You have learned the basic C++ map and multimap vocabulary.

2. You have learned how to instantiate a map and multimap object.

3. You have learned how to insert elements into a STL map and multimap.

4. You have learned how to find elements in a STL map and multimap.

5. You have learned the basic member functions which can be used with a map and multimap.

Is This A Good Question/Topic? 0
  • +

Replies To: BEGINNER C++ STL MAP AND MULTIMAP CONTAINER CLASSES TUTORIAL

#2 Guest_Jovan*


Reputation:

Posted 23 December 2010 - 10:05 PM

So for a class I'm doing (don't worry, it's extra credit for a Chemistry class, not a coding class), I'm making a program that calculates atomic mass based on the user inputting the symbols and amounts of each element involved in the molecule.

As an example, if the molecule that the user wants the atomic mass of is H2O, then I want them to be able to input "H" into the program, and "2" for the amount, and "O" and "1". The program is then supposed to go through the map I've created and find those values and calculate the mass and spit it out.

Firstly, I want to know if this is possible? Secondly, can you show me what to do, based on the code I have here:

#include <iostream>
#include <string>
#include <map>
using namespace std;
struct ElementInfo
{
    std::string el_symbol;
    double el_mass;
    int el_number;

    ElementInfo( const std::string& name = "", 
                         int number = 0,
                         double weight = 0) : el_symbol(name),
                         el_mass(weight), el_number(number) {}
};

typedef std::map<int, ElementInfo> PeriodicTable;

enum { H, He, Li, Be, B, C, N, O, F, Ne, Na, Mg, Al, Si, P, S, Cl, Ar, K, Ca, Sc, Ti, V, Cr, Mn, Fe, Co, Ni, Cu, Zn, Ga, Ge, As, Se, Br, Kr, Rb, Sr, Y, Zr, Nb, Mo, Tc, Ru, Rh, Pd, Ag, Cd, In, Sn, Sb, Te, I, Xe, Cs, Ba, La, Ce, Pr, Nd, Pm, Sm, Eu, Gd, Tb, Dy, Ho, Er, Tm, Yb, Lu, Hf, Ta, W, Re, Os, Ir, Pt, Au, Hg, Tl, Pb, Bi, Po, At, Rn, Fr, Ra, Ac, Th, Pa, U, Np, Pu, Am, Cm, Bk, Cf, Es, Fm, Md, No, Lr, Rf, Db, Sg, Bh, Hs, Mt, Uun, Uuu, Uub, Uuq };

void InitializePeriodicTable( PeriodicTable& theTable)
{
     theTable[H] = ElementInfo("H", 1, 1.0079);
     theTable[He] = ElementInfo("He", 2, 4.0026);
     theTable[Li] = ElementInfo("Li", 3, 6.941);
     theTable[Be] = ElementInfo("Be", 4, 9.0122);
     theTable[B] = ElementInfo("B", 5, 10.811);
     theTable[C] = ElementInfo("C", 6, 12.011);
     theTable[N] = ElementInfo("N", 7, 14.007);
     theTable[O] = ElementInfo("O", 8, 15.999);
     theTable[F] = ElementInfo("F", 9, 18.998);
     theTable[Ne] = ElementInfo("Ne", 10, 20.180);
     theTable[Na] = ElementInfo("Na", 11, 22.990);
     theTable[Mg] = ElementInfo("Mg", 12, 24.305);
     theTable[Al] = ElementInfo("Al", 13, 26.982);
     theTable[Si] = ElementInfo("Si", 14, 28.086);
     theTable[P] = ElementInfo("P", 15, 30.974);
     theTable[S] = ElementInfo("S", 16, 32.065);
     theTable[Cl] = ElementInfo("Cl", 17, 35.453);
     theTable[Ar] = ElementInfo("Ar", 18, 39.948);
     theTable[K] = ElementInfo("K", 19, 39.098);
     theTable[Ca] = ElementInfo("Ca", 20, 40.078);
     theTable[Sc] = ElementInfo("Sc", 21, 44.956);
     theTable[Ti] = ElementInfo("Ti", 22, 47.867);
     theTable[V] = ElementInfo("V", 23, 50.942);
     theTable[Cr] = ElementInfo("Cr", 24, 51.996);
     theTable[Mn] = ElementInfo("Mn", 25, 54.938);
     theTable[Fe] = ElementInfo("Fe", 26, 55.845);
     theTable[Co] = ElementInfo("Co", 27, 58.933);
     theTable[Ni] = ElementInfo("Ni", 28, 58.693);
     theTable[Cu] = ElementInfo("Cu", 29, 63.546);
     theTable[Zn] = ElementInfo("Zn", 30, 65.39);
     theTable[Ga] = ElementInfo("Ga", 31, 69.723);
     theTable[Ge] = ElementInfo("Ge", 32, 72.61);
     theTable[As] = ElementInfo("As", 33, 74.992);
     theTable[Se] = ElementInfo("Se", 34, 78.96);
     theTable[Br] = ElementInfo("Br", 35, 79.904);
     theTable[Kr] = ElementInfo("Kr", 36, 83.80);
     theTable[Rb] = ElementInfo("Rb", 37, 85.468);
     theTable[Sr] = ElementInfo("Sr", 38, 87.62);
     theTable[Y] = ElementInfo("Y", 39, 88.906);
     theTable[Zr] = ElementInfo("Zr", 40, 91.224);
     theTable[Nb] = ElementInfo("Nb", 41, 92.906);
     theTable[Mo] = ElementInfo("Mo", 42, 95.94);
     theTable[Tc] = ElementInfo("Tc", 43, 98.0);
     theTable[Ru] = ElementInfo("Ru", 44, 101.07);
     theTable[Rh] = ElementInfo("Rh", 45, 102.91);
     theTable[Pd] = ElementInfo("Pd", 46, 106.42);
     theTable[Ag] = ElementInfo("Ag", 47, 107.87);
     theTable[Cd] = ElementInfo("Cd", 48, 112.41);
     theTable[In] = ElementInfo("In", 49, 114.82);
     theTable[Sn] = ElementInfo("Sn", 50, 118.71);
     theTable[Sb] = ElementInfo("Sb", 51, 121.76);
     theTable[Te] = ElementInfo("Te", 52, 127.60);
     theTable[I] = ElementInfo("I", 53, 126.90);
     theTable[Xe] = ElementInfo("Xe", 54, 131.29);
     theTable[Cs] = ElementInfo("Cs", 55, 132.91);
     theTable[Ba] = ElementInfo("Ba", 56, 137.33);
     theTable[La] = ElementInfo("La", 57, 138.91);
     theTable[Ce] = ElementInfo("Ce", 58, 140.12);
     theTable[Pr] = ElementInfo("Pr", 59, 140.91);
     theTable[Nd] = ElementInfo("Nd", 60, 144.24);
     theTable[Pm] = ElementInfo("Pm", 61, 145.0);
     theTable[Sm] = ElementInfo("Sm", 62, 150.36);
     theTable[Eu] = ElementInfo("Eu", 63, 151.96);
     theTable[Gd] = ElementInfo("Gd", 64, 157.25);
     theTable[Tb] = ElementInfo("Tb", 65, 158.93);
     theTable[Dy] = ElementInfo("Dy", 66, 162.50);
     theTable[Ho] = ElementInfo("Ho", 67, 164.93);
     theTable[Er] = ElementInfo("Er", 68, 167.26);
     theTable[Tm] = ElementInfo("Tm", 69, 168.93);
     theTable[Yb] = ElementInfo("Yb", 70, 173.04);
     theTable[Lu] = ElementInfo("Lu", 71, 174.97);
     theTable[Hf] = ElementInfo("Hf", 72, 178.49);
     theTable[Ta] = ElementInfo("Ta", 73, 180.95);
     theTable[W] = ElementInfo("W", 74, 183.84);
     theTable[Re] = ElementInfo("Re", 75, 186.21);
     theTable[Os] = ElementInfo("Os", 76, 190.23);
     theTable[Ir] = ElementInfo("Ir", 77, 192.22);
     theTable[Pt] = ElementInfo("Pt", 78, 195.08);
     theTable[Au] = ElementInfo("Au", 79, 196.97);
     theTable[Hg] = ElementInfo("Hg", 80, 200.59);
     theTable[Tl] = ElementInfo("Tl", 81, 204.38);
     theTable[Pb] = ElementInfo("Pb", 82, 207.2);
     theTable[Bi] = ElementInfo("Bi", 83, 208.98);
     theTable[Po] = ElementInfo("Po", 84, 209.0);
     theTable[At] = ElementInfo("At", 85, 210.0);
     theTable[Rn] = ElementInfo("Rn", 86, 222.0);
     theTable[Fr] = ElementInfo("Fr", 87, 223.0);
     theTable[Ra] = ElementInfo("Ra", 88, 226.0);
     theTable[Ac] = ElementInfo("Ac", 89, 227.0);
     theTable[Th] = ElementInfo("Tc", 90, 232.04);
     theTable[Pa] = ElementInfo("Pa", 91, 231.04);
     theTable[U] = ElementInfo("U", 92, 238.03);
     theTable[Np] = ElementInfo("Np", 93, 237.0);
     theTable[Pu] = ElementInfo("Pu", 94, 244.0);
     theTable[Am] = ElementInfo("Am", 95, 143.0);
     theTable[Cm] = ElementInfo("Cm", 96, 247.0);
     theTable[Bk] = ElementInfo("Bk", 97, 247.0);
     theTable[Cf] = ElementInfo("Cf", 98, 251.0);
     theTable[Es] = ElementInfo("Es", 99, 252.0);
     theTable[Fm] = ElementInfo("Fm", 100, 257.0);
     theTable[Md] = ElementInfo("Md", 101, 258.0);
     theTable[No] = ElementInfo("No", 102, 259.0);
     theTable[Lr] = ElementInfo("Lr", 103, 262.0);
     theTable[Rf] = ElementInfo("Rf", 104, 261.0);
     theTable[Db] = ElementInfo("Db", 105, 262.0);
     theTable[Sg] = ElementInfo("Sg", 106, 266.0);
     theTable[Bh] = ElementInfo("Bh", 107, 264.0);
     theTable[Hs] = ElementInfo("Hs", 108, 269.0);
     theTable[Mt] = ElementInfo("Mt", 109, 268.0);
     theTable[Uun] = ElementInfo("Uun", 110, 271.0);
     theTable[Uuu] = ElementInfo("Uuu", 111, 272.0);
     theTable[Uub] = ElementInfo("Uub", 112, 277.0);
     theTable[Uuq] = ElementInfo("Uuq", 113, 289.0);
     
}
int main()
{
    PeriodicTable myTable;
    InitializePeriodicTable( myTable );
    
    int chemchoice, choicetwo;
    double choice, mass, grams, moles, atoms, x, molesk, ratio, molesu, massu, final;
    
    cout<<"\tChemistry Calculator 1.0\n\n";
    cout<<"---------------------------------------------------------------------\n";
    cout<<"1 - Mole/Gram/Atom Calculator\n";
    cout<<"2 - Stoichiometry Calculator\n";
    cout<<"---------------------------------------------------------------------\n";
    cout<<"\nPlease type in your choice (1 or 2). To find out more information about either \n";
    cout<<"choice, type 3: ";
    cin>>chemchoice;
    
    system("cls");
    switch (chemchoice)
    {
           case 1:
                cout<<"\t\n\n\nMoles -- Grams -- Atoms Converter\n\n\n";
 	cout<<"There is a list of choices below:\n\n";
 	cout<<"1 - Grams to Moles\n";
 	cout<<"2 - Grams to Atoms\n";
 	cout<<"3 - Moles to Grams\n";
 	cout<<"4 - Moles to Atoms\n";
 	cout<<"5 - Atoms to Moles\n";
 	cout<<"6 - Atoms to Grams\n";
 	cout<<"\nPlease enter the number of your choice: ";
 	cin>>choice;
 	
 	if (choice == 1)
 	{
		   		cout<<"\n\nPlease enter the atomic mass of the atom: ";
		   		cin>>mass;
		   		cout<<"\nPlease enter the number of grams of the atom: ";
		   		cin>>grams;
		   		moles = grams / mass;
		   		cout<<"\n\nThe number of moles of this atom is: "<<moles<<"\n\n\n";
				}
     else if (choice == 2)
     {
		   		cout<<"\n\nPlesase enter the grams of the atom: ";
		   		cin>>grams;
		   		cout<<"\nPease enter the atomic mass of the atom: ";
		   		cin>>mass;
		   		x = grams/mass;
		   		atoms = x * 6.022e+23;
		   		cout<<"\n\nThe number of atoms is: "<<atoms<<"\n\n\n";
				}
     else if (choice == 3)
     {
 		 cout<<"\n\nPlease enter the atomic mass of the atom: ";
 		 cin>>mass;
 		 cout<<"\nPlease enter the number of moles of the atom: ";
 		 cin>>moles;
 		 grams = moles*mass;
 		 cout<<"\n\nThe number of grams of the atom is "<<grams<<"\n\n\n";
		 }
	 else if (choice == 4)
	 {
	  	  cout<<"\n\nPlease enter the number of moles: ";
	  	  cin>>moles;
	  	  atoms = moles * 6.022e+23;
	  	  cout<<"\n\nThe number of atoms is: "<<atoms<<"\n\n\n";
		  }
     else if (choice == 5)
     {
	  	  cout<<"\n\nPlease enter the number of atoms (ex. 1.65*10^24 becomes 1.65e+24): ";
	  	  cin>>atoms;
	  	  moles = atoms / 6.022e+23;
	  	  cout<<"\n\nThe number of moles is: "<<moles<<"\n\n\n";
		  }
     else if (choice == 6)
     {
	  	  cout<<"\n\nPlease enter the number of atoms (ex. 1.65*10^24 becomes 1.65e+24): ";
	  	  cin>>atoms;
	  	  cout<<"\n\nPlease enter the atomic mass of the atom you want to convert: ";
	  	  cin>>mass;
	  	  moles = atoms / 6.022e+23;
	  	  grams = moles * mass;
	  	  cout<<"\n\nThe amount (in grams) of the atom is: "<<grams<<"\n\n\n";
		  }
     else {
                   cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
  }
    system ("pause");
    return 0;
    break;
    case 2:
         cout<<"\t\n\n\nStoichiometry Calculator\n\n\n";
 	cout<<"Enter the amount (in grams) of the known compound: ";
 	cin>>grams;
 	
 	cout<<"\nEnter the atomic mass of the known compound: ";
 	cin>>mass;
 	molesk=grams/mass;
 	
 	cout<<"\nEnter the mole ratio of UNKOWN compound to KNOWN compound (Use the coefficient \n";
	cout<<"from the balanced equation). Enter in decimal format: ";
 	cin>>ratio;
 	molesu=molesk*ratio;
 	
 	cout<<"\nEnter the atomic mass of the unkown compound (in grams): ";
 	cin>>massu;
 	final=molesu*massu;
 	cout<<"\nThe final answer is: "<<final<<"\n\n\n";
 	
 	system ("pause");
 	return 0;
 	break;
 	case 3:
         cout<<"Which would you like to find out more about?\n";
         cout<<"\n1 - The Mole/Gram/Atom Calculator\n";
         cout<<"2 - The Stoichiometry Calculator\n";
         cout<<"\nPlease type in your answer (1 or 2): ";
         cin>>choicetwo;
         }
  switch (choicetwo)
  {
         case 1:
              cout<<"\n\nThis calculator does many different conversions between Atoms, Grams and Moles.\n";
              break;
         case 2:
              cout<<"This calculator recieves your input and gives you the answer to a Stoichiometric problem.\n";
              break;
              }
   cout<<"\n\n\n---------------------------------------------------------------------\n";
    cout<<"1 - Mole/Gram/Atom Calculator\n";
    cout<<"2 - Stoichiometry Calculator\n";
    cout<<"---------------------------------------------------------------------\n";
    cout<<"\nPlease type in your choice (1 or 2). To find out more information about either \n";
    cout<<"choice, type 3: ";
    cin>>chemchoice;
    system("cls");
     switch (chemchoice)
    {
           case 1:
                cout<<"\t\n\n\nMoles -- Grams -- Atoms Converter\n\n\n";
 	cout<<"There is a list of choices below:\n\n";
 	cout<<"1 - Grams to Moles\n";
 	cout<<"2 - Grams to Atoms\n";
 	cout<<"3 - Moles to Grams\n";
 	cout<<"4 - Moles to Atoms\n";
 	cout<<"5 - Atoms to Moles\n";
 	cout<<"6 - Atoms to Grams\n";
 	cout<<"\nPlease enter the number of your choice: ";
 	cin>>choice;
 	
 	if (choice == 1)
 	{
		   		cout<<"\n\nPlease enter the atomic mass of the atom: ";
		   		cin>>mass;
		   		cout<<"\nPlease enter the number of grams of the atom: ";
		   		cin>>grams;
		   		moles = grams / mass;
		   		cout<<"\n\nThe number of moles of this atom is: "<<moles<<"\n\n\n";
				}
     else if (choice == 2)
     {
		   		cout<<"\n\nPlesase enter the grams of the atom: ";
		   		cin>>grams;
		   		cout<<"\nPease enter the atomic mass of the atom: ";
		   		cin>>mass;
		   		x = grams/mass;
		   		atoms = x * 6.022e+23;
		   		cout<<"\n\nThe number of atoms is: "<<atoms<<"\n\n\n";
				}
     else if (choice == 3)
     {
 		 cout<<"\n\nPlease enter the atomic mass of the atom: ";
 		 cin>>mass;
 		 cout<<"\nPlease enter the number of moles of the atom: ";
 		 cin>>moles;
 		 grams = moles*mass;
 		 cout<<"\n\nThe number of grams of the atom is "<<grams<<"\n\n\n";
		 }
	 else if (choice == 4)
	 {
	  	  cout<<"\n\nPlease enter the number of moles: ";
	  	  cin>>moles;
	  	  atoms = moles * 6.022e+23;
	  	  cout<<"\n\nThe number of atoms is: "<<atoms<<"\n\n\n";
		  }
     else if (choice == 5)
     {
	  	  cout<<"\n\nPlease enter the number of atoms (ex. 1.65*10^24 becomes 1.65e+24): ";
	  	  cin>>atoms;
	  	  moles = atoms / 6.022e+23;
	  	  cout<<"\n\nThe number of moles is: "<<moles<<"\n\n\n";
		  }
     else if (choice == 6)
     {
	  	  cout<<"\n\nPlease enter the number of atoms (ex. 1.65*10^24 becomes 1.65e+24): ";
	  	  cin>>atoms;
	  	  cout<<"\n\nPlease enter the atomic mass of the atom you want to convert: ";
	  	  cin>>mass;
	  	  moles = atoms / 6.022e+23;
	  	  grams = moles * mass;
	  	  cout<<"\n\nThe amount (in grams) of the atom is: "<<grams<<"\n\n\n";
		  }
     else {
                   cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
  }
    system ("pause");
    return 0;
    break;
    case 2:
         cout<<"\t\n\n\nStoichiometry Calculator\n\n\n";
 	cout<<"Enter the amount (in grams) of the known compound: ";
 	cin>>grams;
 	
 	cout<<"\nEnter the atomic mass of the known compound: ";
 	cin>>mass;
 	molesk=grams/mass;
 	
 	cout<<"\nEnter the mole ratio of UNKOWN compound to KNOWN compound (Use the coefficient \n";
	cout<<"from the balanced equation). Enter in decimal format: ";
 	cin>>ratio;
 	molesu=molesk*ratio;
 	
 	cout<<"\nEnter the atomic mass of the unkown compound (in grams): ";
 	cin>>massu;
 	final=molesu*massu;
 	cout<<"\nThe final answer is: "<<final<<"\n\n\n";
 	
 	system ("pause");
 	return 0;
 	break;
 	case 3:
         cout<<"Which would you like to find out more about?\n";
         cout<<"\n1 - The Mole/Gram/Atom Calculator\n";
         cout<<"2 - The Stoichiometry Calculator\n";
         cout<<"\nPlease type in your answer (1 or 2): ";
         cin>>choicetwo;
         }
         switch (choicetwo)
  {
         case 1:
              cout<<"\n\nThis calculator does many different conversions between Atoms, Grams and Moles.\n";
              break;
         case 2:
              cout<<"This calculator recieves your input and gives you the answer to a Stoichiometric problem.\n";
              break;
              }
              }


Was This Post Helpful? 0

#3 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 01 August 2011 - 09:43 AM

Hello Jovan,

Thank you for your comments:

View PostJovan, on 24 December 2010 - 01:05 AM, said:

So for a class I'm doing (don't worry, it's extra credit for a Chemistry class, not a coding class), I'm making a program that calculates atomic mass based on the user inputting the symbols and amounts of each element involved in the molecule.

As an example, if the molecule that the user wants the atomic mass of is H2O, then I want them to be able to input "H" into the program, and "2" for the amount, and "O" and "1". The program is then supposed to go through the map I've created and find those values and calculate the mass and spit it out.

Firstly, I want to know if this is possible? Secondly, can you show me what to do, based on the code I have here:

#include <iostream>
#include <string>
#include <map>
using namespace std;
struct ElementInfo
{
    std::string el_symbol;
    double el_mass;
    int el_number;

    ElementInfo( const std::string& name = "", 
                         int number = 0,
                         double weight = 0) : el_symbol(name),
                         el_mass(weight), el_number(number) {}
};

typedef std::map<int, ElementInfo> PeriodicTable;

enum { H, He, Li, Be, B, C, N, O, F, Ne, Na, Mg, Al, Si, P, S, Cl, Ar, K, Ca, Sc, Ti, V, Cr, Mn, Fe, Co, Ni, Cu, Zn, Ga, Ge, As, Se, Br, Kr, Rb, Sr, Y, Zr, Nb, Mo, Tc, Ru, Rh, Pd, Ag, Cd, In, Sn, Sb, Te, I, Xe, Cs, Ba, La, Ce, Pr, Nd, Pm, Sm, Eu, Gd, Tb, Dy, Ho, Er, Tm, Yb, Lu, Hf, Ta, W, Re, Os, Ir, Pt, Au, Hg, Tl, Pb, Bi, Po, At, Rn, Fr, Ra, Ac, Th, Pa, U, Np, Pu, Am, Cm, Bk, Cf, Es, Fm, Md, No, Lr, Rf, Db, Sg, Bh, Hs, Mt, Uun, Uuu, Uub, Uuq };

void InitializePeriodicTable( PeriodicTable& theTable)
{
     theTable[H] = ElementInfo("H", 1, 1.0079);
     theTable[He] = ElementInfo("He", 2, 4.0026);
     theTable[Li] = ElementInfo("Li", 3, 6.941);
     theTable[Be] = ElementInfo("Be", 4, 9.0122);
     theTable[B] = ElementInfo("B", 5, 10.811);
     theTable[C] = ElementInfo("C", 6, 12.011);
     theTable[N] = ElementInfo("N", 7, 14.007);
     theTable[O] = ElementInfo("O", 8, 15.999);
     theTable[F] = ElementInfo("F", 9, 18.998);
     theTable[Ne] = ElementInfo("Ne", 10, 20.180);
     theTable[Na] = ElementInfo("Na", 11, 22.990);
     theTable[Mg] = ElementInfo("Mg", 12, 24.305);
     theTable[Al] = ElementInfo("Al", 13, 26.982);
     theTable[Si] = ElementInfo("Si", 14, 28.086);
     theTable[P] = ElementInfo("P", 15, 30.974);
     theTable[S] = ElementInfo("S", 16, 32.065);
     theTable[Cl] = ElementInfo("Cl", 17, 35.453);
     theTable[Ar] = ElementInfo("Ar", 18, 39.948);
     theTable[K] = ElementInfo("K", 19, 39.098);
     theTable[Ca] = ElementInfo("Ca", 20, 40.078);
     theTable[Sc] = ElementInfo("Sc", 21, 44.956);
     theTable[Ti] = ElementInfo("Ti", 22, 47.867);
     theTable[V] = ElementInfo("V", 23, 50.942);
     theTable[Cr] = ElementInfo("Cr", 24, 51.996);
     theTable[Mn] = ElementInfo("Mn", 25, 54.938);
     theTable[Fe] = ElementInfo("Fe", 26, 55.845);
     theTable[Co] = ElementInfo("Co", 27, 58.933);
     theTable[Ni] = ElementInfo("Ni", 28, 58.693);
     theTable[Cu] = ElementInfo("Cu", 29, 63.546);
     theTable[Zn] = ElementInfo("Zn", 30, 65.39);
     theTable[Ga] = ElementInfo("Ga", 31, 69.723);
     theTable[Ge] = ElementInfo("Ge", 32, 72.61);
     theTable[As] = ElementInfo("As", 33, 74.992);
     theTable[Se] = ElementInfo("Se", 34, 78.96);
     theTable[Br] = ElementInfo("Br", 35, 79.904);
     theTable[Kr] = ElementInfo("Kr", 36, 83.80);
     theTable[Rb] = ElementInfo("Rb", 37, 85.468);
     theTable[Sr] = ElementInfo("Sr", 38, 87.62);
     theTable[Y] = ElementInfo("Y", 39, 88.906);
     theTable[Zr] = ElementInfo("Zr", 40, 91.224);
     theTable[Nb] = ElementInfo("Nb", 41, 92.906);
     theTable[Mo] = ElementInfo("Mo", 42, 95.94);
     theTable[Tc] = ElementInfo("Tc", 43, 98.0);
     theTable[Ru] = ElementInfo("Ru", 44, 101.07);
     theTable[Rh] = ElementInfo("Rh", 45, 102.91);
     theTable[Pd] = ElementInfo("Pd", 46, 106.42);
     theTable[Ag] = ElementInfo("Ag", 47, 107.87);
     theTable[Cd] = ElementInfo("Cd", 48, 112.41);
     theTable[In] = ElementInfo("In", 49, 114.82);
     theTable[Sn] = ElementInfo("Sn", 50, 118.71);
     theTable[Sb] = ElementInfo("Sb", 51, 121.76);
     theTable[Te] = ElementInfo("Te", 52, 127.60);
     theTable[I] = ElementInfo("I", 53, 126.90);
     theTable[Xe] = ElementInfo("Xe", 54, 131.29);
     theTable[Cs] = ElementInfo("Cs", 55, 132.91);
     theTable[Ba] = ElementInfo("Ba", 56, 137.33);
     theTable[La] = ElementInfo("La", 57, 138.91);
     theTable[Ce] = ElementInfo("Ce", 58, 140.12);
     theTable[Pr] = ElementInfo("Pr", 59, 140.91);
     theTable[Nd] = ElementInfo("Nd", 60, 144.24);
     theTable[Pm] = ElementInfo("Pm", 61, 145.0);
     theTable[Sm] = ElementInfo("Sm", 62, 150.36);
     theTable[Eu] = ElementInfo("Eu", 63, 151.96);
     theTable[Gd] = ElementInfo("Gd", 64, 157.25);
     theTable[Tb] = ElementInfo("Tb", 65, 158.93);
     theTable[Dy] = ElementInfo("Dy", 66, 162.50);
     theTable[Ho] = ElementInfo("Ho", 67, 164.93);
     theTable[Er] = ElementInfo("Er", 68, 167.26);
     theTable[Tm] = ElementInfo("Tm", 69, 168.93);
     theTable[Yb] = ElementInfo("Yb", 70, 173.04);
     theTable[Lu] = ElementInfo("Lu", 71, 174.97);
     theTable[Hf] = ElementInfo("Hf", 72, 178.49);
     theTable[Ta] = ElementInfo("Ta", 73, 180.95);
     theTable[W] = ElementInfo("W", 74, 183.84);
     theTable[Re] = ElementInfo("Re", 75, 186.21);
     theTable[Os] = ElementInfo("Os", 76, 190.23);
     theTable[Ir] = ElementInfo("Ir", 77, 192.22);
     theTable[Pt] = ElementInfo("Pt", 78, 195.08);
     theTable[Au] = ElementInfo("Au", 79, 196.97);
     theTable[Hg] = ElementInfo("Hg", 80, 200.59);
     theTable[Tl] = ElementInfo("Tl", 81, 204.38);
     theTable[Pb] = ElementInfo("Pb", 82, 207.2);
     theTable[Bi] = ElementInfo("Bi", 83, 208.98);
     theTable[Po] = ElementInfo("Po", 84, 209.0);
     theTable[At] = ElementInfo("At", 85, 210.0);
     theTable[Rn] = ElementInfo("Rn", 86, 222.0);
     theTable[Fr] = ElementInfo("Fr", 87, 223.0);
     theTable[Ra] = ElementInfo("Ra", 88, 226.0);
     theTable[Ac] = ElementInfo("Ac", 89, 227.0);
     theTable[Th] = ElementInfo("Tc", 90, 232.04);
     theTable[Pa] = ElementInfo("Pa", 91, 231.04);
     theTable[U] = ElementInfo("U", 92, 238.03);
     theTable[Np] = ElementInfo("Np", 93, 237.0);
     theTable[Pu] = ElementInfo("Pu", 94, 244.0);
     theTable[Am] = ElementInfo("Am", 95, 143.0);
     theTable[Cm] = ElementInfo("Cm", 96, 247.0);
     theTable[Bk] = ElementInfo("Bk", 97, 247.0);
     theTable[Cf] = ElementInfo("Cf", 98, 251.0);
     theTable[Es] = ElementInfo("Es", 99, 252.0);
     theTable[Fm] = ElementInfo("Fm", 100, 257.0);
     theTable[Md] = ElementInfo("Md", 101, 258.0);
     theTable[No] = ElementInfo("No", 102, 259.0);
     theTable[Lr] = ElementInfo("Lr", 103, 262.0);
     theTable[Rf] = ElementInfo("Rf", 104, 261.0);
     theTable[Db] = ElementInfo("Db", 105, 262.0);
     theTable[Sg] = ElementInfo("Sg", 106, 266.0);
     theTable[Bh] = ElementInfo("Bh", 107, 264.0);
     theTable[Hs] = ElementInfo("Hs", 108, 269.0);
     theTable[Mt] = ElementInfo("Mt", 109, 268.0);
     theTable[Uun] = ElementInfo("Uun", 110, 271.0);
     theTable[Uuu] = ElementInfo("Uuu", 111, 272.0);
     theTable[Uub] = ElementInfo("Uub", 112, 277.0);
     theTable[Uuq] = ElementInfo("Uuq", 113, 289.0);
     
}
int main()
{
    PeriodicTable myTable;
    InitializePeriodicTable( myTable );
    
    int chemchoice, choicetwo;
    double choice, mass, grams, moles, atoms, x, molesk, ratio, molesu, massu, final;
    
    cout<<"\tChemistry Calculator 1.0\n\n";
    cout<<"---------------------------------------------------------------------\n";
    cout<<"1 - Mole/Gram/Atom Calculator\n";
    cout<<"2 - Stoichiometry Calculator\n";
    cout<<"---------------------------------------------------------------------\n";
    cout<<"\nPlease type in your choice (1 or 2). To find out more information about either \n";
    cout<<"choice, type 3: ";
    cin>>chemchoice;
    
    system("cls");
    switch (chemchoice)
    {
           case 1:
                cout<<"\t\n\n\nMoles -- Grams -- Atoms Converter\n\n\n";
 	cout<<"There is a list of choices below:\n\n";
 	cout<<"1 - Grams to Moles\n";
 	cout<<"2 - Grams to Atoms\n";
 	cout<<"3 - Moles to Grams\n";
 	cout<<"4 - Moles to Atoms\n";
 	cout<<"5 - Atoms to Moles\n";
 	cout<<"6 - Atoms to Grams\n";
 	cout<<"\nPlease enter the number of your choice: ";
 	cin>>choice;
 	
 	if (choice == 1)
 	{
		   		cout<<"\n\nPlease enter the atomic mass of the atom: ";
		   		cin>>mass;
		   		cout<<"\nPlease enter the number of grams of the atom: ";
		   		cin>>grams;
		   		moles = grams / mass;
		   		cout<<"\n\nThe number of moles of this atom is: "<<moles<<"\n\n\n";
				}
     else if (choice == 2)
     {
		   		cout<<"\n\nPlesase enter the grams of the atom: ";
		   		cin>>grams;
		   		cout<<"\nPease enter the atomic mass of the atom: ";
		   		cin>>mass;
		   		x = grams/mass;
		   		atoms = x * 6.022e+23;
		   		cout<<"\n\nThe number of atoms is: "<<atoms<<"\n\n\n";
				}
     else if (choice == 3)
     {
 		 cout<<"\n\nPlease enter the atomic mass of the atom: ";
 		 cin>>mass;
 		 cout<<"\nPlease enter the number of moles of the atom: ";
 		 cin>>moles;
 		 grams = moles*mass;
 		 cout<<"\n\nThe number of grams of the atom is "<<grams<<"\n\n\n";
		 }
	 else if (choice == 4)
	 {
	  	  cout<<"\n\nPlease enter the number of moles: ";
	  	  cin>>moles;
	  	  atoms = moles * 6.022e+23;
	  	  cout<<"\n\nThe number of atoms is: "<<atoms<<"\n\n\n";
		  }
     else if (choice == 5)
     {
	  	  cout<<"\n\nPlease enter the number of atoms (ex. 1.65*10^24 becomes 1.65e+24): ";
	  	  cin>>atoms;
	  	  moles = atoms / 6.022e+23;
	  	  cout<<"\n\nThe number of moles is: "<<moles<<"\n\n\n";
		  }
     else if (choice == 6)
     {
	  	  cout<<"\n\nPlease enter the number of atoms (ex. 1.65*10^24 becomes 1.65e+24): ";
	  	  cin>>atoms;
	  	  cout<<"\n\nPlease enter the atomic mass of the atom you want to convert: ";
	  	  cin>>mass;
	  	  moles = atoms / 6.022e+23;
	  	  grams = moles * mass;
	  	  cout<<"\n\nThe amount (in grams) of the atom is: "<<grams<<"\n\n\n";
		  }
     else {
                   cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
  }
    system ("pause");
    return 0;
    break;
    case 2:
         cout<<"\t\n\n\nStoichiometry Calculator\n\n\n";
 	cout<<"Enter the amount (in grams) of the known compound: ";
 	cin>>grams;
 	
 	cout<<"\nEnter the atomic mass of the known compound: ";
 	cin>>mass;
 	molesk=grams/mass;
 	
 	cout<<"\nEnter the mole ratio of UNKOWN compound to KNOWN compound (Use the coefficient \n";
	cout<<"from the balanced equation). Enter in decimal format: ";
 	cin>>ratio;
 	molesu=molesk*ratio;
 	
 	cout<<"\nEnter the atomic mass of the unkown compound (in grams): ";
 	cin>>massu;
 	final=molesu*massu;
 	cout<<"\nThe final answer is: "<<final<<"\n\n\n";
 	
 	system ("pause");
 	return 0;
 	break;
 	case 3:
         cout<<"Which would you like to find out more about?\n";
         cout<<"\n1 - The Mole/Gram/Atom Calculator\n";
         cout<<"2 - The Stoichiometry Calculator\n";
         cout<<"\nPlease type in your answer (1 or 2): ";
         cin>>choicetwo;
         }
  switch (choicetwo)
  {
         case 1:
              cout<<"\n\nThis calculator does many different conversions between Atoms, Grams and Moles.\n";
              break;
         case 2:
              cout<<"This calculator recieves your input and gives you the answer to a Stoichiometric problem.\n";
              break;
              }
   cout<<"\n\n\n---------------------------------------------------------------------\n";
    cout<<"1 - Mole/Gram/Atom Calculator\n";
    cout<<"2 - Stoichiometry Calculator\n";
    cout<<"---------------------------------------------------------------------\n";
    cout<<"\nPlease type in your choice (1 or 2). To find out more information about either \n";
    cout<<"choice, type 3: ";
    cin>>chemchoice;
    system("cls");
     switch (chemchoice)
    {
           case 1:
                cout<<"\t\n\n\nMoles -- Grams -- Atoms Converter\n\n\n";
 	cout<<"There is a list of choices below:\n\n";
 	cout<<"1 - Grams to Moles\n";
 	cout<<"2 - Grams to Atoms\n";
 	cout<<"3 - Moles to Grams\n";
 	cout<<"4 - Moles to Atoms\n";
 	cout<<"5 - Atoms to Moles\n";
 	cout<<"6 - Atoms to Grams\n";
 	cout<<"\nPlease enter the number of your choice: ";
 	cin>>choice;
 	
 	if (choice == 1)
 	{
		   		cout<<"\n\nPlease enter the atomic mass of the atom: ";
		   		cin>>mass;
		   		cout<<"\nPlease enter the number of grams of the atom: ";
		   		cin>>grams;
		   		moles = grams / mass;
		   		cout<<"\n\nThe number of moles of this atom is: "<<moles<<"\n\n\n";
				}
     else if (choice == 2)
     {
		   		cout<<"\n\nPlesase enter the grams of the atom: ";
		   		cin>>grams;
		   		cout<<"\nPease enter the atomic mass of the atom: ";
		   		cin>>mass;
		   		x = grams/mass;
		   		atoms = x * 6.022e+23;
		   		cout<<"\n\nThe number of atoms is: "<<atoms<<"\n\n\n";
				}
     else if (choice == 3)
     {
 		 cout<<"\n\nPlease enter the atomic mass of the atom: ";
 		 cin>>mass;
 		 cout<<"\nPlease enter the number of moles of the atom: ";
 		 cin>>moles;
 		 grams = moles*mass;
 		 cout<<"\n\nThe number of grams of the atom is "<<grams<<"\n\n\n";
		 }
	 else if (choice == 4)
	 {
	  	  cout<<"\n\nPlease enter the number of moles: ";
	  	  cin>>moles;
	  	  atoms = moles * 6.022e+23;
	  	  cout<<"\n\nThe number of atoms is: "<<atoms<<"\n\n\n";
		  }
     else if (choice == 5)
     {
	  	  cout<<"\n\nPlease enter the number of atoms (ex. 1.65*10^24 becomes 1.65e+24): ";
	  	  cin>>atoms;
	  	  moles = atoms / 6.022e+23;
	  	  cout<<"\n\nThe number of moles is: "<<moles<<"\n\n\n";
		  }
     else if (choice == 6)
     {
	  	  cout<<"\n\nPlease enter the number of atoms (ex. 1.65*10^24 becomes 1.65e+24): ";
	  	  cin>>atoms;
	  	  cout<<"\n\nPlease enter the atomic mass of the atom you want to convert: ";
	  	  cin>>mass;
	  	  moles = atoms / 6.022e+23;
	  	  grams = moles * mass;
	  	  cout<<"\n\nThe amount (in grams) of the atom is: "<<grams<<"\n\n\n";
		  }
     else {
                   cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
  }
    system ("pause");
    return 0;
    break;
    case 2:
         cout<<"\t\n\n\nStoichiometry Calculator\n\n\n";
 	cout<<"Enter the amount (in grams) of the known compound: ";
 	cin>>grams;
 	
 	cout<<"\nEnter the atomic mass of the known compound: ";
 	cin>>mass;
 	molesk=grams/mass;
 	
 	cout<<"\nEnter the mole ratio of UNKOWN compound to KNOWN compound (Use the coefficient \n";
	cout<<"from the balanced equation). Enter in decimal format: ";
 	cin>>ratio;
 	molesu=molesk*ratio;
 	
 	cout<<"\nEnter the atomic mass of the unkown compound (in grams): ";
 	cin>>massu;
 	final=molesu*massu;
 	cout<<"\nThe final answer is: "<<final<<"\n\n\n";
 	
 	system ("pause");
 	return 0;
 	break;
 	case 3:
         cout<<"Which would you like to find out more about?\n";
         cout<<"\n1 - The Mole/Gram/Atom Calculator\n";
         cout<<"2 - The Stoichiometry Calculator\n";
         cout<<"\nPlease type in your answer (1 or 2): ";
         cin>>choicetwo;
         }
         switch (choicetwo)
  {
         case 1:
              cout<<"\n\nThis calculator does many different conversions between Atoms, Grams and Moles.\n";
              break;
         case 2:
              cout<<"This calculator recieves your input and gives you the answer to a Stoichiometric problem.\n";
              break;
              }
              }



If your program still does not work, please post your questions in the DIC forum and either I or someone else will help you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1