Click to See Complete Forum and Search --> : Problem with me logics again


waheedrafiq
January 28th, 2004, 05:20 AM
Ok guy's from my understanding you can call the main() function from any other function are i'm correct in saying this


example.


int displaymenu(int a)
{

// main menu is display


now return back to main();



}




main()
{

displaymenu();




}
would the above work if so why and if not why

dude_1967
January 28th, 2004, 06:55 AM
WDQ,

I believe that you are speaking of a fundamental mechanism in the C++ language: Simply the ability to call a subroutine and then return to the calling point.

However, your question is a bit unclear since you speak of calling 'main()'. It is not common practice to call main and in my opinion this should never be done.

Yet in your code example you simply call the display subroutine from main and it seems that you expect to return to main. Well, your expectation is correct. The compiler will call the display subroutine and, upon completion, simply return to the point just after the calling line.

...or did I miss the point of the question?

Sincerely, Chris.

:)

waheedrafiq
January 28th, 2004, 07:17 AM
NO you did not miss the point of the question and i thank you for replying to this post i understand that you can't call main() function from a function


my problem is witht his code that i am experiementing with and learning as i go along the way.


here is my code.


#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <stdlib.h>
#include <fstream> // for opening and creating files/ append


using namespace std;




int view()
{
char buffer[256];

ifstream examplefile ("example.txt");
if( ! examplefile.is_open())
{
cout<<"Error opening file"; exit (1); }
while(! examplefile.eof())
{
examplefile.getline(buffer,100);
cout<< buffer <<endl;
}
return 0;

}
// clear screen function needs to be added

char add(string a) // ADD USERS TO THE DATA BASE
{
int age,M,menu,wait;
string name;
int displaymenu;
char b;



// initise M
M = 0;


cout<<"******************"<<"\n";
cout<<"** Add menu **"<<"\n";
cout<<"******************"<<"\n";

ofstream examplefile ("example.txt");










cout<<"Please enter your name--->";
cin>>name;

cout<<"Please enter your age---->";

cin>>age;


cout<<"Your name is:"<<name<<"& your age is:"<<age;

cout<<"\n";

//while need a loop here if not I = M then loop unitl true


if(examplefile.is_open())
{
examplefile <<name<<"\n";
examplefile <<age<<"\n";
examplefile.close();
}





cout<<"Press M to go back to the main menu:"<<"\n";

do
{
M = getch();
if ( M == 77 )
cout <<" Going back to the Main menu"<<endl;
main(); // i accept it to go to main and bring up the menu

else
cout<<" Incorrect input please Enter M"<<endl;
}while(M !=77); /* check to see if M is press if press go to the main
menu */

}// end of Add function















main(int argc, string *argv[])
{
// char name[20],data[20];
string data,name;
char b;

int age,num1,num2,total,choice;


cout<<"----------------------------"<<"\n";
cout<<"-- Data base program 1.1 --"<<"\n";
cout<<"----------------------------"<<"\n";


cout<<"\n";
cout<<"***********************************"<<"\n";
cout<<" 1) Add data "<<"\n";
cout <<" 2) view data"<<endl;
cout <<" 3) Exit"<<"\n";
cout <<"***********************************"<<"\n";



cout<<"Enter choice:"<<"\n";
cin>>choice;


switch (choice)
{
case 1:
data= add(name); // call add data function
break;
case 2:

cout<<"\n";
view(); // call view database function

cout<<"\n";



cout<<"-------------------------"<<"\n";

break;
case 3:
cout<<"\n";
cout<<" Good bye and thanks for using"<<"\n";
cout<<" D.C.S database program"<<"\n";
break;
default:
cout<<" Wrong choice";
}








} // end of menu function.

dude_1967
January 28th, 2004, 09:49 AM
WDQ,

Your goals can easily be accomplished with a simple loop in main.

Please take a look at the simplified example provided below.

You will get some ideas from the sample.

Good luck.

Sincerely, Chris.

P.S. The simplified view function has not yet been fully implemented.



#include <iostream>
#include <fstream>
#include <string>

using namespace std;

static const string str_database_name("w:\\test\\data.txt");

static void view(void)
{
ifstream is(str_database_name.data());

if(is.is_open())
{
// TBD: Do something to show the lines...


is.close();
}

}

static void add(void)
{
int age;
string name;

cout << "Enter name: ";
cin >> name;

cout << "Enter age: ";
cin >> age;

ofstream os(str_database_name.data(), ios::out | ios::app);

if(os.is_open())
{
os << name << ", " << age << endl;
os.close();
}
}

int main(int argc, string *argv[])
{
ifstream is(str_database_name.data());

if(!is.is_open())
{
ofstream os(str_database_name.data());

if(os.is_open())
{
os.close();
}
}

bool exit = false;

while(!exit)
{

cout << "----------------------------" << endl;
cout << "-- Data base program 1.1 ---" << endl;
cout << "----------------------------" << endl;

cout << endl;
cout << "***********************************"<<"\n";
cout << " 1) Add data "<<"\n";
cout << " 2) view data"<<endl;
cout << " 3) Exit"<<"\n";
cout << "***********************************"<<"\n";

char choice;
cout << "Enter choice: ";
cin >> choice;

switch (choice)
{
case '1':
::add();
break;

case '2':
::view();
break;

case '3':
cout << endl;
cout << " Good bye and thanks for using" << endl;
cout << " D.C.S database program" << endl;
exit = true;
break;

default:
cout << " Wrong choice";
break;
}
}

return 0;
}

waheedrafiq
January 28th, 2004, 10:48 AM
Are you a teacher or a Professor , your code is so clear and easy to read compare to mine and i understand it.

the main thing that i like about your code is that you have the main display within the main function and then all you did was added a loop and from that loop you call the sub functions



wow i mean i really understand how your code works once i have completed learning c++ i shall almost in definately write a book :)


thanks guru


Waheed Rafiq

dude_1967
January 28th, 2004, 11:09 AM
WDQ,

Many thanks for the compliment.

My profession is technical management of a software development group and I have been programming for nearly 20 years in several languages.

In my group one of our goals is to provide legible code of high quality and we have spent considerable effort improving this base practice over the years.

With time I'm sure that you will develop a suitable style of your own.

I think that many experienced programmers consider code to be a form of modern individual expression.

There are also many other board members with considerably more experience than I.

Sincerely, Chris.

:)