Mail Archives: djgpp/1998/04/27/16:08:01
>>In the following program, I have to print the largest integer using the
>>if statement. It works as it is using " else " but I was wondering why I
>>can't get it to work without the" else ".As I understand it, if I leave
>>out the else the program should skip " answer = int1 ".It doesn't , that
>>is why I had to add " else ".If you can help please email at
>>
>>krusty AT iaccess DOT com DOT au
>>
>>Thanks in advance
>>Brad.
>>
>>#include <stdio.h>
>>
>>void main(void)
>>
>>{
>>int int1,int2,answer;
>>
>>printf("Please enter 2 integers.\n");
>>
>>scanf("%i %i",&int1 ,&int2);
>>
>> if (int1 > int2)
>> answer = int1;
>> else
>>
>> answer = int2;
>>
>> printf(" The larger integer is %i \n"),answer;
>>
>>
>>}
Surely in C++, it is better to forget the if/then/else construct altogether:
void main(void)
{
int int1,int2,answer;
printf("Please enter 2 integers.\n");
scanf("%i %i",&int1 ,&int2);
answer = ( (int1 > int2) ? int1 : int2 );
printf(" The larger integer is %d\n",answer);
}
This is the same thing but in true C++ style. There's also a similar way of
doing printf and scanf but I can't remember it offhand :^)
Note: I've re-written the printf statement so that it works.
James Arthur
jaa AT arfa DOT clara DOT net
- Raw text -