Question:
What's wrong with my C++ code?
Owly's watching you
2009-09-25 02:53:53 UTC
I am supposed to create a program that will input 3 numbers and then will pick the highest one..
But when I ran it the highest number displayed is always ZERO..why?

#include
#define p printf
#define s scanf
int firstnumber, secondnumber, thirdnumber, highestnumber;
main ()
{

p("Input first number ", firstnumber);
s("%d", &firstnumber);
p("Input second number ", secondnumber);
s("%d", &secondnumber);
p("Input third number ", thirdnumber);
s("%d", &thirdnumber);

if (firstnumber > secondnumber > thirdnumber) highestnumber = firstnumber;
if (secondnumber > thirdnumber > firstnumber) highestnumber = secondnumber;
if (thirdnumber > firstnumber > secondnumber) highestnumber = thirdnumber;
if (firstnumber > thirdnumber > secondnumber) highestnumber = firstnumber;
if (secondnumber > firstnumber > thirdnumber) highestnumber = secondnumber;
if (thirdnumber > secondnumber > firstnumber) highestnumber = thirdnumber;

p("The highest number is %d", highestnumber);

}
Six answers:
2009-09-25 05:32:47 UTC
Hm....



















syntax: if(a>b>c) ..., legal in C/C++.

But this code always false.

Meaning: unreachable if-statement.



Fix if-statement with right logical operation.
NA
2009-09-25 08:59:55 UTC
I try to compile:

#include

#define p printf

#define s scanf

int firstnumber, secondnumber, thirdnumber, highestnumber;

main ()

{



p("Input first number ", firstnumber);

s("%d", &firstnumber);

p("Input second number ", secondnumber);

s("%d", &secondnumber);

p("Input third number ", thirdnumber);

s("%d", &thirdnumber);

.... Until right here: compile success!

.... warning unreach syntax

.... Still success

p("The highest number is %d", highestnumber);

.... See, why highestnumber always ZERO

}
cja
2009-09-25 03:55:09 UTC
As Paul said, your syntax is incorrect. Also, your logic is more complicated than it needs to be. I think it's simpler to just arrange the numbers in ascending order, then the largest is the last one in the list. See below for how I would do this.



Other points: for good coding style, I would avoid macros to hide library function names; you should get in the habit of validating user input, as I've done below using fgets and sscanf, scanf doesn't give you as much control.



/*

  * Accept integers, 3 at a time, display the min,

  * max, and middle values, and the sorted list of

  * numbers. Input can be entered interactively,

  * or piped/redirected from a file. If interactive,

  * terminate input with Ctrl-d.

  */

#include



#define MAX_LINE_LEN 1024



typedef enum { false = 0, true } bool;



bool getInt3(int *, int *, int *);

void swap(int *, int *);

void threesort(int *, int *, int *);

int min3(int, int, int);

int max3(int, int, int);

int mid3(int, int, int);



int main(int argc, char *argv[]) {

    int a, b, c;

    bool done;



    do {

        printf("Enter 3 integers (a b c) : ");

        if ((done = getInt3(&a,&b,&c)) == false) {

            printf("\nEntered values : %d, %d, %d\n",a,b,c);

            printf("min = %d\n",min3(a,b,c));

            printf("max = %d\n",max3(a,b,c));

            printf("mid = %d\n",mid3(a,b,c));

            threesort(&a,&b,&c);

            printf("Sorted: %d %d %d\n",a, b, c);

        }

    } while (done == false);

    return 0;

}



void swap(int *a, int *b) {

    int temp = *a;

    *a = *b;

    *b = temp;

}



void threesort(int *a, int *b, int *c) {

    if (*a > *b) swap(a,b);

    if (*a > *c) swap(a,c);

    if (*b > *c) swap(b,c);

}



int min3(int a, int b, int c) {

    int x = a, y = b, z = c;

    threesort(&x,&y,&z);

    return x;

}



int max3(int a, int b, int c) {

    int x = a, y = b, z = c;

    threesort(&x,&y,&z);

    return z;

}



int mid3(int a, int b, int c) {

    int x = a, y = b, z = c;

    threesort(&x,&y,&z);

    return y;

}



