Code closes automatically when 1 is entered

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lumpybanana247
    New Member
    • Apr 2007
    • 134

    Code closes automatically when 1 is entered

    when i use this script and choose "2" or "3" (in bold) it closes automatically, but "1" (underlined) doesn't


    #include <fstream>
    #include <iostream>
    #include <iostream>
    using namespace std;
    int main()
    {
    char str[10];
    char FileName[500];
    char FileContents[999999];
    char usernameb[99];
    char username[99];
    char FileExt[99];
    char ExtensionName[500];
    char fullnamew[500];
    int Choice;
    int Choice2;

    cout<<"~~File Name~~\n";
    cout<<"Enter The File Name\n";
    cin.getline ( FileName, 500 );
    cout<<"\n~~File Contents~~\n";
    cout<<"Enter The File Contents\n";
    cin.getline ( FileContents, 999999 );
    cout<<"\n~~Save Directory~~";
    //cout<<"Type File Extension";
    //cin.getline ( FileExt, 99 );
    cout<<"Type...\ n";
    cout<<"1. To Save File On Desktop\n";
    cout<<"2. To Save File In My Documents\n";
    cout<<"3. To Type In Your Own Location\n";

    cin>> Choice;
    cin.ignore();
    if ( Choice == 1 ) {
    string FileRoot("C:/Documents and Settings/All Users/Desktop/");
    string root = FileRoot;
    cout<<"\n~~File Extension~~";
    cout<<"\nPress. ..\n";
    cout<<"1. Text Format\n";
    cout<<"2. HTML Format\n";
    cout<<"3. Rich Text Format\n";
    cout<<"4. Manually Type In Extension...\n" ;
    cin>> Choice2;
    cin.ignore();
    if ( Choice2 == 1 ) {
    string fullname = root + "/" + FileName + ".txt";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 2 ) {
    string fullname = root + "/" + FileName + ".html";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 3 ) {
    string fullname = root + "/" + FileName + ".rtf";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 4 ) {
    cout<<"~~Extens ion~~\n";
    cout<<"Type In The Extension (ex. '.txt')\n";
    cin.getline ( ExtensionName, 500 );
    string fullname = root + "/" + FileName + ExtensionName;
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }





    if ( Choice == 2 ) {
    cout<<"What Is Your Username\n";
    cin.getline ( usernameb, 999 );
    string username = usernameb;
    string root = "C:/Documents and Settings/" + username + "/My Documents/";
    cout<<"\n~~File Extension~~";
    cout<<"\nPress. ..\n";
    cout<<"1. Text Format\n";
    cout<<"2. HTML Format\n";
    cout<<"3. Rich Text Format\n";
    cout<<"4. Manually Type In Extension...\n" ;
    cin>> Choice2;
    cin.ignore();
    if ( Choice2 == 1 ) {
    string fullname = root + "/" + FileName + ".txt";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 2 ) {
    string fullname = root + "/" + FileName + ".html";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 3 ) {
    string fullname = root + "/" + FileName + ".rtf";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 4 ) {
    cout<<"~~Extens ion~~\n";
    cout<<"Type In The Extension (ex. '.txt')\n";
    cin.getline ( ExtensionName, 500 );
    string fullname = root + "/" + FileName + ExtensionName;
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }


    if ( Choice == 3 ) {
    cout<<"Type In The Directory (ex. 'C:/Documents and Settings/Bob/My Documents/\n";
    cin.getline ( usernameb, 999 );
    string username = usernameb;
    string fullname = username + "/" + FileName + ".txt";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); \
    }

    // cin.getline ( usernameb, 999 );
    // string username = usernameb;


    }

    cin.get();}}
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by lumpybanana247
    when i use this script and choose "2" or "3" (in bold) it closes automatically, but "1" (underlined) doesn't


    #include <fstream>
    #include <iostream>
    #include <iostream>
    using namespace std;
    int main()
    {
    char str[10];
    char FileName[500];
    char FileContents[999999];
    char usernameb[99];
    char username[99];
    char FileExt[99];
    char ExtensionName[500];
    char fullnamew[500];
    int Choice;
    int Choice2;

    cout<<"~~File Name~~\n";
    cout<<"Enter The File Name\n";
    cin.getline ( FileName, 500 );
    cout<<"\n~~File Contents~~\n";
    cout<<"Enter The File Contents\n";
    cin.getline ( FileContents, 999999 );
    cout<<"\n~~Save Directory~~";
    //cout<<"Type File Extension";
    //cin.getline ( FileExt, 99 );
    cout<<"Type...\ n";
    cout<<"1. To Save File On Desktop\n";
    cout<<"2. To Save File In My Documents\n";
    cout<<"3. To Type In Your Own Location\n";

    cin>> Choice;
    cin.ignore();
    if ( Choice == 1 ) {
    string FileRoot("C:/Documents and Settings/All Users/Desktop/");
    string root = FileRoot;
    cout<<"\n~~File Extension~~";
    cout<<"\nPress. ..\n";
    cout<<"1. Text Format\n";
    cout<<"2. HTML Format\n";
    cout<<"3. Rich Text Format\n";
    cout<<"4. Manually Type In Extension...\n" ;
    cin>> Choice2;
    cin.ignore();
    if ( Choice2 == 1 ) {
    string fullname = root + "/" + FileName + ".txt";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 2 ) {
    string fullname = root + "/" + FileName + ".html";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 3 ) {
    string fullname = root + "/" + FileName + ".rtf";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 4 ) {
    cout<<"~~Extens ion~~\n";
    cout<<"Type In The Extension (ex. '.txt')\n";
    cin.getline ( ExtensionName, 500 );
    string fullname = root + "/" + FileName + ExtensionName;
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }





    if ( Choice == 2 ) {
    cout<<"What Is Your Username\n";
    cin.getline ( usernameb, 999 );
    string username = usernameb;
    string root = "C:/Documents and Settings/" + username + "/My Documents/";
    cout<<"\n~~File Extension~~";
    cout<<"\nPress. ..\n";
    cout<<"1. Text Format\n";
    cout<<"2. HTML Format\n";
    cout<<"3. Rich Text Format\n";
    cout<<"4. Manually Type In Extension...\n" ;
    cin>> Choice2;
    cin.ignore();
    if ( Choice2 == 1 ) {
    string fullname = root + "/" + FileName + ".txt";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 2 ) {
    string fullname = root + "/" + FileName + ".html";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 3 ) {
    string fullname = root + "/" + FileName + ".rtf";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }
    if ( Choice2 == 4 ) {
    cout<<"~~Extens ion~~\n";
    cout<<"Type In The Extension (ex. '.txt')\n";
    cin.getline ( ExtensionName, 500 );
    string fullname = root + "/" + FileName + ExtensionName;
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); }


    if ( Choice == 3 ) {
    cout<<"Type In The Directory (ex. 'C:/Documents and Settings/Bob/My Documents/\n";
    cin.getline ( usernameb, 999 );
    string username = usernameb;
    string fullname = username + "/" + FileName + ".txt";
    ofstream a_file(fullname .c_str());
    a_file<<FileCon tents;
    a_file.close(); \
    }

    // cin.getline ( usernameb, 999 );
    // string username = usernameb;


    }

    cin.get();}}
    I'm not exactly sure but maybe the problem is with your getline() calls. Try making the second arguement smaller than 99 ( the size of usernameb). I think it raises an Acces Violation because it can't store that much but again, I'm not sure. Did you try debugging it?

    Comment

    • lumpybanana247
      New Member
      • Apr 2007
      • 134

      #3
      Originally posted by ilikepython
      I'm not exactly sure but maybe the problem is with your getline() calls. Try making the second arguement smaller than 99 ( the size of usernameb). I think it raises an Acces Violation because it can't store that much but again, I'm not sure. Did you try debugging it?

      No, that didn't work, but at least now i know not to make it over 99 anymore :-)

      Comment

      • lumpybanana247
        New Member
        • Apr 2007
        • 134

        #4
        Originally posted by lumpybanana247
        No, that didn't work, but at least now i know not to make it over 99 anymore :-)
        im gunna try to rewrite the whole thing- debuging doesnt help...
        ill (probably) tell you what happend later

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          HI,
          I have found couple of problems
          1)You have used unix slash(/) in th path but in windows u have to use \\
          2)after opening the file you are not checking whether the file handler is null or not, better check for null and write
          3)Before reading from stdin flush the stdin and then read
          fflush(stdin);

          Thanks
          Raghuram

          Comment

          • lumpybanana247
            New Member
            • Apr 2007
            • 134

            #6
            it works now- i did so many things, i forgot what i did. thanks... case closed!

            Comment

            Working...