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);
}
//cout<<"please enter your coin.\n";
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);
}