So i'm making this periodic table kinda program here mainly outta boredom. But I came across a problem, when I type in the name for the element, the program just closes. Any ideas on how to get it to work properly. Here's my current code.

#include <iostream>
#include <windows.h>
#include <stdlib.h>
using namespace std;

char elementname[100];
char mainmenuyesno[20];
int consoletextcolour;
HANDLE hConsole;
int main () {

	consoletextcolour = 234;
	SetConsoleTextAttribute(hConsole, consoletextcolour);
	mainmenu:
	cout <<"Welcome to Valestrom's Periodic Table"<<endl;
	Sleep(5000);
	cout <<"Please report any problems to Blackmethod@gmx.com"<<endl;
	Sleep(7500);
	system("cls");
	cout <<"Valestrom's Periodic table. By Blackmethod and Valestrom"<<endl;
	cout <<"Press ENTER/RETURN to continue";
	cin.get();
	system("cls");
	cout <<"Valestrom's periodic table V1.0"<<endl;
	cout <<"Type the name of an element to get info on it: ";
	cin.getline(elementname, 100);



	if (elementname == "Lithium" || elementname == "Li")
		do {
		system("cls");
	cout <<"LITHIUM (Li): Atomic Weight: 6.941" <<endl;
	cout <<"Ionic Charge: +1, Melting Point: 180.54 C" <<endl;
	cout <<"Boiling Point: 1342 °C"<<endl;
	cout <<"Return to main menu? Yes/No: ";
	cin.getline (mainmenuyesno, 20);
	if (mainmenuyesno == "Yes")
		goto mainmenu;
	else 
		exit(1);
		} while (elementname == "Lithium" || elementname == "Li");
	
	
	
	if (elementname == "Hydrogen" || elementname == "H")
		do {
		
	system("cls");
	cout <<"HYDROGEN (H): Atomic Weight: 1.00794" <<endl;
	cout<<"Ionic Charge: +1 -1, Melting Point: -259.14 C" <<endl;
	cout<<"Boiling Point: -252.87 C"<<endl;
	cout <<"Return to main menu? Yes/No: ";
	cin.getline (mainmenuyesno, 20);
	if (mainmenuyesno == "Yes")
		goto mainmenu;
	else 
		exit(1);
		} while (elementname == "Hydrogen" || elementname == "H");







end:
	return 0;
}

Ummm First off dont Use Goto because it skips over a bunch of code... Use function-prototypes or else make a function with the mainmenu before int main().. Second why not use Switches and Cases?

I will test it..

Edit: Your problem was the GoTo.. it skips over the code.. and whatever was already inputted, stays in the input buffer.. Clearing the buffer for me is always a pain in the ass so instead Just made a function called Mainmenu(); and called it whenever..

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

string elementname;
string mainmenuyesno;
int consoletextcolour;
HANDLE hConsole;

void Mainmenu()
{
    consoletextcolour = 234;
	SetConsoleTextAttribute(hConsole, consoletextcolour);
	cout <<"Welcome to Valestrom's Periodic Table"<<endl;
	cout <<"Please report any problems to Blackmethod@gmx.com"<<endl;
	system("cls");
	cout <<"Valestrom's Periodic table. By Blackmethod and Valestrom"<<endl;
	system("cls");
	cout <<"Valestrom's periodic table V1.0"<<endl;
	cout <<"Type the name of an element to get info on it: ";
	cin>>elementname;
}

int main () {

	Mainmenu();



	if (elementname == "Lithium" || elementname == "Li")       //Try to use Cases instead of if this and if that and if the other..
		do {
		system("cls");
	cout <<"LITHIUM (Li): Atomic Weight: 6.941" <<endl;
	cout <<"Ionic Charge: +1, Melting Point: 180.54 C" <<endl;
	cout <<"Boiling Point: 1342 °C"<<endl;
	cout <<"Return to main menu? Yes/No: ";
	cin>>mainmenuyesno;
	if (mainmenuyesno == "Yes")
		Mainmenu();
	else 
		exit(1);
		} while (elementname == "Lithium" || elementname == "Li");
	
	
	
	if (elementname == "Hydrogen" || elementname == "H")
		do {
		
	system("cls");
	cout <<"HYDROGEN (H): Atomic Weight: 1.00794" <<endl;
	cout<<"Ionic Charge: +1 -1, Melting Point: -259.14 C" <<endl;
	cout<<"Boiling Point: -252.87 C"<<endl;
	cout <<"Return to main menu? Yes/No: ";
	cin>>mainmenuyesno;
	if (mainmenuyesno == "Yes")
		Mainmenu();
	else 
		exit(1);
		} while (elementname == "Hydrogen" || elementname == "H");

	return 0;
}

P.S. You may also want to put a loop like a do-while loop in order to make the program repeat itself.. because after u enter the first element, you go back to the main menu, enter the second one and it will return 0; or you can easily add the element information procedure to another function and call that in Mainmenu to avoid a stackoverflow.

commented: Damn good help. +1

Can you give an example of how a case could replace the "if" statement and such?

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

string elementname;
string mainmenuyesno;
int consoletextcolour;
HANDLE hConsole;

void Mainmenu()
{
    consoletextcolour = 234;
	SetConsoleTextAttribute(hConsole, consoletextcolour);
	cout <<"Welcome to Valestrom's Periodic Table"<<endl;
	cout <<"Please report any problems to Blackmethod@gmx.com"<<endl;
	system("cls");
	cout <<"Valestrom's Periodic table. By Blackmethod and Valestrom"<<endl;
	system("cls");
	cout <<"Valestrom's periodic table V1.0"<<endl;
	cout <<"Type the name of an element to get info on it: ";
}

int main () {
do{
    
	Mainmenu();
    cin>>elementname;
	if (elementname == "Lithium" || elementname == "Li")
    {
	 system("cls");
	 cout <<"LITHIUM (Li): Atomic Weight: 6.941" <<endl;
	 cout <<"Ionic Charge: +1, Melting Point: 180.54 C" <<endl;
	 cout <<"Boiling Point: 1342 °C"<<endl;
	 cout <<"Return to main menu? Yes/No: ";
	 cin>>mainmenuyesno;
        if (mainmenuyesno == "Yes")
		   Mainmenu();
	    else 
           exit(1);
	}
	else if (elementname == "Hydrogen" || elementname == "H")
	{
	 system("cls");
	 cout <<"HYDROGEN (H): Atomic Weight: 1.00794" <<endl;
	 cout<<"Ionic Charge: +1 -1, Melting Point: -259.14 C" <<endl;
	 cout<<"Boiling Point: -252.87 C"<<endl;
	 cout <<"Return to main menu? Yes/No: ";
	 cin>>mainmenuyesno;
	   if (mainmenuyesno == "Yes")
		  Mainmenu();
	   else 
          exit(1);
	}
		
}while(mainmenuyesno == "Yes");
	return 0;
}

Well you would have to make a map of the strings or Chars since Switch() only accepts integrals and chars. and thats kinda too much work to make a map of it all.. If it was C# then sure you can do something like:

cout<<"Enter Element Name";
cin>>elementname;
switch(elementname)
{
   case "Lithium": cout<<"fdgsdg";        <---- Notice the strings "Lithium".. In c++ u can only do that for integers and chars.
                   break;
   case "Hydrogen": cout<<"fdgdsgshgf";   <---- Case of string "Hydrogen"..    that means if the user enters that for the element name, then do...
                   break;

    default: cout<<"That element doesnt exist";
                   break;
    break;
}
return 0;

Well thanks, you got it working. Thumbs up to you =D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.