Number input and formatted output

This is a discussion on Number input and formatted output within the C++ Programming forums, part of the General Programming Boards category; Hello First off, this is not homework or anything. It is just an exercise out of "C++ Primer Plus." Ok, ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    Number input and formatted output

    Hello

    First off, this is not homework or anything. It is just an exercise out of "C++ Primer Plus."

    Ok, my main question is about the parts in bold. Is that the correct way to accept input from users? Like, is that the proper way to check to make sure a number has been entered and to get rid of the '\n'?

    My other question is about formatted output. I know in C with printf(), you could enter something like printf("%10d",num); to have the number print out in a specific place. Without using printf, how can I do this in C++?

    If anything jumps out at you as wrong, I would appreciate any info on that as well

    Thanks for the help.

    Code:
    //*******************************************************************
    //
    // MonetaryContributions.cpp - Keeps track of contributions
    //
    // Date: 12/19/2004	
    // Author: Sean (bachelde@usc.edu)
    //
    //*******************************************************************
    
    #include <iostream>
    
    struct Contributors {
    	char szName[40];
    	float fAmount;
    };
    
    int main(void)
    {
    	Contributors *People;
    	int nNumPeople;
    	char szBad[30];
    
    	std::cout << "Enter the number of contributors: ";
    	while ( (std::cin >> nNumPeople) == false) // make sure a number is entered
    	{
    		std::cin.clear();
    		std::cin >> szBad;
    		std::cout << "Please enter a number: ";
    	}
    	std::cin.get(szBad[0]); // shave off '\n'
    
    
    	People = new Contributors[nNumPeople];
    
    
    	// get info about contributors
    	for (int i = 0; i < nNumPeople; i++)
    	{
    		std::cout << "Enter a name: ";
    		std::cin.getline(People[i].szName, sizeof(People[i].szName));
    
    		std::cout << "Enter the amount they donated: ";
    		while ( (std::cin >> People[i].fAmount) == false) // make sure a number is entered
    		{
    			std::cin.clear();
    			std::cin >> szBad;
    			std::cout << "Please enter a number: ";
    		}
    		std::cin.get(szBad[0]); // shave off '\n'
    	}
    
    
    	// print out info about donors
    	// first "Grand Patrons" >= 10000
    	std::cout << "\n\nGrand Patrons\n";
    	std::cout << "==========================\n\n";
    	for (i = 0; i < nNumPeople; i++)
    		if (People[i].fAmount >= 10000.0)
    			std::cout << People[i].szName << "\t" << People[i].fAmount << "\n";
    
    	// next "Patrons" < 10000
    	std::cout << "\n\nPatrons\n";
    	std::cout << "==========================\n\n";
    	for (i = 0; i < nNumPeople; i++)
    		if (People[i].fAmount < 10000.0)
    			std::cout << People[i].szName << "\t" << People[i].fAmount << "\n";
    
    
    	free(People);
    	return 0;
    }

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think
    Code:
    while ( (std::cin >> nNumPeople) == false)
    can just be written:
    Code:
    while (std::cin >> nNumPeople)
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Quote Originally Posted by The Brain
    I think
    Code:
    while ( (std::cin >> nNumPeople) == false)
    can just be written:
    Code:
    while (std::cin >> nNumPeople)
    while ( !(std:cin >> nNumPeople)) makes more sense..

    thanks for the tip

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > Without using printf, how can I do this in C++?
    There are a whole bunch of IO manipulators, which are in
    Code:
    #include <iomanip>
    An example use would be
    Code:
        std::cout << std::setw(10) << People[i].fAmount << std::endl;
    You should use the endl in place of "\n" to guarantee that the line will be flushed to the output stream.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Ah, very good. Thanks for the help Salem and The Brain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Never find EOF after improperly formatted input
    By MALDATA in forum C Programming
    Replies: 5
    Last Post: 09-30-2008, 01:24 PM
  2. Two Dimensional Array Input from Formatted Data
    By teedoff087 in forum C Programming
    Replies: 14
    Last Post: 04-29-2007, 01:46 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. Formatted input
    By vaibhav in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2005, 11:19 PM
  5. formatted file input
    By Flikm in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21