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 ([email protected])
//
//*******************************************************************
#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;
}