Saving to a file

This is a discussion on Saving to a file within the C++ Programming forums, part of the General Programming Boards category; HI all I am trying to make a small database to help me improve my skill but i am totally ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Saving to a file

    HI all
    I am trying to make a small database to help me improve my skill but i am totally stuck so can someone please help me. I want it so if the person enters n it goes to the start of the program and if the user enters y it carries on and saves the file, but I dont know how to evaluate user input.
    Code:
    #include<fstream>// headerfile for output/input 
    #include<iostream>
    #include<string> 
    using namespace std; 
    
    
    
    int main() 
    { 
        string personname;
        string activity;
        string arriving;
        string answer;
        ofstream djout; // declare our djoutput 
        djout.open("C:\\TEST.txt"); // opens/creates and edits "TEST.txt" 
        
        cout << "Please enter the persons name: ";
        getline (cin, personname);
        cout << "Please enter the activity: ";
        getline (cin, activity);
        cout << "How are they arriving: ";
        getline (cin, arriving);
        cout << "Please confirm the details: ";
        cout << "Name: " << personname << endl;
        cout << "Activity: " << activity << endl;
        cout << "Arriving by: " << arriving << endl;
        cout << "Are the details correct? Y/N" << endl;
        getline (cin, answer);
        
        
        if (answer == 'y')
        { 
           djout << personname << endl;
           djout << activity << endl;
           djout << arriving << endl;
           djout.flush(); // to save the file, you must close the file, or flush 
           djout.close(); // the buffer to the file Closing the file will not let 
                       // you access it anymore, so only call it when you are 
                       // done using the file and it will automatically save 
                       // the file for you. Flushing the buffer will write the 
                       // buffer to the file and still keep the file open, so 
                       // use this function when necessary. 
                  
        }
        if (answer == 'n')
        {
                   //I want it to go back to the beginning of the program
        }   
        else
        {
            cout << "System error sorry guys im not always right :)" << endl;
        }
     
        system("pause"); 
        return 0; 
    }
    Can someone please show me what to do lol

    Many thanks

    HLA91

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,675
    You need a loop:
    Code:
    do
    {
        // Ask user for input here
        // Ask if input is correct or not here
    } while( answer == "n" );
    // Continue with rest of program here
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    21,172
    Use a loop, then do continue to start the loop over, if you want or just let the loop do it automatically for you.
    Btw, since answer is std::string, you should do
    Code:
    if (answer == "y")
    Not the quotes there. Single quotes are for comparing a char, not a char* or any sort of string class. Though you could do
    Code:
    answer[0] == 'y'
    this may give unintended effect since the user can enter ysklsd,vjkhdf,gjhdfgjhdfg and it'll still work (interpret as yes).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21