-
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";
}
-
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
-
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.
-
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.
-
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.
-
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
-
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" ;
}
-
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
-
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.
-
how do i take the values and write them to the file???
infile >> varible >> varible etc..
-
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;
}
-
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.
-
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
-
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" ;
}
-
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