c++ code giving headache

Apr 5, 2018 at 12:33pm
Write your question here.

When running the program given, it is supposed to give the output bewlow. However, the program
contains erros that prevent it from compiling and/or running. Correct the program so that it works
properly.

The output:

Please enter 10 integers, positive, negative, or zeros.
The numbers you entered are:
2
7
-4
-3
0
7
4
0
-9
-4

There are 6 evens, which includes 2 zeros.
The number of odd numbers is: 4

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

#include <iostream> 
using namespace std; 
 
const int LIMIT = 10; 
 
int main () 
{ 
    float counter; 
    int number; 
 
    int zeros; 
    int odds; 
    int evens; 
 
    cout << "Please enter " << Limit << " integers, " 
         << "positive, negative, or zeros." << endl; 
 
    cout << "The numbers you entered are:" << endl; 
 
    for (counter = 1; counter <= LIMIT; counter++) 
    { 
        cin << number; 
 
        switch (number / 2) 
        { 
          case 0: 
             evens++; 
              if (number = 0) 
                  zeros++; 
          case 1: 
          case -1: 
              odds++; 
        } 
    } 
 
    cout << endl; 
 
    cout << "There are " << evens << " evens, " 
         << "which includes " << zeros << " zeros." 
         << endl; 
    cout << "The number of odd numbers is: " << odds 
         << endl; 
 
    return 0; 
}
  Put the code you need help with here.
Apr 5, 2018 at 1:07pm
what are you having trouble with? This looks like the original code you should correct.
for one thing, it says it will print what you entered before you entered it. For another, number/2 won't give you an even odd test. There is no default case. case 1 makes no sense at all. try math:

11/2 = 5 (integer math).
switch (5)
is it 0? no.
is it 1? no
is it -1? no
what do you do, then??
Apr 5, 2018 at 9:06pm
yes i have been ask to correct and been trying it the whole day that is why i am asking for help
Apr 5, 2018 at 9:24pm
Line 16 vs line 5 and line 21: uppercase and lowercase are different. Write LIMIT on line 16.

Line 23: << should be >>

Line 29: = (assignment) should be == (comparison for equality)

Line 9: a counting variable should be an int, not a float.

Lines 12, 13, 14: you need to initialise each of odds, evens and zeros (to 0) - otherwise you would be adding to ... goodness knows.

Line 25: should be % not / (this is what @jonnin was trying to get you to work out for yourself; please don't ignore it)


Your compiler should give you the lines on (or, occasionally, near) which there are errors.
Last edited on Apr 5, 2018 at 9:26pm
Apr 5, 2018 at 9:31pm
lastchance wrote:
Your compiler should give you the lines on (or, occasionally, near) which there are errors.

Exactly my thoughts. If your compiler is bad, try one of the online ones, like at https://repl.it/languages/cpp , which I use alllllll the time when helping people here ;D This one gives you suggestions like "did you mean X?"
Apr 5, 2018 at 9:55pm
thanks a lot lastchance im still a biginner on c++ still struggling much and with a person like you I will get there..thanks a lot
Topic archived. No new replies allowed.