c++ problem

Discussion in 'Software' started by Rayzipan, Nov 13, 2007.

  1. Rayzipan

    Rayzipan Specialist

    hi,
    i need to make a c++ program that creates contacts.txt and temp.txt text files.
    the program must run and allow me to add data to the contacts.txt file.
    it must sort all contact entries by surname.

    below is some code i did.
    it does not work though.
    i would be delighted of someone could help.




    #include <iostream>
    #include <fstream> // stream class to both read and write from/to files
    #include <string>

    using namespace std; // no need for .h after libraries

    int main()

    {
    ifstream instream_1; // temporary
    ofstream outstream_1; // contacts
    ifstream instream_2; // temporary
    ofstream outstream_2; // contacts

    string surname_1, first_name_1, email_1, telephone_1, type_1; // string for old contacts
    string surname_2, first_name_2, email_2, telephone_2, type_2; // string for new contacts

    /*
    outstream.open ("contacts.txt");
    outstream.close();
    */

    instream_1.open("temporary.txt",ios::in); // open for input operations
    outstream_1.open("contacts.txt",ios::eek:ut); // open for output operations.

    instream_1>>surname_1>>first_name_1>>email_1>>telephone_1>>type_1;

    cout<<"Enter Surname: ";
    cin>>surname_1;
    cout<<"\nEnter First Name: ";
    cin>>first_name_1;
    cout<<"\nEnter Email Address: ";
    cin>>email_1;
    cout<<"\nEnter Telephone Number: ";
    cin>>telephone_1;
    cout<<"\nEnter Type(Work / Personal): ";
    cin>>type_1;

    while (surname_1 != "zzzzz") // 2 or more while loops are required
    {
    instream_1>>surname_1>>first_name_1>>email_1>>telephone_1>>type_1;
    outstream_1<<surname_1<<first_name_1<<email_1<<telephone_1<<type_1;
    }
    /*
    while(! instream_1.eof())
    {

    }
    */
    while((surname_2 > surname_1))
    {
    instream_1<<surname_1<<first_name_1<<email_1<<telephone_1<<type_1;
    outstream_1>>surname_1>>first_name_1>>email_1>>telephone_1>>type_1;
    }

    instream_1.close(); // the file is closed
    outstream_1.close(); // the file is closed

    return 0;

    }
     
  2. Wookie

    Wookie Sergeant Major

    Ray,

    The first little tidbit I noticed is this would not compile, this is due to the below code

    while((surname_2 > surname_1))
    {
    instream_1<<surname_1<<first_name_1<<email_1<<telephone_1<<type_1;
    outstream_1>>surname_1>>first_name_1>>email_1>>telephone_1>>type_1;
    }

    You have the stream operators backwards
    The below allows this to compile

    instream_1>>surname_1>>first_name_1>>email_1>>telephone_1>>type_1;
    outstream_1<<surname_1<<first_name_1<<email_1<<telephone_1<<type_1;

    try changing that and it should compile now
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds