delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/28/09:43:18

From: d DOT vrabel AT n-shropshire DOT ac DOT uk (David Vrabel)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Programming Prob.
Date: Tue, 28 Apr 1998 06:47:51 GMT
Organization: Staffordshire University
Lines: 44
Message-ID: <354579e1.383980@192.168.1.250>
References: <006501bd7217$2ac352e0$864e08c3 AT arthur>
NNTP-Posting-Host: www.n-shropshire.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

"Arthur" <arfa AT clara DOT net> wrote:
>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 actually C and not specific to C++.
Also main() should return an int.

In C++ you could write a templated inlined function:

#include<iostream>

inline template<class T>
T max( T a, T b ) {
    return (a>b)?a:b;
}

int main( void ) {

	int a,b;

	cout << "Enter a: ";
	cin >> a;
	cout << "Enter b:";
	cin >> b;

	cout << "Largest is: " << max( a, b ) << endl;

	return 0;
}

David Vrabel  d DOT vrabel AT n-shropshire DOT ac DOT uk
A poor student (aren't they all) at:
North Shropshire College
Oswestry, Shropshire, UK

- Raw text -


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