why it write the last twice??

good day all;

I have a question says

Write a complete C++ program that keeps read numbers from user (i.e. while(cin)), then find and write on a file each number with its divisors. At the end of the file write the number that has the maximum sum of divisors with its index.

Example:

input:
Enter Number 1: 25
Enter Number 2: 20
Enter Number 3: 5
Enter Number 4: 12
Enter Number 5: ^z (ctrl + z)

output file:
25: 5
20: 2 4 5 10
5: no divisors
12: 2 3 4 6
20 has maximum sum of divisors with index 2

here the 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
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
	int num, factor = 2,
		factSum = 0, index = 0,
		maxSum = 0, maxIndex = 0, maxNum;

	int count = -1;
	
	ofstream outFile;
	outFile.open("num.txt");

	while (cin)
	{
		cout << "Enter number " << index + 1 << ": ";
		cin >> num;	

		index ++;						//count the number we read
	
		outFile << num << " : ";

		//find factors and it's sum
		while (factor <= (num/2))
		{
			if (num % factor == 0)
			{
				outFile << factor;
				if (factor < (num/2))		//I won't comma after the last number but ...
					outFile << ", ";
				factSum += factor;
			}
			factor ++;
		}

		if (factSum == 0)
			outFile << "There is no divisors.";

		//find the number with max sum of factors
		if (factSum > maxSum)
		{
			maxSum = factSum;			//need for the condition
			maxIndex = index;			//need to print it
			maxNum = num;				//need to print it
		}
		
		//initialize the value
		outFile << endl;
		factSum = 0;
		factor = 2;

	}

	//print the result
	outFile << maxNum << " has the maximum sum of divisors with index " << maxIndex;
	outFile.close();

	return 0;
}


now the problem is that the output repeat the last number twice !! for example
instead of the result appear like befor, it appear like this

output file:
25: 5
20: 2 4 5 10
5: no divisors
12: 2 3 4 6
12: 2 3 4 6
20 has maximum sum of divisors with index 2

the cause as I think is when I enter (ctrl+z) to stop reading, the program use the last value it has and calculate it divisors.

now how can I solve this problem, and let the compiler read the last value once not twice???

and thanks alot for your help.

regards

You have to check the input stream after line 20.

1
2
3
if( cin ) {
  // then do stuff
}

I use if (cin)
but nothing change!!

does I use it in wrong way??
I use it after while(cin) directly

thanks
closed account (z05DSL3A)
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
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
    int num, factor = 2,
        factSum = 0, index = 0,
        maxSum = 0, maxIndex = 0, maxNum;

    int count = -1;
    
    ofstream outFile;
    outFile.open("num.txt");

    while (cin)
    {
        cout << "Enter number " << index + 1 << ": ";
        if( cin >> num)  
        {

            index ++;                        //count the number we read
    
            outFile << num << " : ";

            //find factors and it's sum
            while (factor <= (num/2))
            {
                if (num % factor == 0)
                {
                    outFile << factor;
                    if (factor < (num/2))        //I won't comma after the last number but ...
                        outFile << ", ";
                    factSum += factor;
                }
                factor ++;
            }

            if (factSum == 0)
                outFile << "There is no divisors.";

            //find the number with max sum of factors
            if (factSum > maxSum)
            {
                maxSum = factSum;            //need for the condition
                maxIndex = index;            //need to print it
                maxNum = num;                //need to print it
            }
            
            //initialize the value
            outFile << endl;
            factSum = 0;
            factor = 2;
        }

    }

    //print the result
    outFile << maxNum << " has the maximum sum of divisors with index " << maxIndex;
    outFile.close();

    return 0;
}
Last edited on
Oh, thanks
Topic archived. No new replies allowed.