Cannot figure out this error for the life of me

Hi everyone, I am new to this forum. I came here for help because I would really like to know why I have this particular error. I have literally spent hours trying to figure out and debug this, but unfortunately no success.

The code below represents a simple program in progress I am trying to work on that basically adds two arrays of type char that hold int characters. I have no problem on the whole idea of this program, however there is a very nasty error preventing me from finishing this.

This error is a bit difficult to explain. The easiest way for you to see it is to run this code as is. Look and take note of the output. Then change the processArrays function so it looks like this:

1
2
3
4
5
6
7
8
9
void processArrays ()
{	
        int secondTerm = fillSecondArray();
	int firstTerm = fillFirstArray();
	
	
	cout << firstTerm << endl;
        cout << secondTerm;
}


After doing this, you should notice that whatever function is stored into a variable first, is going to have the correct output, and the latter, will have the wrong output.


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
121
122
#include <iostream>
#include <math.h>

using namespace std;

// Named constants - array capacity
const int CAPACITY = 20; 

// function declarations

int fillFirstArray ();
// fillFirstArray
// @return int
int fillSecondArray ();
// fillSecondArray
// @return int

int calculateArrayValue (const char a[], int numElement);
// calculateFirstArray
// @param const char[]
// @param int
// @return int

int findIntValue (char num);
// convertArray
// @param char
// @return int

void processArrays ();
// processArrays
// @param int
// @param int

int main()
{
	processArrays();

	cout << endl;
	system ("Pause");
	return 0;
}

int fillFirstArray ()
{
	cout << "Enter a positive integer of no more than 20 digits." << endl
		<< "Type (q) at the end of the integer: ";	

	char numInput, a1[CAPACITY];
	bool done = false;
	int numElement = 0;

	while (numElement < CAPACITY && !done)
	{
		cin.get(numInput);

		if (numInput == 'q')
		{
			done = true;
		}
		else
		{
			a1[numElement] = numInput;
			numElement++;
		}
	
	}
	return calculateArrayValue (a1, numElement);
}

int fillSecondArray ()
{
	cout << "Enter a positive integer of no more than 20 digits." << endl
		<< "Type (q) at the end of the integer: ";	

	char numInput, a1[CAPACITY];
	bool done = false;
	int numElement = 0;

	while (numElement < CAPACITY && !done)
	{
		cin.get(numInput);

		if (numInput == 'q')
		{
			done = true;
		}
		else
		{
			a1[numElement] = numInput;
			numElement++;
		}
	}
	return calculateArrayValue (a1, numElement);
}

int calculateArrayValue (const char a[], int numElement)
{
	int sum = 0;

	for (int i = 0; i < numElement; i++)
	{
		sum += findIntValue (a[i]) * pow (10.0, (numElement - (i + 1)));
	}

	return sum; 
}

int findIntValue (char num)
{
	return (int) num - 48; 
}

void processArrays ()
{	
	int firstTerm = fillFirstArray();
	int secondTerm = fillSecondArray();

	cout << firstTerm << endl;
        cout << secondTerm;
}



I would REALLY appreciate if someone could help me on this and explain why this error exists. I am more interested at this point how this particular error works than finishing the program.

Thanks!
Last edited on
what was happening was that after the user pressed return, the buffer was adding a ASCII character 10 into the stream, which had not been processed. Character 10 is the Line Feed symbol.

When you went to call the second function, it took into consideration this character (which you couldn't see) when looping to determine the number of items in the buffer. since it incremented the counter by 1, the numbers never matched. I fixed it by doing this:
1
2
3
4
5
6
7
8
9
10
void processArrays ()
{	
char t;
	int firstTerm = fillFirstArray();
	cin.get(t);
	int secondTerm = fillSecondArray();

	cout << firstTerm << endl;
        cout << secondTerm;
}


i use cin.get() to extract any single item symbols still in the string, with char t just being a trash can.

i'm not sure of another way to clear the stream.
Last edited on
Topic archived. No new replies allowed.