EDN Admin
Well-known member
Is there an easier way to check an array for numeric input? What I have below works fine, but it looks messy.
<pre lang="x-cpp //Michael Markgraf
//4/12/11 C++
//Assignment 15 Structure
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
void clearbuf();
//declares datatype as gloabal datatype
struct Student //create a data type Student
{
char cnum [5];
char cname [20];
float frate;
float fhours;
};
int main ()
{
int i, inum; //i increments loop inum for dynamic space allocation
char ctemp[6]; //temp array for hours and rate
float ftemp; //temp variable for gross
do
{
cout << "How many records do you want to enter: ";
cin.getline(ctemp,2);
clearbuf();
if (!isdigit(ctemp[0]))
cout << endl << "ERROR - Input must be numeric!" << endl << endl;
}while (!isdigit(ctemp[0]));
inum = int(atoi(ctemp));
Student *stu = new Student[inum]; //student structure dynamically allocated
for (i=0; i<inum; i++)
{
do
{
cout << "Enter the employee ID number: "; //input emp num
cin.getline(stu.cnum, 5);
clearbuf();
//This is the code im refering to
if (!isdigit(stu.cnum[0]) || !isdigit(stu.cnum[1]) || !isdigit(stu.cnum[2]) || !isdigit(stu.cnum[3]))
cout << endl << "ERROR - Input must be numeric!" << endl << endl;
}while (!isdigit(stu.cnum[0]) || !isdigit(stu.cnum[1]) || !isdigit(stu.cnum[2]) || !isdigit(stu.cnum[3]));
//The code between these comments
cout << "Enter the employee name: "; //input emp name
cin.getline(stu.cname, 20);
clearbuf();
cout << "Enter the employee rate: "; //input emp pay rate
cin.getline(ctemp, 6);
clearbuf();
stu.frate = float(atof(ctemp)); //converts crate string data to float
cout << "Enter the employee hours: "; //input emp hours
cin.getline(ctemp, 6);
clearbuf();
stu.fhours = float(atof(ctemp)); //converts chours string data to float
cout << endl;
}
cout << " ID Name Rate Hours Gross Pay"<< endl;
cout << "************************************************************" << endl;
ftemp = 0; //assign value to variable
for (i=0; i<inum; i++)
{
//add gross to temp variable
ftemp = ftemp +(stu.frate* stu.fhours);
//loop output for each entry
cout << left << setw(6) << stu.cnum << setw(21) << stu.cname
<< setprecision(2) << fixed << right << setw(8) << stu.frate << right << setw(10)
<< stu.fhours << right << setw(15) << stu.frate* stu.fhours<< endl;
cout << setw(0) << endl;
}
//output total gross of all entries
cout <<"Total Gross Pay------------------------------ " << right << setw(14) << ftemp << endl;
delete [] stu; //release the space allocated with the "new" keyword
cout << endl << "Any Key...." << endl;
_getch ();
return 0;
}
void clearbuf()
{
if (!cin)
{
cin.clear(); //clear the error flag
cin.ignore(1000, n); //clear the input stream
cout << " too much data - excess ignored" << endl;
}
return;
}[/code]
<br/>
View the full article
<pre lang="x-cpp //Michael Markgraf
//4/12/11 C++
//Assignment 15 Structure
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
void clearbuf();
//declares datatype as gloabal datatype
struct Student //create a data type Student
{
char cnum [5];
char cname [20];
float frate;
float fhours;
};
int main ()
{
int i, inum; //i increments loop inum for dynamic space allocation
char ctemp[6]; //temp array for hours and rate
float ftemp; //temp variable for gross
do
{
cout << "How many records do you want to enter: ";
cin.getline(ctemp,2);
clearbuf();
if (!isdigit(ctemp[0]))
cout << endl << "ERROR - Input must be numeric!" << endl << endl;
}while (!isdigit(ctemp[0]));
inum = int(atoi(ctemp));
Student *stu = new Student[inum]; //student structure dynamically allocated
for (i=0; i<inum; i++)
{
do
{
cout << "Enter the employee ID number: "; //input emp num
cin.getline(stu.cnum, 5);
clearbuf();
//This is the code im refering to
if (!isdigit(stu.cnum[0]) || !isdigit(stu.cnum[1]) || !isdigit(stu.cnum[2]) || !isdigit(stu.cnum[3]))
cout << endl << "ERROR - Input must be numeric!" << endl << endl;
}while (!isdigit(stu.cnum[0]) || !isdigit(stu.cnum[1]) || !isdigit(stu.cnum[2]) || !isdigit(stu.cnum[3]));
//The code between these comments
cout << "Enter the employee name: "; //input emp name
cin.getline(stu.cname, 20);
clearbuf();
cout << "Enter the employee rate: "; //input emp pay rate
cin.getline(ctemp, 6);
clearbuf();
stu.frate = float(atof(ctemp)); //converts crate string data to float
cout << "Enter the employee hours: "; //input emp hours
cin.getline(ctemp, 6);
clearbuf();
stu.fhours = float(atof(ctemp)); //converts chours string data to float
cout << endl;
}
cout << " ID Name Rate Hours Gross Pay"<< endl;
cout << "************************************************************" << endl;
ftemp = 0; //assign value to variable
for (i=0; i<inum; i++)
{
//add gross to temp variable
ftemp = ftemp +(stu.frate* stu.fhours);
//loop output for each entry
cout << left << setw(6) << stu.cnum << setw(21) << stu.cname
<< setprecision(2) << fixed << right << setw(8) << stu.frate << right << setw(10)
<< stu.fhours << right << setw(15) << stu.frate* stu.fhours<< endl;
cout << setw(0) << endl;
}
//output total gross of all entries
cout <<"Total Gross Pay------------------------------ " << right << setw(14) << ftemp << endl;
delete [] stu; //release the space allocated with the "new" keyword
cout << endl << "Any Key...." << endl;
_getch ();
return 0;
}
void clearbuf()
{
if (!cin)
{
cin.clear(); //clear the error flag
cin.ignore(1000, n); //clear the input stream
cout << " too much data - excess ignored" << endl;
}
return;
}[/code]
<br/>
View the full article