This random 2?!

Ok, whenever i input an even number' it keeps adding and additional 2. ex, if i input 22 it should list 2, 4, 6 .... 22. but instead it stops at 24. Why is this?



#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <fstream>
using namespace std;

int main()


{
float display;
int number, que1, que2, que3;

display = 0;

cout << "Please input a number greater then zero to find the multiples of the even\nand odd numbers. If you would like to exit please input zero";
cin >> number;

que1 = number % 5;
que2 = number % 2;
que3 = number % 1;

{
if (que1 == 0)
for (display = 0; number > display;)
{display = display + 5; //I had to do it this way because I could not put it in the for statement
cout << display << " " ;}

else if (!que1 == 0 && que2 == 0)
{display = 0;
while (number > display)
{display = display + 2;
cout << display << " ";}}

else if (que3 == 0)
display = -1;
do{
display = display + 2;
cout << display << " ";
}while (number > display);
}
{
while (number < 0)
{cout << "Error, bad input. Please try again" << endl;
cin >> number;}

if (number == 0)
exit(0); }


return 0;
}

Not absolutely clear about what the program is expected to do. Is it something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    int number ;
    std::cin >> number ;

    int delta = 1 ;
    if( number%5 == 0 ) delta = 5 ;
    else if( number%3 == 0 ) delta = 3 ;
    else if( number%2 == 0 ) delta = 2 ;

    for( int n = delta ; n <= number ; n += delta ) std::cout << n << ' ' ;
    std::cout << '\n' ;
}
Its an assignment, ik it looks weird. We are almost forced to use a FOR, WHILE, AND A DO. everything is working in my program besides a random 2 being added when an even number is inputed.

NOTE: In simple terms the program is suppose to receive a number, and out multiples of it. 22 = 2, 4, 6, 8...... and 25 would be 5, 10 ,15..... 9 being 1, 3, 5..... and so forth. while making sure the user does not input a negative number and allows them to exit by inputin 0

ou are to write a program to utilize three different looping structures: WHILE,
FOR, and DO. Your program should loop processing input from the user until he indicates
that he wants to quit by entering a zero for the integer.
INPUT: Prompt the user to enter a positive integer or a zero to quit. You may
assume that the user has entered an integer but you must make sure that it is
positive or zero. If it is not positive or zero, loop until the user enters a positive
integer or zero. If the user enters a zero, the program should end.
PROCESSING: If the number entered is a multiple of 5, print all the multiples of 5
from 5 through the number entered. Use a FOR loop to generate the numbers.
If the number entered is an even number, print all even numbers from 2 through the
number entered. Use a WHILE loop to generate the numbers.
If the number entered is an odd number, print all odd numbers from 1 through the
number entered. Use a DO loop to generate the numbers.
Only print one set of numbers for each value entered. For example, if 20 is entered,
print 5 10 15 20 but do not print 2 4 6 8 ... 20.
OUTPUT: Print the desired numbers to the screen.
METHODOLOGY: If the remainder of the number divided by 5 is 0, the number is a
multiple of 5. If the remainder of the number divided by 2 is 0, the number is even.
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
#include <iostream>

int main()
{
    int number = 1 ;
    while( std::cout << "a number greater then zero (0 or negative to quit)? " &&
           std::cin >> number && number > 0 )
    {
        // If the number entered is a multiple of 5, print all the multiples of 5
        // from 5 through the number entered. Use a FOR loop to generate the numbers.
        if( number%5 == 0 ) for( int n = 5 ; n <= number ; n += 5 ) std::cout << n << ' ' ;

        // If the number entered is an even number, print all even numbers from 2 through the
        // number entered. Use a WHILE loop to generate the numbers.
        else if( number%2 == 0 )
        {
            int n = 2 ;
            while( n <= number ) { std::cout << n << ' ' ; n += 2 ; }
        }

        // If the number entered is an odd number, print all odd numbers from 1 through the
        // number entered. Use a DO loop to generate the numbers.
        else
        {
            int n = 1 ;
            do { std::cout << n << ' ' ; n += 2 ; } while( n <= number ) ;
        }

        std::cout << '\n' ;
    }
}
Topic archived. No new replies allowed.