need some help

This is a discussion on need some help within the C++ Programming forums, part of the General Programming Boards category; you just got my feet moving. Thanks for the post. I was not seeing that. Now I just need to ...

  1. #16
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    you just got my feet moving. Thanks for the post. I was not seeing that.

    Now I just need to figure out how to for example enter two sets of data through the loop above. Data is written to file. Then in the bottom search function be able to type termnumber 1 or 2 and get it to display the info.

    Thanks again for getting me moving.

  2. #17
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    Okay thanks for that great info. I should of caught that , and my compliler should have said something about that. It did i just didnt understand what it was telling me.

    here is the revised code. My search function doesnt work,,, it never finds a search == Termnumber and proceds to the error statement.

    Any suggestions:

    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("afile.txt");
    	assert(outFile.is_open());
    
    	
    	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;
    		
    	}
    	
        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"  ;
    }

  3. #18
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You are never writing more then one record to the file.
    put the output statement inside the loop.
    for the search part you have to use a loop as well.
    Kurt

  4. #19
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    Thanks again for the suggestions. I put the output inside the loop and did some tests it appears the data gets written over with the next set of questions. Any suggestions on how to get it to keep both sets of data??

  5. #20
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If that happenes then you propably open and close the output stream inside the loop as well.
    show your code.
    Kurt

  6. #21
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    Zuk I started to go down another route using a class still seems to have the same problems. Any suggestions??

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include<cassert>
    using namespace std;
    
    class TermInfo
    {
    public:
    	string bldg;
    	char accessCode;
    	int transRate;
    	string termType;
    	int serviceDay;
    	int serviceMonth;
    	int serviceYear;
    	int TermNumber;
    };
    void main()
    {
    	/* int TotalTerm; */
    
    	int const TotalTerm = 2;
    	
        
    	cout << "Enter two sets of data for your terminal entrys today" << "\n";
    	/* cin >> TotalTerm;  */
    	ofstream outFile("afile.txt");
    	assert(outFile.is_open());
        TermInfo info[TotalTerm];
    	
    	for (int i=0; i < TotalTerm; i++)
    	{
    		cout << "Enter Terminal Number (INT):  "  << "\n";
    		cin >> info[i].TermNumber;
    		cout << "Enter Transmission rate (INT):  " << "\n";
    		cin >> info[i].transRate;
    		cout << "Enter terminal type (String):  " << "\n";
    		cin >> info[i].termType;
    		cout << "Enter building terminal is located (String):  " << "\n";
    		cin >> info[i].bldg;
    		cout << "Enter access code to terminal (Char):  " << "\n";
    		cin >> info[i].accessCode;
    		cout << "Enter day of last servicing date (dd) (INT):  " << "\n";
    		cin >> info[i].serviceDay;
    		cout << "Enter month of last servicing date (mm)(INT):  " << "\n";
    		cin >> info[i].serviceMonth;
    		cout << "Enter year of the last servicing date (YYYY) (INT):  " << "\n";
    		cin >> info[i].serviceYear;
    		outFile << info[i].TermNumber << info[i].transRate << info[i].termType << info[i].bldg << info[i].accessCode << info[i].serviceDay << info[i].serviceMonth << info[i].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 >> info[i].TermNumber >> info[i].transRate >> info[i].termType >> info[i].bldg >> info[i].accessCode >> info[i].serviceDay >> info[i].serviceMonth >> info[i].serviceYear;
    	if (search == 1)
    	{
    
    		ch = inFile.get();
    		cout << info[i].TermNumber <<  "   " << info[i].bldg << "     "  << info[i].accessCode << "\n"; 
    	    cout << info[i].transRate << "     " << info[i].termType << "     " << info[i].serviceDay << "\n";
    	    cout << info[i].serviceMonth << "    " << info[i].serviceYear << "\n";
    	}
    	else 
    		cout << "please re run the program"  ;
    }

  7. #22
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    as I said you have to put the reading into some loop as well
    something like this
    Code:
            for ( int i = 0; i < TotalTerm; i++) {
                 char ch;
    	     inFile >> info[i].TermNumber >> info[i].transRate >> info[i].termType >> info[i].bldg >> info[i].accessCode >> info[i].serviceDay >> info[i].serviceMonth >> info[i].serviceYear;
                 if (search == info[i].TermNumber ) {
    		cout << info[i].TermNumber <<  "   " << info[i].bldg << "     "  << info[i].accessCode << "\n"; 
    	       cout << info[i].transRate << "     " << info[i].termType << "     " << info[i].serviceDay << "\n";
    	       cout << info[i].serviceMonth << "    " << info[i].serviceYear << "\n";
    	     }
            }
    BTW main returns int.
    Kurt

  8. #23
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    This would be appropriate for you to output and input terminal records with overloaded << and >> stream operators. But anyways, you can put an endl at the end of each terminal record line:

    Code:
    outFile << info[i].TermNumber << info[i].transRate << info[i].termType << info[i].bldg << info[i].accessCode << info[i].serviceDay << info[i].serviceMonth << info[i].serviceYear << endl;
    If you overload operator << to ouput your class, you can like

    Code:
    outfile << info;
    If you overload operator >> to input your class, you can like

    Code:
    while(infile >> yrclass)
    {
            if(search == yrclass.termnumbuh)
            {
                    dostuf();
            }
    }

  9. #24
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    I agree it needs to be in a loop if you would want to output everything that was input above.

    My problem is i need to prompt the user for which terminal they would like to see. I.E. 1 or 2. Then the data prints to the screen just for that terminal.

  10. #25
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, if you did all that stream overloading like I said, then

    Code:
    while(infile >> yrclass)
    {
            if(search == yrclass.termnumbuh)
            {
                    cout << yrclass;
            }
    }
    Otherwise, just

    Code:
    while(infile >> yrclass.var1 >> yrclass.var2 >> yrclass.var3 /* and so on */)
    {
            if(search == yrclass.termnumbuh)
            {
                    cout << yrclass.var1 << yrclass.var2 << yrclass.var3 /* and so on */;
            }
    }

  11. #26
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    Tonto

    I am not following. I attempted to do what you are suggesting. Seems my compiler doesnt like the info part. I think i am missing the boat here. I am very new to this programming stuff.

  12. #27
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    This code compiles , but when i am prompted for which terminal i would like to see. I enter 1 and get press any key to continue.

    here is the code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include<cassert>
    using namespace std;
    
    class TermInfo
    {
    public:
    	string bldg;
    	char accessCode;
    	int transRate;
    	string termType;
    	int serviceDay;
    	int serviceMonth;
    	int serviceYear;
    	int TermNumber;
    };
    void main()
    {
    	/* int TotalTerm; */
    
    	int const TotalTerm = 2;
    	
        
    	cout << "Enter two sets of data for your terminal entrys today" << "\n";
    	/* cin >> TotalTerm;  */
    	ofstream outFile("afile.txt");
    	assert(outFile.is_open());
        TermInfo info[TotalTerm];
    	
    	for (int i=0; i < TotalTerm; i++)
    	{
    		cout << "Enter Terminal Number (INT):  "  << "\n";
    		cin >> info[i].TermNumber;
    		cout << "Enter Transmission rate (INT):  " << "\n";
    		cin >> info[i].transRate;
    		cout << "Enter terminal type (String):  " << "\n";
    		cin >> info[i].termType;
    		cout << "Enter building terminal is located (String):  " << "\n";
    		cin >> info[i].bldg;
    		cout << "Enter access code to terminal (Char):  " << "\n";
    		cin >> info[i].accessCode;
    		cout << "Enter day of last servicing date (dd) (INT):  " << "\n";
    		cin >> info[i].serviceDay;
    		cout << "Enter month of last servicing date (mm)(INT):  " << "\n";
    		cin >> info[i].serviceMonth;
    		cout << "Enter year of the last servicing date (YYYY) (INT):  " << "\n";
    		cin >> info[i].serviceYear;
    		outFile << info[i].TermNumber << info[i].transRate << info[i].termType << info[i].bldg << info[i].accessCode << info[i].serviceDay << info[i].serviceMonth << info[i].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 >> info[i].TermNumber >> info[i].transRate >> info[i].termType >> info[i].bldg >> info[i].accessCode >> info[i].serviceDay >> info[i].serviceMonth >> info[i].serviceYear;
    	while(inFile >> info[i].TermNumber)
    {
            if(search == info[i].TermNumber)
            {
                cout << info[i].TermNumber <<  "   " << info[i].bldg << "     "  << info[i].accessCode << "\n"; 
    			cout << info[i].transRate << "     " << info[i].termType << "     " << info[i].serviceDay << "\n";
    			cout << info[i].serviceMonth << "    " << info[i].serviceYear << "\n";
    	}	
    	else 
    		cout << "please re run the program"  ;
              
            }
    }

  13. #28
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    If you wanted to do it my special E-Z way, you would have to provide overloaded implementations of those operators to work on your classes. Um, read this. http://cpp.codenewbie.com/articles/c...g-Page_19.html But I provided the alternate way to do it without overloading them in my second example. Just for now, try not overloading those.

  14. #29
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Okay, little find and replace action. It seems better to not use that info array any more, and especially not the variable i. Just have a new, temporary TermInfo.

    Code:
    	cout << "Enter Terminal number to search for:  " << "\n";
    	int search;
    	cin >> search;
    
    
            TermInfo currentInfo;
    
    	while(inFile >> currentInfo.TermNumber >> currentInfo.transRate 
    	       >> currentInfo.termType >> currentInfo.bldg >> currentInfo.accessCode 
                   >> currentInfo.serviceDay >> currentInfo.serviceMonth 
                   >> currentInfo.serviceYear)
    {
            if(search == currentInfo.TermNumber)
            {
                cout << currentInfo.TermNumber <<  "   " << currentInfo.bldg << "     "  << currentInfo.accessCode << "\n"; 
    			cout << currentInfo.transRate << "     " << currentInfo.termType << "     " << currentInfo.serviceDay << "\n";
    			cout << currentInfo.serviceMonth << "    " << currentInfo.serviceYear << "\n";
    	}	
    	else 
    		cout << "please re run the program"  ;
              
            }

  15. #30
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    didnt the way i do it in the code posted just before your post , do it the non overloading way. I am very confused. attempting to read the link you provided now.

Page 2 of 3 FirstFirst 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