need some help

This is a discussion on need some help within the C++ Programming forums, part of the General Programming Boards category; I am writing a program to take in data into a file, close the file. Then re open the file, ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    25

    need some help

    I am writing a program to take in data into a file, close the file. Then re open the file, ask for terminal number person wants to see, program needs to display all information entered for that terminal number.

    Here is my problem , no matter what terminal number I enter the last information entered is what is displayed.

    how do I make this work. Thanks in advance for your suggestions and help.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include<cassert>
    #include <math.h>
    using namespace std;
    
    
    void main()
    {
    	string bldg;
    	char accessCode;
    	int transRate;
    	string termType;
    	int serviceDay;
    	int serviceMonth;
    	int serviceYear;
    	int TermNumber;
    	int TotalTerm;
    
    	
    	ofstream outStream;
    	outStream.open("OutputFile.TXT");
    
    	cout << "Enter total number of Terminals to store " << "\n";
    	cin >> TotalTerm;
    
        
    	for (int i=0; i < TotalTerm; i++)
    	{
    		cout << "Enter Terminal Number:  "  << "\n";
    		cin >> TermNumber;
    		cout << "Enter Transmission rate:  " << "\n";
    		cin >> transRate;
    		cout << "Enter terminal type:  " << "\n";
    		cin >> termType;
    		cout << "Enter building terminal is located:  " << "\n";
    		cin >> bldg;
    		cout << "Enter access code to terminal:  " << "\n";
    		cin >> accessCode;
    		cout << "Enter day of last servicing date (dd):  " << "\n";
    		cin >> serviceDay;
    		cout << "Enter month of last servicing date (mm):  " << "\n";
    		cin >> serviceMonth;
    		cout << "Enter year of the last servicing date (YYYY):  " << "\n";
    		cin >> serviceYear;
    	}
    	outStream.close();
    
    	/* This section gets value from the closed file  */
    
    	ifstream inFile("OutputFile.TXT", ios::end);
    	
    	if(!inFile)
    	{
    		cerr << "File could not be opened!" << "\n";
    		exit (1);
    	}
    
    
    	cout << "Enter Terminal number to search for:  " << "\n";
    	int search;
    	cin >> search;
        char ch;
    	ch = inFile.get();
    	if (search = TermNumber)
    
    		
    		cout << TotalTerm << "    " << bldg << "     "  << accessCode << "\n"; 
    	    cout << transRate << "     " << termType << "     " << serviceDay << "\n";
    	    cout << serviceMonth << "    " << serviceYear << "\n";
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    void main()
    That should be int main(). [edit] http://faq.cprogramming.com/cgi-bin/...&id=1043284376 [/edit]
    Code:
    if (search = TermNumber)
    ->
    Code:
    if (search == TermNumber)
    and don't forget the curly braces {}.

    exit() is in <cstdlib>. <math.h> is <cmath> in C++.

    You don't actually write anything to the file; try
    Code:
    outStream << variable
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    thanks for the suggestions. I still have the same problem, if i you run the program and enter the data for 2 terminals. Only the last one entered comes up after i search for a terminal.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You do not keep track of the records entered. Every time you go through the loop, you get information into some variables, and in the next pass through the loop, they are overwritten with new data. You will want to write your data to a file after you have gathered it, in each pass of the loop. You might need to have a funny little tag for your data so that you can find it, or store the terminal data one per line. Later, you might just want to make an array or vector of TermData things which you could hold in a structure:

    Code:
    struct TermData
    {
    	string bldg;
    	char accessCode;
    	int transRate;
    	string termType;
    	int serviceDay;
    	int serviceMonth;
    	int serviceYear;
    	int TermNumber;
    };
    Or have parallel arrays of this data, but that is worse design. If it is an assignment though, then whatever.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    i dont think a struct would work for what i am attempting to do. I need the program to find the data within the file. I just built the struct still does the same thing shows only the last terminal entered.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I dont see how your code is searching for anything?
    Seems like all it does is basicly nothing, cept for get user input, then display it at the end of code
    and where are you getting the values from the file and adding them back into variables to be displayed? shouldnt you do something like
    Code:
    inFile >> variable >> variable >> variable >> variable >> variable; // and then display them

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    here is some revised code. the search function doesnt seem to find anything.


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include<cassert>
    using namespace std;
    
    
    void main()
    {
    	string bldg;
    	char accessCode;
    	int transRate;
    	string termType;
    	int serviceDay;
    	int serviceMonth;
    	int serviceYear;
    	int TermNumber;
    	int TotalTerm;
    
    	ofstream outStream;
    	outStream.open("OutputFile.TXT");
    
    	cout << "Enter total number of Terminals to store (INT) " << "\n";
    	cin >> TotalTerm;  
    
    	
    	for (int i=0; i < TotalTerm; i++)
    	{
    		cout << "Enter Terminal Number (INT):  "  << "\n";
    		cin >> TermNumber;
    		cout << "Enter Transmission rate (INT):  " << "\n";
    		cin >> transRate;
    		cout << "Enter terminal type (String):  " << "\n";
    		cin >> termType;
    		cout << "Enter building terminal is located (String):  " << "\n";
    		cin >> bldg;
    		cout << "Enter access code to terminal (Char):  " << "\n";
    		cin >> accessCode;
    		cout << "Enter day of last servicing date (dd) (INT):  " << "\n";
    		cin >> serviceDay;
    		cout << "Enter month of last servicing date (mm)(INT):  " << "\n";
    		cin >> serviceMonth;
    		cout << "Enter year of the last servicing date (YYYY) (INT):  " << "\n";
    		cin >> serviceYear;
    	}
    	outStream.close();
    
    	/* This section gets value from the closed file  */
    
    	ifstream inFile("OutputFile.TXT", ios::end);
    	
    	
    	if(!inFile)
    	{
    		cerr << "File could not be opened!" << "\n";
    		exit (1);
    	}
        inFile >> TermNumber >> transRate >> termType >> bldg >> accessCode >> serviceDay >> serviceMonth >> serviceYear;
    
    	cout << "Enter Terminal number to search for:  " << "\n";
    	int search;
    	cin >> search;
        char ch;
    	ch = inFile.get();
    	if (search == &TermNumber)
    	{
    
    		
    		cout << TermNumber <<  "   " << bldg << "     "  << accessCode << "\n"; 
    	    cout << transRate << "     " << termType << "     " << serviceDay << "\n";
    	    cout << serviceMonth << "    " << serviceYear << "\n";
    	}
    	else 
    		cout << "please re run the program"  ;
    }

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    umm, what code do you think searchs the file? and I see you added inFile >>; but you need to add the inFile after you have searched and went to the line or else the inFile will just put the last line into variable. also on your inFile why do you use ios::end?? I am sorry if I sound rude just having a bad day, but dont get em wrong I am just trying to help

    EDIT: You could always use a little more space taking way and just make a .dat file for each thing, and then just have the file name be the ID number , and use c_str() to open the file. This way doesnt add very much space because a 1 line .dat file is only 4-50 bytes. So not like hardrive space is a problem, it would take hundreds to even make a megabyte, and I dont think 1 meg is really a big deal
    Last edited by Raigne; 07-14-2006 at 07:12 PM.

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Show me where you write to the file. You don't do it in the code you are showing us. You take the values from the user, and set the variables, then you never write those variables to the file.

    EDIT:

    Code:
    cout << "Enter year of the last servicing date (YYYY) (INT):  " << "\n";
    Could be written as:

    Code:
    cout << "Enter year of the last servicing date (YYYY) (INT):\n";
    All of the output in that section is like that.
    Last edited by Wraithan; 07-14-2006 at 07:21 PM.

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    how do i take the values and write them to the file???

    infile >> varible >> varible etc..

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    It is the same as cout and cin, just with different names
    Code:
    ofstream out ( "afile.txt" );
    out << variable << variable;
    out.close();
    
    ifstream in ( "afile.txt" );
    if ( !in.is_open() ) {
    cout << "File Failed to open" << endl;
    }
    else {
    in >> variable >> variable;
    }

  12. #12
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Well, since you are using " outStream.open("OutputFile.TXT"); " You would write to it using outStream << variable; If you want to read how you plan on doing it, later in the file, then you will want to use outStream << variable << " ";

    Here are some things you should read:

    http://www.cprogramming.com/tutorial/lesson7.html
    http://www.cprogramming.com/tutorial/lesson8.html
    http://www.cprogramming.com/tutorial/lesson10.html

    In general, go through the tutorials, they are there for a reason.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Nice point wraithen, although sierrava06 if you need any one on one help just PM me with a AIM address and I can help you in realtime instead of using forums. I think my AIM address is in my profile

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    Well i got up early this morning and thought maybe i can figure this stuff out. I am still at a stand still.

    The command in the program for Ofstream is where my problem lies. The compile gets stuck on the OutFile part. Doesnt recognize it. Please look over my code and get me moving in a direction. These excercises in the book are killing me. The book doesnt have clear running programs utilizing the commands. Please assist

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include<cassert>
    using namespace std;
    
    
    void main()
    {
    	string bldg;
    	char accessCode;
    	int transRate;
    	string termType;
    	int serviceDay;
    	int serviceMonth;
    	int serviceYear;
    	int TermNumber;
    	int TotalTerm;
    
    	
    	
        
    	cout << "Enter total number of Terminals to store (INT) " << "\n";
    	cin >> TotalTerm;  
    
    	ofstream outfile;
    	for (int i=0; i < TotalTerm; i++)
    	{
    		cout << "Enter Terminal Number (INT):  "  << "\n";
    		cin >> TermNumber;
    		cout << "Enter Transmission rate (INT):  " << "\n";
    		cin >> transRate;
    		cout << "Enter terminal type (String):  " << "\n";
    		cin >> termType;
    		cout << "Enter building terminal is located (String):  " << "\n";
    		cin >> bldg;
    		cout << "Enter access code to terminal (Char):  " << "\n";
    		cin >> accessCode;
    		cout << "Enter day of last servicing date (dd) (INT):  " << "\n";
    		cin >> serviceDay;
    		cout << "Enter month of last servicing date (mm)(INT):  " << "\n";
    		cin >> serviceMonth;
    		cout << "Enter year of the last servicing date (YYYY) (INT):  " << "\n";
    		cin >> serviceYear;
    		
    	}
    	ofstream outFile("afile.txt");
        outFile >> TermNumber >> transRate >> termType >> bldg >> accessCode >> serviceDay >> serviceMonth >> serviceYear;
    	outFile.close();
    
    	/* This section gets value from the closed file  */
    
    	ifstream inFile("afile.txt");
    	
    	
    	if(!inFile)
    	{
    		cerr << "File could not be opened!" << "\n";
    		exit (1);
    	}
        
    	cout << "Enter Terminal number to search for:  " << "\n";
    	int search;
    	cin >> search;
        char ch;
    	inFile >> TermNumber >> transRate >> termType >> bldg >> accessCode >> serviceDay >> serviceMonth >> serviceYear;
    	if (search == TermNumber)
    	{
    
    		ch = inFile.get();
    		cout << TermNumber <<  "   " << bldg << "     "  << accessCode << "\n"; 
    	    cout << transRate << "     " << termType << "     " << serviceDay << "\n";
    	    cout << serviceMonth << "    " << serviceYear << "\n";
    	}
    	else 
    		cout << "please re run the program"  ;
    }

  15. #15
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
       outFile >> TermNumber >> transRate >> termType >> bldg >> accessCode >> serviceDay >> serviceMonth >> serviceYear;
    you can't read from an output stream.
    use
    Code:
       outFile << TermNumber << transRate << termType << bldg << accessCode << serviceDay << serviceMonth << serviceYear;
    Kurt

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

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