c++ program...... help me.......

Discussion in 'Programmer's Corner' started by transistor_circuit, Mar 9, 2009.

  1. transistor_circuit

    Thread Starter New Member

    Mar 9, 2009
    4
    0
    how to create a program that save an information into a data base or into the hard drive???


    example...


    enter a name>>>
    then it will save it...

    enter an age......
    than it will save it...



    enter an email...
    then it will save...



    please reply
     
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
  3. digitalmind

    Active Member

    Mar 7, 2009
    30
    0
    Do you know the basics of C++ programming? I'm just asking to see if you'd be able to modify and use some example code posted.

    Do you need a database, e.g. a C++ programme that works with MySQL for example? Or will a text file be good enough?

    Here's something you could go on based on what you've written.

    Code ( (Unknown Language)):
    1.  
    2. #include<iostream>
    3. #include<fstream>
    4. #include<string>
    5.  
    6. using namespace std;
    7.  
    8. int main()
    9. {
    10.      string name, age, email;
    11.  
    12.      cout << "Name >";
    13.      getline( cin, name );
    14.  
    15.      cout << "Age >";
    16.      getline( cin, age );
    17.  
    18.      cout << "Email >";
    19.      getline( cin, email );
    20.  
    21.      ofstream txtfile;
    22.      txtfile.open( "data.csv" );
    23.  
    24.      txtfile << name << ','
    25.               << age << ','
    26.               << email << endl;
    27.  
    28.      txtfile.close();
    29. }
    C:\> g++/gxx -o simple.exe -O2 -g simple.cc
    C:\> simple
    ...etc.

    It omits error checking and things like that as you can see. Is that sort of what you're looking for, a simple programme to write data to a text file which might be looked at or read by another programme at some time?
     
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    It also omits such subtleties as allocating space for strings, so although it will probably compile, it will almost certainly generate errors at run time.
     
  5. digitalmind

    Active Member

    Mar 7, 2009
    30
    0
    This is C++, and objects are allocated automatically on the stack. The memory handling for the string value is taken care of automatically by the class, and comes from the heap.

    Notice I did not declare a pointer to a string.

    It sure does.

    No it does not. It works fine. You might like to read Stroustrup's books on C++.
     
Loading...