loop not working correctly

My loop's not working correctly and I don't understand why.
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

#include<iostream>
#include <cctype>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() 
{
	const int SIZE = 20;
	char input[SIZE];
	int total = 0;
	int count = 0;
	double average;
	
	cout << "This program wil average a series of numbers\n";
	cout << "Enter the first number or Q to quit; ";
	cin.getline(input, SIZE);

	while (tolower(input != "q"))
	{
		total += atoi(input);
		count++;
		cout << "Enter the next number or Q to quit; ";
		cin.getline(input, SIZE);
	}
	if (count != 0) 
	{
		average = (total/count);
		cout << "Averge is " << average << endl;
	}
		return 0;
	}

I'm mostly copying an example from my book but with slight changes to see if it works.
This is their version.
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

#include<iostream>
#include <cctype>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() 
{
	const int SIZE = 20;
	char input[SIZE];
	int total = 0;
	int count = 0;
	double average;
	
	cout << "This program wil average a series of numbers\n";
	cout << "Enter the first number or Q to quit; ";
	cin.getline(input, SIZE);

	while (tolower(input[0] != "q")) // I don't understand why the [0] is necessary
	{
		total += atoi(input);
		count++;
		cout << "Enter the next number or Q to quit; ";
		cin.getline(input, SIZE);
	}
	if (count != 0) 
	{
		average = static_cast<double>(total)/count; // I had to go back
// and relearn what static_cast does and after doing so I don't understand
// why they didn't just make the variable "total" a double in the first or why
// it even needs to be a double
		cout << "Averge is " << average << endl;
	}
		return 0;
	}
Last edited on
The [0] is necessary because you want the first (0th) character in the array of characters. Without it, you're getting the address of the array.

When you divide to integers like you have, it does integer division, truncating any decimal portion of the answer. So 1 / 3 would be 0. In order to force floating point division and preserve that decimal part, one of the operands (in this case, total) is cast to a double.
Zhuge wrote:

he [0] is necessary because you want the first (0th) character in the array of characters. Without it, you're getting the address of the array.


I see what you mean but why can't it be []? Why do you have to put 0?
Zhuge wrote:

When you divide to integers like you have, it does integer division, truncating any decimal portion of the answer. So 1 / 3 would be 0. In order to force floating point division and preserve that decimal part, one of the operands (in this case, total) is cast to a double.

Oh ya ha sorry I forgot. Is there a reason though why the book didn't just make both of those variables doubles to begin with?
Last edited on
I believe it has something to do with the fact that that can lead to spotty stuff. Now, I'm asking here--is it the case that sometimes declaring a double variable and then inputting an integer can lead to that integer having a decimal part? That is, can inputting "8" give "8.6"?
Also, the reason it's [0] is that it's checking the first array value, which is at [0], for the quit signal.
QWERIYman wrote:
Also, the reason it's [0] is that it's checking the first array value, which is at [0], for the quit signal.

Are you saying 0 is the address of the first element in any array? If so, are the adresses of the following elements 1, 2, 3, ect.?

Edit: After trying to put a 1 in there and rereading what you said, I understand what you mean. I still wonder though do array positions have their own addresses? (separate from the address of the array)
Last edited on
Topic archived. No new replies allowed.