While loop error

Hello, on line 50 in my else I am attempting to add even numbers between to numbers entered in by a user. My issue is that if an even number is entered, then it will include the even number in the sum. I'm trying to make it so that it's only even numbers in between the two. Everything else appears to work from testing it. Any ideas?

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
#include <iostream>
#include <conio.h> //use of _getch();
#include <math.h> //Use of fmod
using namespace std;

void user_input(double &fNum, double &sNum); //Function prototype
void computation(double comp1, double comp2); //Function prototype

int main() {
	double first, second;
	user_input(first, second); //Function call
	computation(first, second); //Function call
	_getch();
	return 0;
}

void user_input(double &fNum, double &sNum) { //Function grabs user input, then checks for valid input
	cout << "Input two numbers, your first number must be larger than" << endl;
	cout << "your second number. The program will then output all odd" << endl;
	cout << "numbers between the two, and the sum of all even between the two" << endl;

	cout << "What is your first number?" << endl;
	cin >> fNum;
	cin.ignore(256, '\n');
	cout << "What is your second number?" << endl;
	cin >> sNum;
	cin.ignore(256, '\n');
	
	while (fNum >= sNum) { //Check user input
		cin.clear();
		cout << "Invalid input, first number must be less than second" << endl;
		cout << "What is your first number?" << endl;
		cin >> fNum;
		cin.ignore(256, '\n');
		cout << "What is your second number?" << endl;
		cin >> sNum;
		cin.ignore(256, '\n');
	}


}

void computation(double comp1, double comp2) { //Checks for odd numbers between user input, display sum of even numbers
	double sum = 0;
	while (comp1 < comp2) {
		if (fmod(comp1, 2) != 0) { //Fmod used for checking remainders
			cout << comp1 << " is an odd number" << endl;
		}
		else {
			sum += comp1; //Adding even numbers
		}
		comp1++; //Incrememnt comp1 until check fails
		
	}
	cout << "The sum is of all even numbers between your numbers is: " << sum << endl;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
    int sum = 0;
    int start = 0;
    int finish = 0;
    
    std::cin >> start >> finish;
    
    for(int i = start; i <= finish; i++)
    {
       if (i % 2 == 0 and i != start)
           sum += i;
    }
    
    std::cout << sum << '\n';
    
    return 0;
}


You can also write an equivalent few lines using a while loop.
Last edited on
OP: note that odd/even only apply for integers, not doubles. Perhaps you were thinking 5.6 is 'even' but 6.5 is 'odd' etc but note you can always add a 0 to the end of the decimal representation w/o changing the value. For a more formal mathematical approach: http://math.stackexchange.com/questions/92451/can-decimal-numbers-be-considered-even-or-odd
OP: note that odd/even only apply for integers, not doubles.


That's what I was thinking actually

You can also write an equivalent few lines using a while loop


Just got home from work, for this I'm looking to continue using a while loop so hopefully I'll be able to get the equivalent to work.

Thank you.
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
#include <iostream>
#include <conio.h> //use of _getch();
#include <math.h> //Use of fmod
using namespace std;

void user_input(double &fNum, double &sNum); //Function prototype
void computation(double comp1, double comp2); //Function prototype

int main() {
	double first, second;
	user_input(first, second); //Function call
	computation(first, second); //Function call
	_getch();
	return 0;
}

void user_input(double &fNum, double &sNum) { //Function grabs user input, then checks for valid input
	cout << "Input two numbers, your first number must be larger than" << endl;
	cout << "your second number. The program will then output all odd" << endl;
	cout << "numbers between the two, and the sum of all even between the two" << endl;

	cout << "What is your first number?" << endl;
	cin >> fNum;
	cin.ignore(256, '\n');
	cout << "What is your second number?" << endl;
	cin >> sNum;
	cin.ignore(256, '\n');

	while (fNum >= sNum) { //Check user input
		cin.clear();
		cout << "Invalid input, first number must be less than second" << endl;
		cout << "What is your first number?" << endl;
		cin >> fNum;
		cin.ignore(256, '\n');
		cout << "What is your second number?" << endl;
		cin >> sNum;
		cin.ignore(256, '\n');
	}


}

void computation(double comp1, double comp2) { //Checks for odd numbers between user input, display sum of even numbers
	double sum = 0;
	double x = comp1;
	while (comp1 < comp2) {
		if (fmod(comp1, 2) != 0) { //Fmod used for checking remainders
			cout << comp1 << " is an odd number" << endl;
		}
		if (fmod(comp1, 2) == 0 && x != comp1){
			sum += comp1;
		}
		comp1++; //Incrememnt comp1 until check fails
		}
	cout << "The sum is of all even numbers between your numbers is: " << sum << endl;
}


Looks like I got it to work, thank you kermort!
closed account (48T7M4Gy)
Well done!

FWIW a couple of points:
1. 'mod' is OK but '%' is better because you don't need math.h overhead as small as it might be.

2. You don't need all the cin.ignore's, only if you go from a cin to a getline. They don't do much at all as an error trapping solution if that was your purpose.
Topic archived. No new replies allowed.