Issue error checking user input

I am having an issue with the "while" statement that checks the users input and verifies it's not a negative number or a letter.

The code works for the check, the issue comes if the user enters a valid number the first time through the program. It just sits there, if I enter the number or a different number again then it processes through and the program works.

I can't see why my "while" statement in the "main()" is failing on the first iteration... can anyone point me in the right direction?

Thanks


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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include<limits>
using namespace std;


struct listrec
{
	listrec *prev;
	listrec *next;
	float value;
};

listrec *head, *tail, *current, *temp;
float val;
int size;


int listSize (struct listrec* item, string direction)
{
	struct listrec* cur = item;
	int size = 0;
	if(direction == "forward"){

		while (cur != NULL)
		{
			++size;
			cur = cur->next;
		}
	}
	else {
		while (cur != NULL)
		{
			++size;
			cur = cur->prev;
		}

	}
	return size;
}

void printForwardElem() {
	std::cout << "Print Forward: " << std::endl;
	// Going forwards.
	listrec *iter;
	iter = head;
	while (1)
	{
		std::cout << iter->value+1 << std::endl;
		iter = iter->next;
		if (iter ==NULL)
			break;
	}
	size = listSize(head, "forward");
	cout << " total modules "<<size<< endl;

}


void printBackwardElem(){
	std::cout << "Print in reverse order: " << std::endl;
	// Reverse
	listrec *iter;
	iter = tail;
	while (1)
	{
		std::cout << iter->value+1 << std::endl;
		iter = iter->prev;
		if (iter == NULL)
			break;
	}
	size = listSize(tail, "backward");
	cout << " total modules "<<size<< endl;
}


int main()
{
	int userin;
	std::cout << "How many nodes do you want to create?: ";
	std::cin >> userin;
	while(!(cin >> userin) || userin < 0)
	{
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout << "Invalid input, the input must be zero or greater and not a letter, Please enter again:" << endl;
	}

	head   = NULL;
	tail    = NULL;
	current = NULL;
	temp    = NULL;

	for (int i = 0; i < userin; ++i)
	{
		//  std::cout << "Enter " << i << "th value: ";
		//  std::cin >> val;      
		val = i;
		temp        = new listrec;
		temp->prev    = current;
		temp->next    = NULL;
		temp->value = val;

		if (current != NULL)
			current->next = temp;      
		current    = temp;

		// It's the first iteration.
		if (head == NULL && current->prev == NULL)
			head = temp;
		// Last Iteration.
		else if (i == userin - 1)
			tail = temp;
	}

	printForwardElem();
	printBackwardElem();   

	return 0;
}
Last edited on
Remove line 80. The loop condition will read the input from the user.

1
2
std::cin >> userin;
while(!(cin >> userin) || userin < 0)
Last edited on
Oh!! I see it now...

Thank you!
Topic archived. No new replies allowed.