Getting weird values after outputting numbers after loop?!

Hello,

Basically the program reads the file char by char finds the numbers, finds out which one is the biggest, which the smallest, and the average of the numbers.


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
ifstream input;
void decrypt(file){
int declarations....
c = input.get()
while (! input.eof())
{
            if (c >= '0' and c <= '9')  
            {
                number = (number * 10) + (c - '0'); 
            }
            else
            {
                if (nums == true)
                {
          
                    if (number >= maximum)  
                    {
                        maximum  = number;
                    }
                    else
                    {
                        minimum = getal;
                    }

                    sum += getal;  
                    counter++
                }
                nums = false; 
                number = 0;               
                
      c = input.get();
}
cout << "max: " << maximum << endl << "min: " << min << endl << "average: " << som / counter << endl;
}


Then what happens is the minimum is always right. The other two RARELY, usually
it's some huge number like 32445.

Basically the files contains something like Hello13 Whatsup31 Bla22

Anyone know what's going on?
Cheers

Edit: I do call the function in main() of course

Well it just got weirder. While I compile in terminal g++ the numbers don't work. In codeblocks they do work ...

Also: We're not allowed to use any containers (except string for the filename)
Last edited on
try this :

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

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>

int main(int argc, char* argv[])
{
	std::vector<unsigned int> data;

	std::ifstream file("yourfile.txt");

	if (!file.good())
	{
		std::cout << "Could not read the file\n";
		return 0;
	}
	while (!file.eof())
	{
		//store the string in a temporary variable
		std::string temp("");
		file >> temp;

		//extract the value from the temporary string to the value variable
		unsigned int value;
		std::string temp2("");
		for (unsigned int i = 0; i < temp.size(); i++)
		{
			if (isdigit(temp[i]))
			{
				temp2 += temp[i] ;
			}
		}
		std::istringstream stream(temp2);
		stream >> value;

		//insert the value into the container at the end
		data.push_back(value);
	}

	//calculate the average
	std::vector<unsigned int>::iterator it_begin = data.begin();
	std::vector<unsigned int>::iterator it_end = data.end();
	unsigned int sum = 0;
	while (it_begin != it_end)
	{
		sum += *it_begin;
		it_begin++;
	}
	double average = sum / data.size();

	std::cout << "\nThe average is " << average;

	//sorting the container , so that the min value is at the beginning
	//and the highest value is at the end
	std::sort(data.begin(), data.end());

	std::cout << "\nThe minimum value is " << *data.begin();

	std::cout << "\nThe maximum value is " << *(--data.end());

	return 0;
}
Last edited on
Ericool,

Thank you very much for the answer.

However, we are not allowed to use any containers except strings for the filenames. I apologize I forgot to mention that!
Well now the old code seems to work every time when I compile in codeblock, but not in Ubuntu g++... I am flabbergasted...
make sure to tell all the restraints before , how would I know ? I am replacing now the implementation for you.
Ericool,

You're right. I apologized for the inconvenience. I forgot to mention it the first time around.

Do you know why the code works when I compile and run in Code Blocks but not with g++ in linux terminal?
I think "c = input.get();" should be in "while statement", otherwise "while(!input.eof())" will not terminate, and in the first if statement you should turn nums back to true.
Last edited on
jinyang, it's a bad copy paste. You're right. It is in there in the original code. Will add it to the post now.
and now ?
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

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>

int main(int argc, char* argv[])
{

	unsigned int* data;

	std::ifstream file("yourfile.txt");

	if (!file.good())
	{
		std::cout << "Could not read the file\n";
		return 0;
	}

	unsigned int size = 3;
	data = new unsigned int[3];


	int forward_iterator = 0;
	while (!file.eof())
	{
		//store the string in a temporary variable
		std::string temp("");
		file >> temp;

		//extract the value from the temporary string to the value variable
		unsigned int value;
		std::string temp2("");
		for (unsigned int i = 0; i < temp.size(); i++)
		{
			if (isdigit(temp[i]))
			{
				temp2 += temp[i] ;
			}
		}
		std::istringstream stream(temp2);
		stream >> value;

		//insert the value into the container at the end
		data[forward_iterator] = value;
		std::cout << "\n " << data[forward_iterator];
		forward_iterator++;
	}

	//calculate the average
	unsigned int i = 0;
	unsigned int sum = 0;
	auto min = *data;
	auto max = *data;
	while (i < size)
	{
		sum += data[i];
		if (data[i] < min) min = data[i];
		if (data[i] > max) max = data[i];
		i++;
	}
	double average = sum / size;

	std::cout << "\nThe average is " << average;

	std::cout << "\nThe minimum value is " << min;

	std::cout << "\nThe maximum value is " << max;

	delete[] data;

	return 0;
}
Last edited on
@Ericool I think the variable "size" in line 57 and 64 may be replaced by "forward_iterator" because size is fixed with 3 in your code.
that 's right
Topic archived. No new replies allowed.