CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2002
    Location
    Birmingham UK
    Posts
    83

    Problem with me logics again

    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
    Waheed Rafiq

    www.waheedrafiq.com


    MCP x 2 , Network+.CNNA

  2. #2
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    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.

    You're gonna go blind staring into that box all day.

  3. #3
    Join Date
    Dec 2002
    Location
    Birmingham UK
    Posts
    83
    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.
    Waheed Rafiq

    www.waheedrafiq.com


    MCP x 2 , Network+.CNNA

  4. #4
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    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.

    Code:
    #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;
    }
    You're gonna go blind staring into that box all day.

  5. #5
    Join Date
    Dec 2002
    Location
    Birmingham UK
    Posts
    83
    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
    Waheed Rafiq

    www.waheedrafiq.com


    MCP x 2 , Network+.CNNA

  6. #6
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    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.

    You're gonna go blind staring into that box all day.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured