My output is wrong

Here is an even or odd program. I need to sum the even values up to 180 and odd values up to 160. Each time I run this program, the output is even = 8190 odd 6400. I believe the correct outputs are even = 8010 and odd = 6241. Can you help me understand what I am doing wrong?

Also, I would like to add, I don't want to alter the syntax of any of the code. It works properly.

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

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    ofstream Brady;
    Brady.open("EvenOrOdd.out");

   int i, n, x, sum=0;

    string c;

    cout << "Enter even or odd: ";
     cin >> c;
    cout << "Enter the number: ";
     cin >> n;

    if(c=="even")
    {
        i=0;
        while(i<=n)
        {
          sum+=i;
            i+=2;
        }
    }
           if(c=="odd")
            {
                i=1;
                while(i<n)
                {
                    sum+=i;
                      i+=2;
                }
            }
    Brady << "The total for the " << c << " values of " << n
          << " is " << sum;
    Brady.close();
    return 0;
}
why is it <= for even and < for odd?
run it with something you are absolutely sure the answer of, like 10.
1+3+5+7+9 is 25.
2+4+6+8+10 is 30.
do those work?

It works properly.
??? if it worked properly, you would get the correct answers. But I get that you don't want a rewrite, it just struck me funny...
Last edited on
8190 looks correct to me.
sum 2*i, i=0,i=90
https://www.wolframalpha.com/input/?i=sum+2*i%2C+i%3D0%2Ci%3D90

6400 also looks correct to me.
sum 2*i + 1, i=0, i=79
https://www.wolframalpha.com/input/?i=sum+2*i+%2B+1%2C+i%3D0%2C+i%3D79

(I re-phrased the question to WolframAlpha so it could understand it, there is probably a better way)

EDIT:
I figured out a better way to talk to WolframAlpha, using Wolfram syntax.

Sum[i,{i,0, 180,2}]
https://www.wolframalpha.com/input/?i=Sum%5Bi%2C%7Bi%2C0%2C+180%2C2%7D%5D&assumption=%22i%22+-%3E+%22Variable%22&assumption=%7B%22C%22%2C+%22Sum%22%7D+-%3E+%7B%22SumWord%22%7D&assumption=%22i%22+-%3E+%22Variable%22

Sum[i,{i,1, 160,2}]
https://www.wolframalpha.com/input/?i=Sum%5Bi%2C%7Bi%2C1%2C+160%2C2%7D%5D&assumption=%22i%22+-%3E+%22Variable%22&assumption=%7B%22C%22%2C+%22Sum%22%7D+-%3E+%7B%22SumWord%22%7D

Same result. So your assumptions seem to be wrong.

_____________________________________________

My output is wrong
I don't want to alter the syntax of any of the code. It works properly.
So does it work properly or does it not?

If you're referring to previous requests to just print to cout instead of a file, let me explain it in a different way: It doesn't matter to us that your professor forces you to write to a file. If you want help from us, it make our lives easier if we can immediately run code that prints to cout, instead of having to edit the source code ourselves to have it print to cout instead of a file.

We'll still help you either way, but it would be nicer if we didn't have to edit your source code every time to get it to run properly on cpp.sh
Last edited on
Topic archived. No new replies allowed.