A little help
Nov 13, 2011 at 7:06pm UTC
So this is the beginning of a homework assignment using structures. as of right now, it does function. But when I repeat a Do loop, it requires an addition enter key to be pressed to start the loop. I'm not sure whats causing this, any help is appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#include <iostream>
#include <new>
#include <cstring>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;
void clearbuf();
int allnums(char []);
struct Employee
{
char cid[5],
cname[20];
float frate;
float fhours;
};
int main()
{
char ctemp[20];
Employee info;
do
{
cout << "ID: " ;
cin.getline(info.cid, 5);
clearbuf();
}while (allnums(info.cid));
cout << "Name: " ;
cin.getline(info.cname, 20);
do
{
cout << "Hourly Rate: " ;
cin.getline(ctemp, 20);
clearbuf();
info.frate = float (atof(ctemp));
}while (allnums(ctemp));
do
{
cout << "Hours Worked: " ;
cin.getline(ctemp, 20);
clearbuf();
info.fhours = float (atof(ctemp));
}while (allnums(ctemp));
cout << "ID: " << info.cid << endl
<< "Name: " << info.cname << endl
<< "Rate: " << info.frate << endl
<< "Hours Worked: " << info.fhours << endl;
return 0;
}
void clearbuf()
{
if (!cin)
cin.clear();
while (cin.peek() != '\n' )
cin.ignore();
cin.ignore();
return ;
}
int allnums(char temp[])
{
int i=0,j=0,d=0,r=0;
for (i; i < strlen(temp);i++)
{
if (isdigit(temp[i]))
j++;
if (temp[i] == '.' )
d++;
}
if (j+d == i)
r=0;
else
r=1;
if (!strlen(temp) || j==0)
r=1;
return r;
}
Nov 13, 2011 at 10:33pm UTC
Your program works fine without calling clearbuf()
(and eliminating at the same time the ENTER issue).
Why do you want to use it anyway?
Topic archived. No new replies allowed.