Click here to Skip to main content
13,627,709 members
Click here to Skip to main content
Add your own
alternative version

Stats

6.6K views
110 downloads
2 bookmarked
Posted 25 Jul 2016
Licenced CPOL

Singular vs. Plural in Item Counts

, 25 Jul 2016
Rate this:
Please Sign up or sign in to vote.
A simple way to display "item" or "items" instead of "item(s)"

   

Introduction

To allow for an item-count display to handle a number that may or may not be one, this display usually includes something like item(s), file(s), folder(s), document(s), second(s), minute(s), hour(s), or day(s). In some programs, the parentheses around the "s" are left out. In those cases, there is an apparent assumption that the quantity will rarely be one. It would be better to display an "s" only when needed, without parentheses.

The following solution only works for English text. There are various articles on CodeProject that address this problem for other languages.

Solution

To make it easy to display an "s" only when needed, I wrote the following macro:

#define SINGULAR(n) (n == 1 ? _T("") : _T("s"))

If you only want to display Unicode strings, replace _T("...") with L"...".

To use it, put this macro into an include file that is included by source files that need it. Then, write code such as the following:

CString strMsg;
strMsg.Format (_T("%d item%s"), n, SINGULAR(n));

Then display strMsg. For words such as "index" whose plural form ends in "es", use the following macro:

#define SINGULAR_ES(n) (n == 1 ? _T("") : _T("es"))

For words such as "man" whose plural form does not fit either of these patterns, use an inline conditional:

CString strMsg;
strMsg.Format (_T("%d %s"), n, (n==1 ? _T("man") : _T("men")));

Sample Program

The program Singular demonstrates these three circumstances for selecting between singular and plural forms of words.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

David Wincelberg
United States United States
While working as an engineer, I occasionally applied my problem-solving skills to software problems. For example, I noticed that numbers in increasing filenames did not always increase. I tinkered with solutions and subsequently wrote an article that was published in Dr. Dobb’s Journal.

After that job fizzled, I switched to programming full time and have applied my programming skills to multimedia education, web-based games of skill, legal-compliance and ethics-training courses, and shareware programs.

You may also be interested in...

Comments and Discussions

 
QuestionIndex Pin
Member 1252142127-Jul-16 0:13
memberMember 1252142127-Jul-16 0:13 
AnswerRe: Index Pin
David Wincelberg27-Jul-16 7:38
memberDavid Wincelberg27-Jul-16 7:38 
AnswerRe: Index Pin
David Wincelberg8-Mar-17 10:35
memberDavid Wincelberg8-Mar-17 10:35 
QuestionPictures Pin
PeejayAdams26-Jul-16 2:15
memberPeejayAdams26-Jul-16 2:15 
AnswerRe: Pictures Pin
David Wincelberg26-Jul-16 8:48
memberDavid Wincelberg26-Jul-16 8:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180712.1 | Last Updated 25 Jul 2016
Article Copyright 2016 by David Wincelberg
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid