# Thread: Help with isdigit and for loops

1. ## Help with isdigit and for loops

I am attempting to check whether a number entered is a digit. I have devised that the way to do this is to create a character array and save the user input there. Then I open/create a text file. USing a for loop I check that they are all digits. If they are my main program should continue on the return of the value 1 in "digit".

It took me ages to get the open/write/read file to work and now my program does the loop but makes me input too many numbers and wont move on. The user must be able to input a figure of up to 3 digits... If someone could help I would be extremely grateful... I may even send flowers....

Code:
```int coincheck(void)

{
ofstream outfile("c:\data.txt",ios::out); //creates outfile
if(!outfile)
{
cerr<<"error:cant open\n";
exit(1);
}
cin>>coin;
cout<<"\n!!! - "<<coin;
do{
for (i=0;(c=getchar()) != '\n';++i)
{
coin[i] = c;
if(isdigit(c))
{
cout<<"gifgvhcfj";
outfile<<c;
digit=1;
}
else
{
cout<<"must be a number/n";
digit=0;
exit(1);
}
}
outfile.close();
}while (coin !=0);
ifstream infile("c\data.txt",ios::in); //opens file
if(!outfile)
{
cerr<<"error:cant open\n";
exit(1);
}
infile>>newcoin;
infile.close();
return (0);
exit(1);
}```

2. Code:
`while (coin !=0);`
When will that ever happen?

3. Sorry... What I should have told you is that this is a function in a much bigger program that I am writing. The user has to enter on of 5 coin choices 5, 10, 20, 50, 100. To terminate this loop they are to simply enter 0. I am okay checking the values in my main with an if statement using a lot of OR's but decided to write a function to check if it was a digit to stop people inputting letters. Another problem I have is with my for loop... it goes on for too long if they just want to enter the number 5

4. Yeah, but coin is a pointer to a string. Why would it ever be 0?

5. OKAY!!! So how do I say "While the user has not entered the value '0' "? Coin was an int before I changed it to a pointer so I thought it would work... I thought wrong

6. Off the top of my head:
Code:
`while(std::cin.get() != '0')`

7. > ofstream outfile("c:\data.txt",ios:ut);
Don't forget, to get a single \, you must write \\

> ifstream infile("c\data.txt",ios::in);
Missing : as well.