It is better to post the code if you can to the message so people don't have
to follow links. Your code is short enough so that can be done. I am
pasting your code here so others won't have to link to it:
struct Customer{
#include <string>
#include <sstream>
template <class T>
string stringify(T arg){
stringstream ss;
string result;
ss.clear(stringstream::goodbit);
if(!(ss << arg)){
}
ss>>result;
return result;
#include <fstream>
string readLine(){
fstream file;
file.open("filename.txt", ios::in | ios:
ut | ios::binary);
if(!file.is_open()){
cout << "open a file prior to reading it\n(File::readLine())" <<
endl;
return "";
}
string strA;
getline(file, strA);
return strA;
string readLineFromAddress(int address){
fstream file;
file.seekg(address);
return readLine();
int getAddressToWrite(int id){
int lineNum = id % 1000;
int address = ((lineNum-1) * (sizeof(Customer) + 1))+1;
string line = readLineFromAddress(address);
while(!line.empty()){
address += (1000-1) * (sizeof(Customer)+1) + 1;
}
return address;
bool writeLine(int address, string toWrite){
fstream file;
file.seekp(address);
file << toWrite << endl;
return true;
int id = 34500;
//int fAddress = getAddressToWrite(id);// -> the problematic line;
#include <ctime>
#include <string>
void addCustomer(int id, string fName, string lName, string address, string
phone, int acctPeriod){
int fAddress = getAddressToWrite(id); //--->>> PROBLEMATIC LINE
time_t rawTime;
struct tm* timeInfo;
char buffer[80];
time(&rawTime);
timeInfo = localtime(&rawTime);
strftime(buffer, 80, "%B", timeInfo);
string acctCreation(buffer);
string customerString;
customerString += stringify(id);
customerString += fName;
customerString += lName;
customerString += address;
customerString += phone;
customerString += acctCreation;
customerString += stringify(acctPeriod);
customerString += stringify(0);
fAddress = ios::beg;//to remove
writeLine(fAddress, customerString);
Now, the issue I see you are attempting to initialize a variable with a
function call outside of a function block. I.E.
int fAddress = getAddressToWrite(id);
getAddressToWrite(id);
is a function call. I believe that is the source of your problem. I can't
quote chapter and verse, but if you put it inside of a function, such as
main, the problem might go away.
Now, the "output" you describe is:
In function `_start':
undefined reference to `main'
This is a different problem all together and should have nothing to do with
the intilaization of fAddress. The problem is, you don't have a main
function in your program. The entry point for C and C++ program is main.
I.E.
int main()
{
// My code here
Which is where the program starts to execute. You do not have that in your
program so it is an incomplete program and will not compile.
I'm confused as to what your actual error is, but it seems you have at least
2 you need to fix. That is with a perfunctionary examination of your
program, there may be other problems/errors.
Jim Langston
thanks Jim for the reply,
the code i pasted is not full, since i wanted only necessary code to
be pasted for you.
i do have a main and the program compile.
about the first error, well the call to fAddress =
getAddressToWrite(id) is from within a function
just removed the other code for irrelevancy, yet i will add it now so
you will see the big picture.
Also, the class struct is not the same in my code, it is a class with
body of course. i just added the struct so the codepad will compile
the call for the getAddressToWrite is from within the addCustomer
function.
here is the code:
-------------------------------------------------------------------------------------------------------------------------
struct Customer{
}Customer;
#include <string>
#include <sstream>
template <class T>
string stringify(T arg){
stringstream ss;
string result;
ss.clear(stringstream::goodbit);
if(!(ss << arg)){
}
ss>>result;
return result;
}
#include <fstream>
string readLine(){
fstream file;
file.open("filename.txt", ios::in | ios:
ut | ios::binary);
if(!file.is_open()){
cout << "open a file prior to reading it\n(File::readLine())"
<<
endl;
return "";
}
string strA;
getline(file, strA);
return strA;
}
string readLineFromAddress(int address){
fstream file;
file.seekg(address);
return readLine();
}
int getAddressToWrite(int id){
int lineNum = id % 1000;
int address = ((lineNum-1) * (sizeof(Customer) + 1))+1;
string line = readLineFromAddress(address);
while(!line.empty()){
address += (1000-1) * (sizeof(Customer)+1) + 1;
}
return address;
}
bool writeLine(int address, string toWrite){
fstream file;
file.seekp(address);
file << toWrite << endl;
return true;
}
int id = 34500;
#include <ctime>
#include <string>
void addCustomer(int id, string fName, string lName, string address,
string
phone, int acctPeriod){
int fAddress = getAddressToWrite(id); //--->>> PROBLEMATIC LINE
time_t rawTime;
struct tm* timeInfo;
char buffer[80];
time(&rawTime);
timeInfo = localtime(&rawTime);
strftime(buffer, 80, "%B", timeInfo);
string acctCreation(buffer);
string customerString;
customerString += stringify(id);
customerString += fName;
customerString += lName;
customerString += address;
customerString += phone;
customerString += acctCreation;
customerString += stringify(acctPeriod);
customerString += stringify(0);
fAddress = ios::beg;//to remove
writeLine(fAddress, customerString);
}
#include <iostream>
#include <sstream>
using std::cout;
using std::cin;
using std::endl;
using std::istringstream;
int main(int argc, char *argv[])
{
int choise;
MusicShop ms;
do{
ms.printMenu();
//istringstream cin("1\n");
cin >> choise;
switch(choise){
case 0:
{
cout << "Good Bye" << endl;
break;
}
case 1:
{
int id;
cout << "Enter customer id: "<< endl;
//istringstream ccin("21391685\n");
cin >> id;
if(ms.isCustomerExist(id) != -1){
ms.printCustomerExistsSubMenu();
ms.customerExistsHandle();
}
break;
}
case 2:
{
int id, acctPeriod;
string fname, lname, address, phone;
cout << "enter customer details in the following
order: " << endl;
cout << "id, first name, last name, address, phone,
account period " << endl;
cin >> id; cin >> fname; cin >> lname; cin >> address;
cin >> phone; cin >> acctPeriod;
ms.addCustomer(id, fname, lname, address, phone,
acctPeriod);
//ms.tempWriteData("something");
}
}
}while(choise != 0);
}