delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/27/16:08:01

Message-ID: <006501bd7217$2ac352e0$864e08c3@arthur>
From: "Arthur" <arfa AT clara DOT net>
To: <djgpp AT delorie DOT com>
Subject: Re: Programming Prob.
Date: Mon, 27 Apr 1998 21:00:20 +0100
MIME-Version: 1.0

>>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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019