For Loop is not working

Jun 6, 2013 at 2:31am
Hello,

I'm having an issue with my code when doing the For Loop. I have to input single digit values in my code where it adds up all the values I input and then show the Lowest number inputed and the Highest number inputed. The code works perfectly when adding up the values no matter what I input BUT when trying to display the Lowest and Highest Value nothing seems to show up.

Here is my code:

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
 #include <iostream>
#include <iomanip>
#include <string>

using namespace std;

// The user inputed values are solved by the program

void Arrayinfo()
{
	const int size = 30;		// Array Size
	char num[size];				// To hold the number of inputs
	int count = 0,i;			// Loop counter variable
	
	cin >> num;

	for (i=0; i < strlen(num); i++)
	{
		count += num[i] -'0';
	}

	cout << "The Total is " << count << endl;

	int highest, lowest;		// Highest and lowest user inputed value
	int c, num;
	highest = num[0];			// Highest 
	lowest = num[0];			// Lowest

	

	for (num=1; num < size; c++)
	{
		if (num[c] > highest)
			highest = num[c];
		cout << "Highest number is: " << highest << endl;
	}


	for (num=1; num < size; c++)
	{
		if (num[c] < lowest)
			lowest = num[c];
		cout << "Lowest number is: " << lowest << endl;
	}
	

}

// The heading where Program asks user to input their single
// digit values

void userinHeading()
{

	cout << "Enter single digit numbers into the program: " << endl;
}

// Main Function

int main ()
{
	userinHeading();
	Arrayinfo();
	
	return 0;
}



I'm going by an example the Professor put on one of his slides when trying to display the Lowest and Highest values but nothing works :( Help :(

Jun 6, 2013 at 4:19am
You have an int num and a char num, which is undoubtedly causing some problems.
Jun 6, 2013 at 9:27am
At line 25, you declare a variable int num. However, you have already declared char num[size]; at line 12.

Does your compiler not warn you about this?

Jun 6, 2013 at 1:28pm
why did he not have to declare i? is it because it is being used to initialize an int and thus the compiler will auto cast it as an int?
Last edited on Jun 6, 2013 at 1:28pm
Jun 6, 2013 at 1:33pm
i is declared on line 13, the same line as count.

Personally, I prefer to declare each variable on its own line (i.e. in its own statement), to make things as clear as possible.
Jun 6, 2013 at 1:51pm
ah ok just never seen a variable declared in that manner XD. I always also declare my variables before I try to declare/initialize another variable through a formula like.
int a
int b
int sum = a + b;
Topic archived. No new replies allowed.