Thread: returning 0 doesnt quit out of the function

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    returning 0 doesnt quit out of the function

    Code:
        if(testFile.good())
        {
           testFile.close();
           
           int samefile;
           
           cout<<"\nThere is a file at the current save location with the same name...";
           cout<<"\n  <1>Overwrite";
           cout<<"\n  <2>Rename Current File";
           cout<<"\n  <3>Quit";
           cout<<"\n  ->: ";
           cin>>samefile;
           switch(samefile)
           {
                           case 1:
                                File.open(filename.c_str(), ios::out|ios::trunc);
                                File.close();
                                break;
                           case 2:
                                filename.clear();
                                cout<<"\nNew File Name: ";
                                cin>>filename;
                                filename.append(".txt");
                                break;
                           case 3:
                                Quit();
                                break;
                           default:
                                   cout<<"\nNot a valid entry!";
                                   cout<<"\n  ->: ";
           }
        }
        
        File.open(filename.c_str(), ios::out);      
        if(File.bad())
        {
                    cout<<"File could not be created!";
                    Quit();
        }
        else
        {
            cout<<"\t\t\t\'"<<filename<<"\' was created!!"<<endl;
            cout<<setfill('-')<<setw(80)<<"-"<<endl;
        }
    ok my problem is this...
    if the user enter 3 as a choice from the menu... it is suppose to quit...
    all my quit function does is return 0;
    but... instead... it goes on and creates the file anyway... even if i said return 0;...
    is there another way to quit out of the whole function all together?
    my objective is to quit if the user wants to quit... but if not create file...


    should i do something like this?
    Code:
    switch(samefile)
           {
                           case 1:
                                File.open(filename.c_str(), ios::out|ios::trunc);
                                File.close();
                                break;
                           case 2:
                                filename.clear();
                                cout<<"\nNew File Name: ";
                                cin>>filename;
                                filename.append(".txt");
                                break;
                           case 3:
                                done=true;
                                break;
                           default:
                                   cout<<"\nNot a valid entry!";
                                   cout<<"\n  ->: ";
           }
        }
        
         if(done==true)
        {
             Quit();
        }
        File.open(filename.c_str(), ios::out);      
        if(File.bad())
        {
                    cout<<"File could not be created!";
                    Quit();
        }
        else
        {
            cout<<"\t\t\t\'"<<filename<<"\' was created!!"<<endl;
            cout<<setfill('-')<<setw(80)<<"-"<<endl;
        }
    Last edited by gL_nEwB; 05-12-2006 at 06:01 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    return 0 in the Quit() function will simply return 0 from that function. You may want exit(0);

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    No. If I remember, all this code was in another function of yours, void MCreateFile(), right?

    If your user chooses 3, you want to exit MCreateFile() and return to main().

    Just type return; and forget your Quit() function. It doesn't do what you want, if you called it will do absolutely nothing.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There's really no need for the done variable at all. Just call return; for case 3.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    A function call in your code is replaced by the function's return value, so if you have this code:
    Code:
    case 3:
             Quit();
             break;
    and Quit() returns 0, then after Quit() exectues, your code is converted to:
    Code:
    case 3:
             0;
             break;
    Last edited by 7stud; 05-12-2006 at 10:40 PM.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    o ok...thats worked... thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM