I am trying to make a reference program that you can use to access any element of the periodic table based on its name, symbol, atomic number, or atomic mass. It is more than one file, and I need a way to return to main if the user wants to. How can I do that without actually making a function call to main()? all i want to do is make the program restart from the very beginning.

main() IS the entrypoint of the program. Its like saying, "I would like to ride around the block on a bike, but skip getting on the bike".

Why is it more than one file? Can you combine all the functions into one file, or just use headers to contain your functions?

I am using headers to include all of the functions, but I cannot wrap a loop around an entire program that is contained in more than one file. But I figured it out, I made a duplicate function called main1() and included the same syntax as that in main(), and from there I could re reference main1() over and over and in effect restart the program over and over, like re referencing main(), but not. it works

I am using headers to include all of the functions, but I cannot wrap a loop around an entire program that is contained in more than one file. But I figured it out, I made a duplicate function called main1() and included the same syntax as that in main(), and from there I could re reference main1() over and over and in effect restart the program over and over, like re referencing main(), but not. it works

Sounds like a regular old recursive function. Yes, main should not be called from itself, but it's perfectly fine to call another function from itself.

void DoSomething()
{
    // do whatever it is that this function does

    DoSomething(); // do it again
}


int main()
{
    DoSomething();
    return 0;
}

Or, non-recursively:

void DoSomething()
{
    // do whatever it is that this function does
}


int main()
{
    while(true)
    {
        DoSomething();
    }
    return 0;
}

These are infinite loops, but you can change it around and put some graceful exit conditions into the flow.

But don't do this:

int main()
{
    // do something

    main();
    return 0;
}

If you truly want to call main, you can fork into a child process, but that's probably a whole different topic.

Yeah, thats pretty much what I needed it to do. I figured it out shortly after I posted this, but am having some trouble beyond that.

void ElementSymbol()
{
	cout << "\nFrom here you can search for an element by its symbol.\n";
	sleep(1.5);
	cout << "\nYou can return to the main menu by searching 'Menu'.\n";
	sleep(1.5);
	while(true)
	{
	cout << "\nEnter an element's symbol below:\t(Case Sensitive!!)\n\n";
	string elementsymbol;
	getline(cin, elementsymbol);
	if(elementsymbol == "H")
		{hydrogen(); sleep(1); returnmenuword(); sleep(2);}
	else if(elementsymbol == "He")
		{helium(); sleep(1); returnmenuword(); sleep(2);}
	else if(elementsymbol == "Li")
		{lithium(); sleep(1); returnmenuword(); sleep(2);}
	else if(elementsymbol == "Be")
		{beryllium(); sleep(1); returnmenuword(); sleep(2);}
	else if(elementsymbol == "B")
		{boron(); sleep(1); returnmenuword(); sleep(2);}
	else if(elementsymbol == "C")
	{carbon(); sleep(1); returnmenuword(); sleep(2);}
	else if(elementsymbol == "N")
		{nitrogen(); sleep(1); returnmenuword(); sleep(2);}
	else if(elementsymbol == "menu" || elementsymbol == "Menu")
	{main1(); break;}
	else {cout << "Element Symbol not recognized - please enter a different symbol."; sleep(2); }
	}
}

That is one of my functions for identifying an element based on its element symbol. This is main1(); the function that is the same as main and starts the program over again so you can choose again which parameter to search by:

#include <iostream>
#include "Functions.h"
#include "Elements.h"

using namespace std;

void AtomicMass();
void AtomicNumber();
void ElementName();
void ElementSymbol();

int main1() 
{
		cout << "Welcome back to the Main Menu. \n\n";
		//cin.get();
		cout << "\n";
		cout << "From here you can search for elements based on their:\n-Atomic Number" <<
		"\n-Atomic Mass\n-Element Name\n-Element Symbol."<< endl;
		cout << "\nBy which parameter would you like to search?\n\n";
		string parameter;
		getline(cin, parameter, '\n');
		if(parameter == "Atomic Number" || parameter == "atomic number")
		{AtomicNumber();}
		else if(parameter == "Atomic Mass" || parameter == "atomic mass")
		{AtomicMass();}
		else if(parameter == "Element Name" || parameter == "element name" ||
				parameter == "Name" || parameter == "name")
		{ElementName();}
		else if(parameter == "Element Symbol" || parameter == "element symbol" ||
				parameter == "symbol" || parameter == "Symbol")
		{ElementSymbol();}
		else
		{ 
			cout << "Error - command not recognized. Returning to main menu...\n\n";
			main1();
		return 0;
	}
}

#endif

The loop aspect of it works, where typing in 'menu' as an element symbol and pressing enter returns you to this menu, but without the cin.get(); i have inserted as a comment, the function acts as though you had entered nothing then pressed enter, and gives the "Error - command not recognized. Returning to main menu..." message, then loops back around to the beginning of main1(); again and works normally, but its not pretty having it say it will return you to the main menu, then have all that happen and finally be able to re-search for an element. Do you understand what Im saying?

How many times do you want to be able to search? Once per program or is supposed to keep asking you till you quit?

A plain old loop will work just fine here. You don't even need any functions beyond main besides the functions that look up the elements.

int main()
{
  while(true)
  {
    // display the menu options, including quit

    bool error = false;

    do
    {
      // prompt the user for input and read it in

      // long if/else if/else loop
      // if and else if statements checking for valid options

      if(/* check for quit */)
      {
        cout << "Have a nice day!\n";
        return 0;
      }
      // else if statements for valid options

      else
      {
        cout << "Bad input.  Please try again.\n";
        error = true;
      }
    }
    while(error);
  }

  return 0; // we'll never get here but it's good practice to stick it in.
}

This is the kind of program flow you are looking for. Error checking, a way for the user to "play again", a way to ask for new data without re-displaying the menu, and a way to exit.

In your ElementSymbol class, you shouldn't call main1 when the user asks for menu. Just a simple "return" statement suffices because after all, main1 is what called the function. If you do it the way I set it up, it'll return into the menu anyway. The way you have it, you're going to end up with a really big call stack behind the scenes. You'll probably never run it enough times to have a problem, but you'll end up with a huge call stack (i.e. functions are called, but there's not a return).

Not sure what the cin.get() issue you are having is. If you use getline and you also use cin using the >> operator, the >> operator will leave the newline in the stream and then getline will grab it and not pause. Look at the thread pinned at the top of the forum about how to flush the input stream and flush that stream before any getline statements. See if the problem goes away.

>> // we'll never get here but it's good practice to stick it in.
Thats what she said!

Sorry I had to!

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.