else if issue

logical error in my else if statement, not sure what's causing it
thanks for the assist


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
57
58
59
60
61
62
63
64
65
66
  #include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{

/* Sample Output:
 input three different integers: 5 10 15
 
 */
    int number1;
    int number2;
    int number3;
    int smallest;
    int largest;
    
    
    
    cout << "enter 5" << endl;
    cin >> number1;

    cout << "enter value 10" << endl;
    cin >> number2;


    cout << "enter value 15" << endl;
    cin >> number3;

    
    largest = number1;
    //Write a statement to determine if number2 is greater than largest
    
    if (number2 > largest)
    {
        //number2 = largest;
        largest = number2;
        cout << "the number 2 value is the largest with a value of " << number2 << endl;

    }

        //Write a statement to determine if number3 is greater than largest
    else
        if (number3 > 0) {
            largest = number3;
            cout << "the number 3 value is the largest with a value of " << number3 << endl;
        }

        else {

            cout << "the number 1 value is the largest with a value of " << number1 << endl;
        }

    
   
      

    
    return 0;


    
       

}
Last edited on
Like an error error, or a bug error.
Last edited on
//Write a statement to determine if number3 is greater than largest

Tell me, is your code doing that? Are you comparing number3 with largest?
In line 45 condition should be
(number3 > largest)
Correct, I am comparing number3 with largest.
to test to see if that statement was being read all, I also used " if number3 > 0" to force a output and I got nothing. This is leading me to believe that line of code below is not being evaluated at all...
I'm running Microsoft visual compile, not sure if that would make a difference
thanks again
Farrris


if (number3 > largest) {
(largest = number3);
cout << "the number 3 value is the largest with a value of " << number3 << endl;
}

What is your program supposed to do, find the largest and smallest of 3 values?
Yes,
by typing in 5, 10, 15
it is supposed to return 15 as the largest value
Have you learned about/can you use a container and loops? Finding largest/smallest could be streamlined using them.
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
#include <iostream>

int main()
{
   // create a constant variable to avoid using "magic numbers"
   const int MAX_SIZE { 3 };

   // initialize a simple array to contain 3 values
   // let's mix up the values
   int numbers[MAX_SIZE] { 10, 5, 15 };

   // initialize smallest to 10,000
   // (10,000 is an arbitrary placeholder for comparisons, actual numbers will be smaller)
   int smallest { 10000 };
   // set largest to zero
   int largest  { };

   for (int i { }; i < MAX_SIZE; i++)
   {
      // check to see if array value is is smaller than current smallest
      // if it is assign it to smallest
      if (numbers[i] < smallest)
      {
         smallest = numbers[i];
      }

      // check to see if array value is is larger than current largest
      // if it is assign it to largest
      if (numbers[i] > largest)
      {
         largest = numbers[i];
      }
   }

   std::cout << "Largest: " << largest << '\n' << "Smallest: " << smallest << '\n';
 }

Largest: 15
Smallest: 5

Now you can change how many numbers you check for largest and smallest, with no need to change the core logic for finding them.
The original code starts by assuming number1 is largest.

Then it checks to see if number2 is larger than that. If number2 is larger, the program ENDS. It NEVER CHECK number3. number3 could be a million, wouldn't matter, because the original code STOPS CHECKING if number2 is larger than number1.
Hello fwheel777,

After all that has been said sometimes it just comes down to what order you do something in.

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
  #include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{

/* Sample Output:
 input three different integers: 5 10 15
 
 */
    int number1;
    int number2;
    int number3;
    //int smallest;  // <--- Not used yet. Produces a warning.
    int largest;
    
    
    cout << "enter 5: ";
    cin >> number1;

    cout << "enter value 10: ";
    cin >> number2;


    cout << "enter value 15: ";
    cin >> number3;

    
    largest = number1;
    //Write a statement to determine if number2 is greater than largest
    
    if (number3 > largest)
    {
            largest = number3;
            cout << "the number 3 value is the largest with a value of " << number3 << endl;
    }
    
    else if (number2 > largest)
    {
        //number2 = largest;
        largest = number2;
        cout << "the number 2 value is the largest with a value of " << number2 << endl;

    }
    else
    {
        cout << "the number 1 value is the largest with a value of " << number1 << endl;
    }
    
    return 0;
}

You can use the gear icon to the right of the code block to check it out. It is now in the middle of the block.

Andy
Furry Guy,
Thank you, so much for the container code, it's been over ten years since i picked up c++ so i'm having to relearn how to code and this definitely helps.

Repeater,
So basically i'm working problems from a old lab book to get up to speed. For the life of me, I can't understand why the code stop checking at "number2" when the next line of code is a else if statement...go figure.
thanks for the assist

F
Instead of using a regular array I might use a std::vector so the number of values to be compared is not restricted to a certain number at compile time.

it's been over ten years since i picked up c++

C++ underwent a HUGE change and improvement with C++11. Having to relearn with C++11/14/17 makes it close to be a totally different programming language.

Maybe plowing through an online tutorial might be of benefit. There is one here at CPlusPlus, though it is a bit outdated. C++11 for the most part.

Another tutorial that is more up-to-date is Learn C++:
https://www.learncpp.com/
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
#include <Windows.h>
#include <iostream>
#include <vector>
using namespace std;

int find_largest(int n1, int n2, int n3, int &count) {

    if (n1 > n2 && n1 > n3) { count = 1; return n1; }
    else if (n2 > n1 && n2 > n3) { count = 2; return n2; }
    else { count = 3; return n3; }
}

int main()
{

    /* Sample Output:
     input three different integers: 5 10 15

     */
    int number1;
    int number2;
    int number3;
    int smallest;
    int largest;
    int count;


    cout << "enter 5" << endl;
    cin >> number1;

    cout << "enter value 10" << endl;
    cin >> number2;


    cout << "enter value 15" << endl;
    cin >> number3;

    largest = find_largest(number1,number2,number3,count);
   
        cout << "the number "<< count << " value is the largest with a value of " << largest << endl;
    return 0;
}
I can't understand why the code stop checking at "number2" when the next line of code is a else if statement

Because that's what else means! An else block (or an else if block) will only evaluate if the previous if conditions aren't true. If number2 > largest then the block from lines 36 - 41 will be executed, and lines 44 - 53 will not be executed.

If number2 > largest is not true, only then will lines 44 - 53 be executed.

That's the whole point of using else.
...go figure.


As MikeyBoy says, nothing to figure. You simply didn't understand what else means.
This is what you are trying to do...

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
57
58
59
60
  #include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{

/* Sample Output:
 input three different integers: 5 10 15
 
 */
    int number1;
    int number2;
    int number3;
    int smallest;
    int largest;
    
    
    
    cout << "enter 5" << endl;
    cin >> number1;

    cout << "enter value 10" << endl;
    cin >> number2;


    cout << "enter value 15" << endl;
    cin >> number3;

    
    largest = number1;
    //Write a statement to determine if number2 is greater than largest
    
    if (number2 > largest && number2 > number3)
    {
        //number2 = largest;
        largest = number2;
        cout << "the number 2 value is the largest with a value of " << number2 << endl;

    }


        //Write a statement to determine if number3 is greater than largest
    else    if (number3 > largest && number3 > number2) {
            largest = number3;
            cout << "the number 3 value is the largest with a value of " << number3 << endl;
        }

        else {

            cout << "the number 1 value is the largest with a value of " << number1 << endl;
        }

    
   
      

    
    return 0;


But watch out if number2 is equal to number3 the code fails...
Last edited on
Topic archived. No new replies allowed.