It was suggested before that you should write a delimiter (space) in the file. Otherwise all the numbers will be right after each other.
out << num1 << " " << num2 << " " << ...
Printable View
It was suggested before that you should write a delimiter (space) in the file. Otherwise all the numbers will be right after each other.
out << num1 << " " << num2 << " " << ...
i will make that adjustment now. any suggestions on how to get my search at the bottom to only print out the one of the inputs of the terminal information?
latest code still no data prints out get press any key to continue after i enter the number 1 to see the first input. I am so fraustrated with this program. Doing a programming class with no instructor near by is terrible.
I am having these problems with the excercises. man i cant wait for the test. lol I am getting it very slowly so maybe i will be okay.
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;
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" ;
}
}
you are getting close.
like anon already said, you have to output some delimiter when writing the record.
as long as you don't input any whitspace when reading the strings it should work.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 want to allow whitespace in the strings you will have to write some different delimiter and use getline() to read the data.
Kurt
Thanks for all your help. I got it to work now. It seems the small things tend to cause more trouble than the ones you think are harder.
I appreciate the help guys
As already mentioned, this should be int main() . . . .Code:void main()