solve this and post it in comment

The program is supposed to take in two numbers from the user and display them separated by a comma, with the smallest one displayed first. This program does run, so there are no syntax errors, but you need to correct it based on the following description.

The program is supposed to check and make sure that both numbers are positive. Only if they are positive, should it display the numbers separated by commas. If both numbers are not positive, the program should only display “Error: Negative Numbers!”.

Please correct the logic errors and submit updating comments at the top of the program.

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
#include <iostream>

using namespace std;

int main()
{	
	//declare variables
	int num1 = 0;
	int num2 = 0;
	int temp = 0;

	//enter input
	cout << "Enter the first number: ";
	cin >> num1;
	cout << "Enter the second number: ";
	cin >> num2;

	if (0 > num1 || num2 > 0)	{ //check positive numbers
		//if necessary, swap numbers
		if (num2 >= num1)	{
			temp = num1;
			num1 = num2;
			num2 = num1;
		}	//end if
	}
	else	{	//not positive
		cout << "Error: Negative Numbers!" << endl;
	}
	
	//display output
	cout << num1 << ", " << num2 << endl;
	
	return 0;
}	//end of main function 
Last edited on
that looks like fun. A good problem as when you get out of school and into a job you will sometimes have to fix some guys code; people that cheated through school get hired and leave a mess for others to clean up and you have to fix it kinds of things.

How lovely. Not.

You have edited the OP of your previous thread http://www.cplusplus.com/forum/beginner/245220/
to contain same post as this (erasing that threads original content). That thread is now meaningless. That is not acceptable.


Formally, you have now two threads on the same problem. That would be called double-posting. Double-posting is waste of everybody's time.


You have misunderstood your homework. It is your task to understand and edit the code. Have fun, learn well?

There are at least four blatant errors for you to correct.
closed account (1vRz3TCk)
anthony2011,

I'm willing to give you the benefit of doubt. I've edited the code but you will have to compare it to the original.

I hope you pay attention to the others (above). Once you have posted only edit it to fix spelling/formatting. Add replies at the end.

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
#include <iostream>

using namespace std;

int main()
{	
	int num1 = 0;
	int num2 = 0;
	int temp = 0;

	cout << "Enter the first number: ";
	cin >> num1;
	
	cout << "Enter the second number: ";
	cin >> num2;

	if (0 > num1 || 0 > num2)	{
	    cout << "Error: Negative Numbers!" << endl;
	} else {
		if (num1 >= num2)	{
			temp = num1;
			num1 = num2;
			num2 = temp;
		}
	    cout << num1 << "," << num2 << endl;
	}	
	
	return 0;
}


...and a bit of messing around...
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
#include <array>
#include <ios>
#include <iostream>
#include <algorithm>

template <typename T>
T getValidatedInput()
{
    T result;
    std::cin >> result;
    if (std::cin.fail() || std::cin.get() != '\n') {
        std::cin.clear();
        while (std::cin.get() != '\n')
            ;
        throw std::ios_base::failure("Invalid input.");
    }
    return result;
}

int main()
{
    using std::cerr;
    using std::cin;
    using std::cout;
    using std::exception;

    constexpr int count = { 2 };
    std::array<int, 2> input { 0 };
    bool hasNegative = false;
    for (int i = 0; i < count;) {
        cout << "Please enter " << (!i ? "first" : "second") << " number: ";
        try {
            input[i] = getValidatedInput<int>();
            if (input[i] < 0)
                hasNegative = true;
            i++;
        } catch (exception e) {
            cerr << e.what() << "\n";
        }
    }
    if (hasNegative)
        cout << "Error: Negative Numbers!\n";
    else {
        std::sort(input.begin(), input.end());
        cout << input[0] << "," << input[1] << "\n";
    }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.