bool getInt3(int *a, int *b, int *c) {

    bool inputOk, noInput = false;

    char line[MAX_LINE_LEN];



    for (inputOk = false; inputOk == false; ) {

        if (!(noInput = (fgets(line,MAX_LINE_LEN,stdin) == NULL))) {

            inputOk = (sscanf(line,"%d %d %d",a,b,c) == 3);

            if (inputOk == false) {

                printf("invalid input, try again: ");

            }

        } else {

            inputOk = true;

        }

    }

    return noInput;

}



#if 0



Sample run:



Enter 3 integers (a b c) : 4 1 2



Entered values : 4, 1, 2

min = 1

max = 4

mid = 2

Sorted: 1 2 4

Enter 3 integers (a b c) : x y z

invalid input, try again: 4 3 9



Entered values : 4, 3, 9

min = 3

max = 9

mid = 4

Sorted: 3 4 9



#endif
?
2009-09-25 03:27:22 UTC
You can't do expressions such as this in C/C++:



if (firstnumber > secondnumber > thirdnumber) highestnumber = firstnumber;



It would need to be:



if (firstnumber > secondnumber && secondnumber > thirdnumber) highestnumber = firstnumber;
Dj Frosty
2009-09-25 03:21:36 UTC
I had to do the same thing for VB.net, here's the code i used, when a user clicks a button.



Private Sub btnExecise10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecise10.Click



' Declair my varibles

Dim Number1 As Integer = 1

Dim Number2 As Integer = 2

Dim Number3 As Integer = 3

Dim NumberDisplay1 As Integer = 1

Dim NumberDisplay2 As Integer = 3

Dim NumberDisplay3 As Integer = 2



' While the 3 numbers arn't the same

While NumberDisplay1 <> NumberDisplay2 Or NumberDisplay1 <> NumberDisplay3 Or _

NumberDisplay2 <> NumberDisplay3

Try

' Clear the number to display

Number1 = InputBox("Enter Number 1")

Number2 = InputBox("Enter Number 2")

Number3 = InputBox("Enter Number 3")



' If number 1 is greater than 2 and 3 then number to display for slot 1 is 1

If Number1 >= Number2 And Number1 >= Number3 Then

NumberDisplay1 = Number1



' If number 2 is greater than number 3 number 2 is in the display 2

' And number 3 is to be displayed is in display 3

If Number2 >= Number3 Then

NumberDisplay2 = Number2

NumberDisplay3 = Number3

End If



' If number 3 is greater than number 2 number 3 is in the display 2

' And number 2 is to be displayed is in display 3

If Number3 >= Number2 Then

NumberDisplay2 = Number3

NumberDisplay3 = Number2

End If



End If



' If number 2 is greater than or equal to number 1 and

' If number 2 is greater than or equal to number 2 then

If Number2 >= Number1 And Number2 >= Number3 Then



' Display number 1 as 2

NumberDisplay1 = Number2



' If number1 is greater than number 3

If Number1 >= Number3 Then



' Then number 2 is number 1

NumberDisplay2 = Number1



' And number 3 is number 3

NumberDisplay3 = Number3

End If



' If number3 is greater than number 1

If Number3 >= Number1 Then



' Then number 2 is number 3

NumberDisplay2 = Number3



' And number 3 is number 1

NumberDisplay3 = Number1



End If

End If



' Same as above

If Number3 >= Number1 And Number3 >= Number2 Then

NumberDisplay1 = Number3

If Number1 >= Number2 Then

NumberDisplay2 = Number1

NumberDisplay3 = Number2

End If

If Number2 >= Number1 Then

NumberDisplay2 = Number2

NumberDisplay3 = Number1



End If

End If



' Display the numbers

MessageBox.Show("Top number is: " & NumberDisplay1 & vbNewLine & _

"Middle number is: " & NumberDisplay2 & vbNewLine & _

"Last number is: " & NumberDisplay3, "")



Catch ex As Exception



End Try

End While



' Now we exit because all 3 numbers are the same, display that they are

MessageBox.Show("All 3 numbers are the same!")



End Sub



Might be of some use.
Cannibal Ox
2009-09-25 03:02:55 UTC
first of all, that is 'C' code not 'C++'

second of all, you are scanning in references. that is probably not good for your health.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...