Test 1 – 20113 Q2 and Q3

Posted: March 1, 2012 in Uncategorized
#include <cstdio>
#include <cstring>



//20113 Answer to question 2
void getStr(char *str, int (*IsValid)(const char*), const char* prompt){
  int done = false;
  while(!done){
    printf(prompt);
    scanf("%s", str);
    if(IsValid && IsValid(str)){
      done = 1;
      printf("Hello! yourphone number is: %s\n", str);
    }
  }
}

//20113 Answer to question 3
int ValidPhone(const char* st){
  int flag = true;
  int i;
  if (strlen(st)!=10) flag = false;
  for(i = 0; flag && st[i] != '\0' && st[i] >= '0' && st[i] <= '9'; i++);
  if (!flag && i != 10) flag = false;
  return flag;
}

int main() {
  char string[80];
  getStr(string, ValidPhone, "Please enter your phone: ");
  return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s