looping a contact number

...can somebody help me solve this problem..???
if the user has no input..error message should appear(should not be empty/you must input contact number)...but my code is not working & i dont know what to do...please help..me...

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<ctype.h>
#include<string.h>

void main()
{
clrscr();
char con[11];
int i;
info:
do{
printf("\nContact number:");
fflush(stdin);
scanf("%s",con);
for(i=0; i<strlen(con);i++)
{
if (isalpha(con[i]))
{
printf("Must be numeric input");
break;
}}
}while(isalpha(con[i])!=0);
i=strlen(con);
if(i!=11)
{ printf("Contact number must contain 11 integer values.\nRe-enter contact number with 11 digits.");
delay(750);
goto info;
}
if(i==NULL)
{
printf("you must input contact number.\nRe-enter contact number");
goto con;
}
}
getch();
}
I'm not sure, but I don't think your null test is getting called often enough. You probably want it at the start of the processing of your char array, not at the end.

You could probably simplify your code by using cin and cout, and strings instead of character arrays, too.

EDIT:

I took a closer look at your program. DOS...really? I can't help you much with this one...conio.h doesn't exist much anymore, and I can't replace your getch() without extensively modifying your code. You also want to get out of the habit of declaring main() as anything other than int, if you want your code to be portable.

If you're willing to modernize your program a bit, then I can help with some of it.
Last edited on
...but if i use cin and cout...my teacher will question me..coz she didn't teach us whats the use of cin & cout....and i dunno how to use it...
OK...did your teacher specifically ask you to use getch()? If so, is she furnishing you with your development environment (meaning your system, compiler, etc.)?

Getch() is an old feature of C and isn't commonly found in C++ implementations, but this might not be too important for your class. So, ignore my comment about it, and let's focus on the rest of your program.

1. your line goto con; needs to be fixed. con is a variable; you can't goto them. (As an aside, is your teacher OK with you using goto statements? That is really antiquated coding, and rarely recommended.)

2. your line

} while(isalpha(con[i])!=0);

compiles, but I don't like it. You're comparing a char to an int. I'd much prefer:

} while(isalpha(con[i])!='\0');

3. your line:

if(i == NULL)

doesn't make sense to me, and generates a compiler warning. If you're trying to see whether i is 0, just test for it that way.

if(i == 0)

4. you don't clear your input buffer between input attempts. Anything read in before will still be in there if not overwritten by the next read. Similarly, you need to reset i to 0 within your do-while loop.

5. finally, when you get a valid input, you don't do anything with it. Is that what the assignment calls for?

Let's get these fixed. Post an update to your program (please use code blocks when doing so) and let's see where we go from there.
Topic archived. No new replies allowed.