Help with a program

The program should read integers from a text file and then determine if the sum of the odd numbers is equal to the product of the equal numbers.

the text files given are for example:
21153291380
612280377
111-18

it should stop if non integer values are entered.
Here's what I have:
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
 
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	//Declare variables
	string Fname;
	ifstream inFile;
	int prodE = 0, sumO = 0, num;
	
	//input
	cout << "Enter a file name: ";
	getline(cin,Fname);
	
	inFile.open(Fname.c_str());
	
	//output
	if(!inFile)
	{
		cout << "Error entering " << Fname << " because no such file exists" << endl;
		return 0;
	}		
	
	if(inFile)
	{
		cout <<"Input: "; 
		while ( inFile >> num)
		{	
			
			if ( num % 2 == 0 && num != 0)
			{
				prodE *= num;
				cout << num<< " ";
				
			}
			else 
			{
				sumO += num;
				cout << num<< " ";
				
			}
		}
	}
	
	cout << endl;
	cout << "Result: ";
	
	if ( prodE == sumO )
	{	
		cout << endl;
		cout << "success" << endl;
	}
	else 
	{
		cout << "fail" << endl;
	}
	return 0;
}


I know i have a problem with how im reading in the data, but i have no idea how to fix it.

example output (what it should be):
Enter the file name: data01.txt
Input: 2 1 15 3 2 9 1 3 8
Result: success
21153291380
612280377
111-18


I think you may be missing some spaces here, because otherwise your program will not work at all. One thing I'm sure is wrong is this:

1
2
3
int prodE = 0;
//...
prodE *= num;

Any number multiplied by 0 is 0.

If what I assume is true (that you didn't put in the spaces for some reason), then you are supposed to compute the product and sum for the numbers on each line. As in, do not compute a product and sum for all the numbers in the whole file in one go. Otherwise, I'm not sure that int is big enough.
Last edited on
Thanks for the reply. The text files given don't have spaces, but it's worded so that you should only use one of the text files. So I don't really know:

Write a program that reads positive integers from a file and then display “success" if the
sum of the odd numbers read equals the product of the even numbers read.
To illustrate what is required, note that if the file contains the numbers 2, 1, 15, 3, 2, 9, 1, 3, 8
the program should output “success" because
1 + 15 + 3 + 9 + 1 + 3 = 2 × 2 × 8
Let the user specify the name of the file. The program should read values from the
specified file and echo them to the screen while processing them. Processing should
end when the whole file is processed or if zero, a negative number, a non-integer or
a character is encountered before the end of the file. For your convenience we have
provided files with test data that you may use to test your program.

I'm just going to say fuck it and put in spaces as i realise now that that is probably how it should be.

Edit:

I changed the prodE to no longer equal 0;
I get a fail when it should be success though. can you spot any other error?
Last edited on
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 <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	//Declare variables
	string Fname;
	ifstream inFile;
	int prodE , sumO = 0;
	int num;
	
	//input
	cout << "Enter a file name: ";
	getline(cin,Fname);
	
	inFile.open(Fname.c_str());
	
	//output
	if(!inFile)
	{
		cout << "Error entering " << Fname << " because no such file exists" << endl;
		return 0;
	}		
	
	if(inFile)
	{
		cout <<"Input: "; 
		while ( inFile >> num)
		{	
			
			if ( num % 2 == 0 && num != 0)
			{
				prodE *= num;
				cout << num<< " ";
				
			}
			else 
			{
				sumO += num;
				cout << num<< " ";
				
			}
		}
	}
	
	cout << endl;
	cout << "sum: " << sumO << " || prodE" << prodE<< endl; 
	cout << "Result: ";
	
	if ( prodE == sumO )
	{	
		cout << endl;
		cout << "success" << endl;
	}
	else 
	{
		cout << "fail" << endl;
	}
	return 0;
}


changed my code to show me the values. The odd ones are fine but the even ones are showing up as: -345689 and so forth, changes every time i run it
Last edited on
prodE should be initialized to 1 on line 15.
Topic archived. No new replies allowed.