program error

B

Baloff

Hello

I wrote a code which is suppose to read a file which contains lines of
double and prints it out.

thanks for helping

double.txt***************************************
1.01
2.0301
3.0604
4.10101
5.15202

in a stack.h I have
//stack.h****************************************
#ifndef STACK_H
#define STACK_H

struct Stack
{
struct Link
{
void* data;
Link* next;
void initialize (void* dat, Link* nxt);
}
* head;
void initialize ();
void push (void* dat);
void* peek ();
void* pop ();
void cleanup ();
};
#endif

in my main.cpp
string line;
while(getline(in, line)){
textlines.push(reinterpret_cast<void*>(&line));
}
works ok but the following is not printing out the lines

double* s;
while(( s = static_cast<double*>(textlines.pop() )) != 0) {
cout << *s << endl;
delete s;
}
 
K

Kai-Uwe Bux

Baloff said:
Hello

I wrote a code which is suppose to read a file which contains lines of
double and prints it out.
[snip]
in a stack.h I have
//stack.h****************************************
#ifndef STACK_H
#define STACK_H

struct Stack
{
struct Link
{
void* data;
Link* next;
void initialize (void* dat, Link* nxt);
}
* head;
void initialize ();
void push (void* dat);
void* peek ();
void* pop ();
void cleanup ();
};
#endif

Hm, you are using a stack, so maybe you want to revert the order in
printing?

All these void pointers are bad. You should use std::stack.
in my main.cpp
string line;
while(getline(in, line)){
textlines.push(reinterpret_cast<void*>(&line));
}
works ok ...

Really? Now, I am pretty sure that this stores some *void items in your
stack, all of which will happily point to the address of the string
variable line which did not change!

... but the following is not printing out the lines

double* s;
while(( s = static_cast<double*>(textlines.pop() )) != 0) {

which kind of magic is this cast supposed to perform? textlines.pop() will
return a *void pointing to &line. Now you are telling the compiler that it
should not expect a string there but treat the bytes there as the
representation of a double. [You might want to have a look into
boost::lexical_cast!]
cout << *s << endl;
delete s;

Here, you are freeing memory that you nerver allocated!

Use the standard library:

#include <stack>
#include <iostream>
#include <string>
#include <fstream>

int main ( void ) {
std::stack< double > double_stack;
{
// read
std::ifstream in_file ( "numbers.txt" );
double value;
while ( in_file >> value ) {
double_stack.push( value );
}
}
{
// print
while ( ! double_stack.empty() ) {
std::cout << double_stack.top() << '\n';
double_stack.pop();
}
}
}


Best

Kai-Uwe Bux
 
R

Rolf Magnus

Baloff said:
Hello

I wrote a code which is suppose to read a file which contains lines of
double and prints it out.

thanks for helping

double.txt***************************************
1.01
2.0301
3.0604
4.10101
5.15202

in a stack.h I have
//stack.h****************************************
#ifndef STACK_H
#define STACK_H

struct Stack
{
struct Link
{
void* data;
Link* next;
void initialize (void* dat, Link* nxt);
}
* head;
void initialize ();
void push (void* dat);
void* peek ();
void* pop ();
void cleanup ();
};
#endif

in my main.cpp
string line;

You define one single string here.
while(getline(in, line)){

Now you go through the file, overwriting the string contents with the
current line each time.
textlines.push(reinterpret_cast<void*>(&line));

Then you put a pointer to the string object into your stack. Note that it's
always the same string object and thus always the same pointer value. So at
the end, you have a stack of pointers that point all to the same string,
which contains the last line of your file, since that is what was last
written to it.
}
works ok but the following is not printing out the lines

double* s;
while(( s = static_cast<double*>(textlines.pop() )) != 0) {

You cannot just take a pointer to a string and treat it as if it were a
pointer to double. This will not parse the string. It just interprets the
bits that the string is composed of as double value. The result is of
course garbage.
cout << *s << endl;
delete s;
}

Wha?
 
B

Baloff

I think I need to show the whole code, it is an exercise form Thinking
In C++ by Bruce Eckel. page 255 (8)
I need to make the Stack example in the book holds doubles, fill it
with 25 double values and print them out.

thanks

//: C04:Stack.h******************************
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Nested struct in linked list
#ifndef STACK_H
#define STACK_H

struct Stack {
struct Link {
void* data;
Link* next;
void initialize(void* dat, Link* nxt);
}* head;
void initialize();
void push(void* dat);
void* peek();
void* pop();
void cleanup();
};
#endif // STACK_H ///:~

//: C04:Stack.cpp {O}*************************
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Linked list with nesting
#include "Stack.h"
#include "../require.h"
using namespace std;

void
Stack::Link::initialize(void* dat, Link* nxt) {
data = dat;
next = nxt;
}

void Stack::initialize() { head = 0; }

void Stack::push(void* dat) {
Link* newLink = new Link;
newLink->initialize(dat, head);
head = newLink;
}

void* Stack::peek() {
require(head != 0, "Stack empty");
return head->data;
}

void* Stack::pop() {
if(head == 0) return 0;
void* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;
}

void Stack::cleanup() {
require(head == 0, "Stack not empty");
} ///:~

//: C04:StackTest.cpp*************************
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
//{L} Stack
//{T} StackTest.cpp
// Test of nested linked list
#include "Stack.h"
#include "../require.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[]) {
requireArgs(argc, 1); // File name is argument
ifstream in(argv[1]);
assure(in, argv[1]);
Stack textlines;
textlines.initialize();
string line;
// Read file and store lines in the Stack:
while(getline(in, line))
textlines.push(new string(line));
// Pop the lines from the Stack and print them:
string* s;
while((s = (string*)textlines.pop()) != 0) {
cout << *s << endl;
delete s;
}
textlines.cleanup();
} ///:~

