Answer to Question #9199 in C++ for Wayne Leiser

Question #9199
I attempted to submit this to the C category and the website is broken and I could not submit it in 4 different browsers so I had to submit this under C++. This is a C question. Issue with isdigit. Supposed to look at the first digit and return 0 if non digit entered. It always returns a 0 no matter what. I have attempted changing count to a float. Why isn't this working?

Win7 64
Dev C++

#include <conio.h>
#include <iostream>
#include <ctype.h>

using namespace std;

int main(int argc, char *argv[])
{

int loopCounter=0,count=0,a;

for(a=4;a>0;a--) {
printf("Please enter a number 1 - 255: ");
scanf("%d",&count);
if(isdigit(count)) {
if(count<=0)
a=2;
else if(count>0 && count<256)
a=0;
else
return 1;
}
else
return 1;
}
while(count>=loopCounter) {
printf("%d\n",count);
count--;
}
getch();
return EXIT_SUCCESS;
}
1
Expert's answer
2012-11-12T06:55:09-0500
#include <conio.h>
#include <iostream>
#include <ctype.h>
using namespace std;
&
int main(int argc, char *argv[])
{
int loopCounter=0,count=0,a;
for(a=2;a>0;a--) {
printf("Please enter a number 1 - 255: ");
if (scanf("%d*c",&count)) {
& if(count<=0) {
& printf("%d is not a number between 1 - 255. ",count);
& a=2;
& }
& else if(count>=256)
a=2;
& else
a=0; &
}
else {
& printf("A valid number was not entered.& The program will now close.");
& count=-1;
& a=0;
}
}
while(count>=loopCounter) {
printf("%d\n",count);
count--;
}
getch();
return EXIT_SUCCESS;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS