this program is just messing around with file i/o, and then i stumbled across something that i¨ve clearly forgot or missed in the tutorials i've read. my 'guess' is its the to if statements messing up (or perhaps rather their else's)
i know i used goto and that means i am a moron so comments about that are welcomme too (as for anything else i may have done in wierd ways:P )

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream ud;
    ud.open ("C:\\Documents and Settings\\Happy\\Skrivebord\\testfile.txt", ios::app);
    crashstart: //for the goto later on
    string userin;
    cout<< "\ninput for file\n";
    cin >> userin;
    ud << userin;
    ud.close();
    if (ud.is_open()) //this is where trouble begins i think, altso i //dont know of this is neccesary but i want to do it just to get the //hang of it. 
    {
        cout << "\nur screwed, the file cant close, shutdown anyway? y/n \n";
        string shutdownyn,y="y", Y="Y", n="n", N="N";
        cin >> shutdownyn;
        if (shutdownyn==y || shutdownyn==Y)
        {
            goto faseout;
        }
        else
        {
            goto crashstart;
        }
    else //this one right here is the little satan i believe?
    {
        goto faseout;
    }
    }
    faseout:
    cout << "\nprogram terminated\n";
    cin.get();
    return 0;
}

Recommended Answers

All 12 Replies

this program is just messing around with file i/o, and then i stumbled across something that i¨ve clearly forgot or missed in the tutorials i've read. my 'guess' is its the to if statements messing up (or perhaps rather their else's)
i know i used goto and that means i am a moron so comments about that are welcomme too (as for anything else i may have done in wierd ways:P )

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream ud;
    ud.open ("C:\\Documents and Settings\\Happy\\Skrivebord\\testfile.txt", ios::app);
    if (ud.is_open())
    {
        cout << "\nur screwed, the file cant close, shutdown anyway? y/n \n";
        string shutdownyn,y="y", Y="Y", n="n", N="N";
        cin >> shutdownyn;
        if (shutdownyn==y || shutdownyn==Y)
        {
            goto faseout;
        }
        else
        {
            goto crashstart;
        }
    } // MISSED CLOSE } ON THE IF
    else
    {
        goto faseout;
    }
    //} // ?

    crashstart:
    string userin;
    cout<< "\ninput for file\n";
    cin >> userin;
    ud << userin;

    faseout:
    ud.close();
    cout << "\nprogram terminated\n";
    cin.get();
    return 0;
}

lol dumb error np : /

eh if u dident notice i changed it in the quote ^^

also a few errors..
you are adding stuff to the file before u checked if it was open.
and you closed it and then checked if it was open... duh?

LOL it took me a while to realise even after reading your post :P you're right, kind of a noob mistake, thank you :D

i also edited some other problems, the one on my post is complete fixing a bunch of problems u had, also try next time you program, not to use goto's, figure out a way around them. : )

Don't use gotos, there is break; and things called functions.
Why are you closing it if it doesn't open?

Re-write it.

I don't check if it is open because the program is not intented for anything:-) just messing around, getting the hang of I'd.

What do you suggest I do about the gotos? I knew you would't like it,but why?

Sorry dor any misspelling, I'm writing from a mobile device

ofstream file("name", ios::app);

if(file.is_open())
{
    cout << "Opened." << endl;
    file << " appending data";

    file.close();
}
else
{
    cout << "\"Could not open file.\"" << endl;
}

cout << "Done.";

Well, thanks Mosaicfuneral, you mentioned break; is it correct that it is used (only?) in switch... case ?

They're for abruptly breaking out of loops, which beginners confuse with goto some reason(never use goto!).

^^reading it as i write (well, as much as my multitasking skills allow me to)

So break will break any loop? for, while, do while and swicth case? and then countinue the program from just after the loop?

^^Right right, ill remember to google and such before i ask stupid questions next time :-)

Thank you everyone!

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.