//: :require.h******************************
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Test for error conditions in programs
// Local "using namespace std" for old compilers
#ifndef REQUIRE_H
#define REQUIRE_H
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>

inline void require(bool requirement,
const std::string& msg = "Requirement failed"){
using namespace std;
if (!requirement) {
fputs(msg.c_str(), stderr);
fputs("\n", stderr);
exit(1);
}
}

inline void requireArgs(int argc, int args,
const std::string& msg =
"Must use %d arguments") {
using namespace std;
if (argc != args + 1) {
fprintf(stderr, msg.c_str(), args);
fputs("\n", stderr);
exit(1);
}
}

inline void requireMinArgs(int argc, int minArgs,
const std::string& msg =
"Must use at least %d arguments") {
using namespace std;
if(argc < minArgs + 1) {
fprintf(stderr, msg.c_str(), minArgs);
fputs("\n", stderr);
exit(1);
}
}

inline void assure(std::ifstream& in,
const std::string& filename = "") {
using namespace std;
if(!in) {
fprintf(stderr, "Could not open file %s\n",
filename.c_str());
exit(1);
}
}

inline void assure(std::eek:fstream& out,
const std::string& filename = "") {
using namespace std;
if(!out) {
fprintf(stderr, "Could not open file %s\n",
filename.c_str());
exit(1);
}
}
#endif // REQUIRE_H ///:~
 
K

Karl Heinz Buchegger

Baloff said:
I think I need to show the whole code, it is an exercise form Thinking
In C++ by Bruce Eckel. page 255 (8)
I need to make the Stack example in the book holds doubles, fill it
with 25 double values and print them out.

So then why don't you do exactly that?
int main(int argc, char* argv[]) {
requireArgs(argc, 1); // File name is argument
ifstream in(argv[1]);
assure(in, argv[1]);
Stack textlines;
textlines.initialize();
string line;
// Read file and store lines in the Stack:
while(getline(in, line))
textlines.push(new string(line));

You are not filling the stack with 'doubles'. You are
filling it with std::string's.

Either you *read* the file not with getline into a std::string
but eg. into a double or you *convert* the string (which
holds the textual representation of that number) into
an actual number.
// Pop the lines from the Stack and print them:
string* s;
while((s = (string*)textlines.pop()) != 0) {

This will be wrong in any case. The stack holds pointers to
double, not to std::string!
 
B

Baloff

Karl Heinz Buchegger said:
So then why don't you do exactly that?
to summarize the problem and reduce your time of reading a long post
at first.
int main(int argc, char* argv[]) {
requireArgs(argc, 1); // File name is argument
ifstream in(argv[1]);
assure(in, argv[1]);
Stack textlines;
textlines.initialize();
string line;
// Read file and store lines in the Stack:
while(getline(in, line))
textlines.push(new string(line));

You are not filling the stack with 'doubles'. You are
filling it with std::string's.

Either you *read* the file not with getline into a std::string
but eg. into a double

how else would you read the file 'each line into a double'?
or you *convert* the string (which
holds the textual representation of that number) into
an actual number.

like this?
string line;
while(getline(in, line)){
textlines.push(new double( atof(line.c_str()) ));
}
 
K

Karl Heinz Buchegger

Baloff said:
how else would you read the file 'each line into a double'?

come on.

double value;

while( in >> value ) {
..
}

like this?
string line;
while(getline(in, line)){
textlines.push(new double( atof(line.c_str()) ));
}

Yep.
But atof() isn't a very good function. There is no way to check
if the string realy represents a valid number. Other methods
would be to eg. use string stream.
This topic has been beaten to death in this newsgroup. Search
the archives at google.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top