Thread: whats wrong??

  1. #1
    Unregistered
    Guest

    Question whats wrong??

    Hi ,

    the prog. should
    1)take the input file from the user,
    2)if file does not exist ,display error message
    3) if the user types in correct file name ,file should be opened, and database populated
    4) if user wants to exit press -1.....

    In my code its becoming exactly opposite ,if I type -1 ,file is opened ,if I type "names.txt", error message is displayed.....Why???
    can anyone let me know where I am going wrong....
    thanks a lot...........
    [code]

    int numb = 0;
    string file = " ";
    int latID=0;
    ifstream inputFile("names.txt", ios::in );
    ofstream outputFile;


    cout<<"Enter the file (-1 to end)"<<endl;
    cout<<"reading the file........"<<endl;
    cin.geline(file, 100, '\n');

    while(file !="-1")
    {

    inputFile.open(file.c_str(), ios::in);
    if(inputFile.fail())
    {
    cout << "\n\n\t\t\..........***Input file does not exist*****"<<endl;
    inputFile.clear();
    cout<<"Enter the file (-1 to end)"<<endl;
    cin.geline(file, 100, '\n');
    inputFile.open(file.c_str(), ios::in);

    }

    }

    adatabase(database,numb);
    displayMainMenu();
    getMenuOption(database,numb);


    return 0;
    }

  2. #2
    Unregistered
    Guest

    going insane!

    Hi,
    sorry forgot to close the endtag......in previous post!!


    the prog. should
    1)take the input file from the user,
    2)if file does not exist ,display error message
    3) if the user types in correct file name ,file should be opened, and database populated
    4) if user wants to exit press -1.....

    In my code its becoming exactly opposite ,if I type -1 ,file is opened ,if I type "names.txt", error message is displayed.....Why???
    can anyone let me know where I am going wrong....
    thanks a lot...........
    Code:
     
    
    int numb = 0; 
    string file = " "; 
    int latID=0; 
    ifstream inputFile("names.txt", ios::in ); 
    ofstream outputFile; 
    
    
    cout<<"Enter the file (-1 to end)"<<endl; 
    cout<<"reading the file........"<<endl; 
    cin.geline(file, 100, '\n'); 
    
    while(file !="-1") 
    { 
    
    inputFile.open(file.c_str(), ios::in); 
    if(inputFile.fail()) 
    { 
    cout << "\n\n\t\t\..........***Input file does not exist*****"<<endl; 
    inputFile.clear(); 
    cout<<"Enter the file (-1 to end)"<<endl; 
    cin.geline(file, 100, '\n'); 
    inputFile.open(file.c_str(), ios::in); 
    
    } 
    
    } 
    
    adatabase(database,numb); 
    displayMainMenu(); 
    getMenuOption(database,numb); 
    
    
    return 0; 
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    "-1" is a string. When the user types -1, it will normally be read as an integer. Try losing the quotes.
    geline in your code should be getline.
    That's a start.
    Truth is a malleable commodity - Dick Cheney

  4. #4
    Unregistered
    Guest

    Smile thanks...but

    Thanks .....but I am accpeting the file as a string so I am using quotes for -1 ........
    If I remove quotes it is displaying error message.......
    Thanks for poitning out the typoerror....
    Please help me..............

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM