Linked Lists Test program

I'm going back to the basics and relearning some concepts I've been kind of putting off like working with dynamic memory. I created a working test program as shown below. Because of my lack of knowledge with pointers and memory de/allocation, I would like to know if I've made any style mistakes or missed some loophole.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <limits>

using std::cout;
using std::cin;
using std::endl;

class NodeTrain{
	NodeTrain* next;
	int code;
	
	public:
		NodeTrain(int,int);
		~NodeTrain();
};
NodeTrain::NodeTrain(int numtocreate, int starting_code){
	if(numtocreate > 0){
		code = starting_code;
		cout << "Greetings, human! I am Node " << code << "!\n";
		next = new NodeTrain(numtocreate-1,starting_code+1);
	}else	next = nullptr;
}
NodeTrain::~NodeTrain(){
	if(next != nullptr && next != NULL){
		cout << "Removing Node " << code << "...\n";
		delete next;
	}
	next = nullptr;
}

int main()
{
	int num_to_create(0);
	cout << "Welcome to this very simple node test! How many nodes would you like to create?\n";
	while(num_to_create <= 0){
		cout << "Number to create: ";
		cin >> num_to_create;
		cin.ignore();
	}
	
	NodeTrain Node(num_to_create,1);
	
	cout << endl << "Press ENTER to terminate the program." << endl;
	cin.ignore(std::numeric_limits<int>::max(),'\n');
	
    return 0;
}
shouldn't line 44 be std::stringstream and not int? Other than that I don't see anything actually aren't you supposed to clear the buffer before ignoring?
I thought from its name numeric_limits is best worked with number-related types?
Also, I don't know how to clear the istream buffer. Do you mind showing some example code?
no the limits automatically gives an int you want to ignore the limits of the stream not of limits of ints. The int limit is like 32million or something and string stream is like 80 I think. I didn't test but you can output both and compare. Also to clear try this.
std::cin.clear();
Ooh... After doing a little research, I realized I am using the wrong type. Unfortunately, we are both wrong. It should be "streamsize" and not int nor stringstream. :p

I have to disagree with cin.clear() :( That function only resets the error flags from a bad input to cin.

Edit:
Nevermind, I was wrong about streamsize. Anyway, I would much rather have a limit of 32*10^6 than just 80 for whatever junk the user decides to throw at my program before pressing enter...
Last edited on
Topic archived. No new replies allowed.