Validating that there are no integers

I am working on a program where the person enters their name and their gross pay. I had to validate that the gross was a number and contained no letters.

I also have to validate the persons name to see if it contains any numbers. The book I am using has an example with only an if statement that says if it is correct or incorrect. I was trying to use <cctype> and toalpha

I just was wondering if there was a way to put it into a while loop or how i would have it ask the user to input their name again. When i tried it just blew up.

Also in the book they use const int SIZE= 8, but I don't want to put a size on the name if I don't have to. If anyone can explain it a little better that would be awesome.
What does your code look like so far? Post it here and we can help you spot why "it just blew up."

const int SIZE = 8

Sounds like a C-string. If you are making a C++ program(since you say you used cctype), you have the option to use the std::string class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main(int argc, char* argv[]){
   string name;
   cout << "Hi! Please type in your name and press ENTER: ";
   cin >> name;
   cout << "\nYou entered: " << name << endl;
   return 0;
}
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <fstream>
using namespace std;

ofstream outputFile;


#define FEDERAL .15
#define STATE .035
#define SOCIAL .085
#define HEALTH 75.00
#define SIZE 5

double gross, //Input for gross pay
fedtax, //cost of fed tax
sttax,// cost of state taxes
soc, //cost of social security/medicare
net; //net pay after taxes

char answer= 'Y';

string name;
string filename;
void newgross();

int main()
{
cout << "Payroll calculator for net pay";
cout<<"\nEnter a filename (include .txt): ";
cin>> filename;
outputFile.open(filename.c_str());
while (toupper(answer) == 'Y')
{




cout << "\n\nPlease enter employee name: ";

cin >> name;

newgross();





cout << "\nEmployee's name :" << name << endl;
cout << "\nYour gross amount is :" << fixed << setprecision(2) << gross << endl;
cout << "\nYour Federal tax is :" << fixed << setprecision(2) << fedtax << endl;
cout << "\nYour State tax is :" << fixed << setprecision(2) << sttax << endl;
cout << "\nYour Social security is :" << fixed << setprecision(2) << soc << endl;
cout << "\nYour Health insurance :" << fixed << setprecision(2) << HEALTH << endl;
cout << "\nYour net pay is :" << fixed << setprecision(2) << net << endl;

outputFile<< "\nEmployee's name :" << name << endl;
outputFile<< "\nYour gross amount is :" << fixed << setprecision(2) << gross << endl;
outputFile<<"\nYour Federal tax is :" << fixed << setprecision(2) << fedtax << endl;
outputFile<<"\nYour State tax is :" << fixed << setprecision(2) << sttax << endl;
outputFile<<"\nYour Social security is :" << fixed << setprecision(2) << soc << endl;
outputFile<< "\nYour Health insurance :" << fixed << setprecision(2) << HEALTH << endl;
outputFile<< "\nYour net pay is :" << fixed << setprecision(2) << net << endl;

cout << "Would you like to input another employee? (Y/N) " ;
cin >> answer;
}

outputFile.close();

return 0;
}

void newgross()
{
cout << "Please enter Gross Pay: " ;
cin >> gross;

while (!cin ||gross < 0)
{
cout << "Invalid entry, Please re-enter a positive number: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >>gross;
}
net = 0;
fedtax = gross*FEDERAL;
sttax = gross*STATE;
soc = gross*SOCIAL;
net = gross - fedtax - soc - net;


}


Thats my code. I think i lost the part that was the return... it was something along the lines of


bool testname(char []);

int main ()
{
const int SIZE = 8;

char name[SIZE];

cin.getline (name, SIZE);
if (testname(name , SIZE))
cout << "thats correct\n";
else
{
cout << "that is wrong";
cout << "\nplease enter valid name";

}


// The function

bool testname ( char newname[], int size)
{
int count;

for (count = 0; count < 3; count ++)
{

if (!isalpha (newname[count]))
return false;
}
return true;
}

//END

So i tried while loops because I know the if loops would only work 1 time but it would either loop endlessly OR lets say i entered 3 numbers ie 456... it would show invalid 3 times. That was the example in the book and i was not able to make it work correctly

I believe i changed the string name as well since i was using it the other way. either way no luck
Last edited on
Well, I have not experienced the infinite loop. However, I can tell you that for inputting the name, you can use getline, so the user can input their full name with spaces. cin only removes characters from the buffer up to a space or '\n,' so the rest stays in the buffer to be collected by the next cin.
Unlike the getline member function that collects a C-string, the getline non-member function collects a C++ string.
 
getline(cin, name);


You also forgot to include the <limits> header in order to use numeric_limits.

Example output:
Payroll calculator for net pay
Enter a filename (include .txt): abc.txt


Please enter employee name: riley
Please enter Gross Pay: 9991783.56

Employee's name :riley

Your gross amount is :9991783.56

Your Federal tax is :1498767.53

Your State tax is :349712.42

Your Social security is :849301.60

Your Health insurance :75.00

Your net pay is :7643714.42
Would you like to input another employee? (Y/N) y


Please enter employee name: bobby
Please enter Gross Pay: 0.77777777

Employee's name :bobby

Your gross amount is :0.78

Your Federal tax is :0.12

Your State tax is :0.03

Your Social security is :0.07

Your Health insurance :75.00

Your net pay is :0.59
Would you like to input another employee? (Y/N) y


Please enter employee name: sheeb dog
Please enter Gross Pay: Invalid entry, Please re-enter a positive number: 34

Employee's name :sheeb

Your gross amount is :34.00

Your Federal tax is :5.10

Your State tax is :1.19

Your Social security is :2.89

Your Health insurance :75.00

Your net pay is :26.01
Would you like to input another employee? (Y/N)


Have you been entering in data differently than this?
Last edited on
Thank you for the help I do appreciate it. I get those results. The problem I am having is that im supposed to make sure they dont enter a number under employee name


so if it said Employee's name: 123456

it should say "invalid entry, please enter a name:"

I will try the getline as well ty
Topic archived. No new replies allowed.