Been working on this project for the past two days and tried googling for solutions and about every 5th solution led me here, so I decided to join and see if I could get my question answered.

Here's what I need to do, this is a semester long project for creating a menu system for a fictional bookstore. Up until now, every screen has been its own separate project, this week, we need to hook up everything to the main menu screen.

Now, for a parameter, my professor forced us to make it a multi file program instead of throwing everything together in a switch statement and making life easier. Everything is written, and my problem is modifying the switch statement towards the end to access the other files

#include <iostream>
#include <fstream>
#include "bookinfo.h"
#include "cashier.h"
#include "invmenu.h"
#include "reports.h"

using namespace std;

int main()
{
	ofstream outputFile;5
	ifstream inputFile;
	int choice;

	do
	{

	cout << "\n" << "\t\t\tSerendipity Booksellers" << "\n\n\n";
	cout << "\t\t\t\tMain Menu" << "\n\n\n";
	cout << "\t\t\t1.Cashier Module" << "\n";
	cout << "\t\t\t2.Inventory Database Module" << "\n";
	cout << "\t\t\t3.Report Module" << "\n";
	cout << "\t\t\t4.Exit" << "\n\n\n";
	cout << "\t\t\tEnter Your Choice:";
	cin >> choice;
	
	while (choice < 1 || choice > 4)
	{
		cout << "Please Enter a number between the range 1-4.\n";
		cout << "Enter Your choice:";
		cin >> choice;
	}
	switch (choice)
		{ 
		case '1': outputFile.open("C:\\Users\\Owner\\Desktop\\Documents\\C++ Projects\\Assignments\\Serendipity\\Main Menu Screen\\Main Menu Screen\\Cashier Screen.cpp");
				if (!inputFile)
					  cout << "Error Opening file.\n";
				break;
		case '2': invmenu();
				break;
		case '3': reports();
				break;
		case '4': cout << "\n\t\t\tEnding Program.\n";
				break;
		}
	}while (choice != 4);

	return 0;
}

Any help would be much appreciated.

Member Avatar for r.stiltskin
case '1': outputFile.open("C:\\Users\\Owner\\Desktop\\Documents\\C++ Projects\\Assignments\\Serendipity\\Main Menu Screen\\Main Menu Screen\\Cashier Screen.cpp");

Are you planning on having your program write something to a file with that name? (I don't think so.)

A multi-file program doesn't involve anything like opening up other source code files while the program is running. You write your functions just the same as if you were writing a single-file program, but you save some of them in different files, and use header files to contain prototypes of the functions. Then you compile all of the implementation (.cpp) files together into a single program.

Let me see if I can explain myself a bit further, what I need this program to do is when I select one of the three options, I need another screen I have already written to pop up and start accepting inputs. So if I chose the cashier option, I need that screen to open up and start asking for quantity of books purchased and their respective prices.

I may have used the wrong wording, my textbook did a pretty bad job explaining how to write the code for doing this.

I have the headers written with all the prototypes of the other functions and complied those, and they work perfectly. The screens I need to access are in the same project, in the same sources folder as the main menu solution, I just need to access the functions contained within the other screens. Still getting used to the new vocab that comes along with coding, so if I use a word incorrectly, forgive me :icon_smile: .

Member Avatar for r.stiltskin

You shouldn't have to write anything differently. You just include the header file for the function you want to call in the implementation file of the calling function. The calling function (let's say main() ) calls the called function (e.g. cashierScreen() ) the same as if they were all in the same file. If everything is in the same project, and the project is configured correctly you just "build" the entire project and run it.

What I'm trying to say is, when I build this, the file just loops, it doesn't call the cashier screen, it just displays the main menu screen again and waits for your input. I can't figure out another way to call the cashier file other than the way I already have written.

Member Avatar for r.stiltskin

I've already told you twice, but you don't want to believe me. I'll try again: You don't call the cashier file. You simply call the function cashiermenu(), or whatever it's called, exactly the same as if it was in the same file as main().

Member Avatar for r.stiltskin

PS: multiple program files are not input/output files. When your program is in a single file you don't "call" that file in the program, do you? Well, in a multi-file program you don't "call" these files either. When you compile the program you end up with a single executable program in a single file, even though the source code is broken up into several files.

And, given what your main() function does in this program, there is absolutely no reason for your main() function to have an ofstream objecte or an ifstream object or to include <fstream>.

Ah, ok, sorry sometimes it takes a few times to get an idea into my skull, just needed to clear up that I needed to call on my function instead of the entire solution. Thanks a bunch!

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.