can't understand what's wrong with this function...

H

hirsh.dan

i have a functions that writes information to a file.
inside that function i have a line in which i call another function.
if this line is executed, nothing is written to the file, but if i
remark that line, it is written.
i wanna understand what's wrong and fix it, but can't find what is
wrong.
also this line is mandatory in my code.
the code attached is in the link, minimized to the needed info, a bit
different then the way it's written
in my env.

i work in debian linux, IDE = code::blocks

thanks you a lot


CODE --> http://codepad.org/5DWT76mk
 
J

Jim Langston

i have a functions that writes information to a file.
inside that function i have a line in which i call another function.
if this line is executed, nothing is written to the file, but if i
remark that line, it is written.
i wanna understand what's wrong and fix it, but can't find what is
wrong.
also this line is mandatory in my code.
the code attached is in the link, minimized to the needed info, a bit
different then the way it's written
in my env.

i work in debian linux, IDE = code::blocks

thanks you a lot

CODE --> http://codepad.org/5DWT76mk

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{
}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::eek: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.
 
H

hirsh.dan

i have a functions that writes information to a file.
inside that function i have a line in which i call another function.
if this line is executed, nothing is written to the file, but if i
remark that line, it is written.
i wanna understand what's wrong and fix it, but can't find what is
wrong.
also this line is mandatory in my code.
the code attached is in the link, minimized to the needed info, a bit
different then the way it's written
in my env.
i work in debian linux, IDE = code::blocks
thanks you a lot

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{

}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::eek: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.

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::eek: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);
}
 
H

hirsh.dan

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::eek: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::eek: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);

}

the problem is more specifically in this function:

string File::readLine(){
if(!file.is_open()){
cout << "open a file prior to reading it\n(File::readLine())"
<< endl;
return "";
}
string strA;
getline(file, strA);
return strA;
}

i can't seem to understand what is done wrong here that fails the
write to file.
if i skip this func, the data is written normally.
 
L

LR

Mandatory? Why is this mandatory?



[snip]
Open it up. Positioned where?

It looks like you're trying to position file (whatever that might be at
this point) to a particular spot, and then.....

I'm not able to really look your code over carefully, but offhand, I'd
say there is a problem with readLineFromAddress. With file. And then...

Before you fix that problem, you may want to consider what will happen
when you invoke readLine().



1000 is a magic number. What is it?
What happens to line?

while(!line.empty()){
address += (1000-1) * (sizeof(Customer)+1) + 1;
}
return address; [snip]
int fAddress = getAddressToWrite(id); //--->>> PROBLEMATIC LINE
[snip]

I think there are other problems with the code. Just for starters, you
may want to try to split the problem up into simpler chunks and see if
you can solve each of them before you put the whole thing together.

LR
 
R

red floyd

Jim said:
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:

Note that none of this code should compile. OP does not qualify
entities in std:: nor does he have a using statemet or declaration.

struct Customer{
}Customer;

#include <string>
#include <sstream>
template <class T>
string stringify(T arg){
error. string not in global namespace
stringstream ss;
error. stringstream not in global namespace

[remainder redacted]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top