user-0 提问: 新手提问关于控制程序重新执行

回复:
user: user-0
content: 5楼的那个还是不行

user: user-1
content: 仅供参考#include <stdio.h> char s[]="123 ab 4"; char *p; int v,n,k; void main() {     p=s;     while (1) {         k=sscanf(p,"%d%n",&v,&n);         printf("k,v,n=%d,%d,%d\n",k,v,n);         if (1==k) {             p+=n;         } else if (0==k) {             printf("skip char[%c]\n",p[0]);             p++;         } else {//EOF==k             break;         }     }     printf("End.\n"); } //k,v,n=1,123,3 //k,v,n=0,123,3 //skip char[ ] //k,v,n=0,123,3 //skip char[a] //k,v,n=0,123,3 //skip char[b] //k,v,n=1,4,2 //k,v,n=-1,4,2 //End.  #include <afxdisp.h> #include <stdio.h> COleDateTime t; int main(int argc, char **argv) {     if (argc<2) {         printf("Usage:%s \"DateTimeStr\"\n",argv[0]);         return 2;     }     if (t.ParseDateTime(argv[1])) {         printf("\"%s\" is a valid DateTimeStr.\n",argv[1]);         printf("%s\n",t.Format("%Y-%m-%d %H:%M:%S"));         return 0;     } else {         printf("\"%s\" is a INVALID DateTimeStr!\n",argv[1]);         return 1;     } }  //以任意字符间隔输入两个整数 #include <stdio.h> int main() {     int a,b,n,v,r,k;     char buf[80];     char *p;      k=0;     r=EOF;     while (1) {         if (EOF==r) {             fgets(buf,80,stdin);             p=buf;         }         while (1) {             r=sscanf(p,"%d%n",&v,&n);             if (1==r) {k++;break;}             if (EOF==r) break;             p++;         }         if (EOF==r) continue;         p+=n;         if (1==k) a=v;         if (2==k) {             b=v;             break;         }     }     printf("%d,%d\n",a,b);     return 0; }

user: user-0
content: 而且只要输入的月份年份不对，也自动退出程序了

user: user-0
content: 谢谢啊，可是我按您的代码试了 输出后 不能重新执行程序，虽然需要q 才退出 请教》？

user: user-2
content: #include<stdio.h> #include<stdlib.h> #include<string.h>  int main(void) { 	 	int a, iY, iM, iD; 	char s[50]; 	char sY[5]; 	char sM[3]; 	char sD[3]; 	FILE *fpt; 	char szText[50];   	printf("%s\n", "请输入年月日（按格式20140101）"); 	 	memset(sY,0,5); 	memset(sM,0,3); 	memset(sD,0,3); 	memset(s,0,50);	 	 	while(1) { 		scanf("%s", s);  		if(strcmp(s , "q") == 0) break; 		a = strlen(s); 		if(a != 8) { 			printf("%s\n", "Error!输入位数错误"); 			return 1; 		} 		 		strncpy(sY, s, 4); 		iY = atoi(sY); 		strncpy(sM, &(s[4]), 2); 		iM = atoi(sM); 		strncpy(sD, &s[6], 2); 		iD = atoi(sD); 		 		if(iY<1 || iY>9999) { 			printf("%s\n", "Error!请检查年份输入"); 			return 1; 		}       		if(iM>12 || iM<1) { 			printf("%s\n", "Error!请检查月份输入"); 			return 1; 		}  		if(iD>31 || iD<1) { 			printf("%s\n", "Error!请检查日输入"); 			return 1; 		} else {   			printf("%d%s\n", iM, "月"); 			 			sprintf(szText, "%d月\n", iM); 			printf(szText); 			 			fpt = fopen("1.txt", "w"); 			fprintf(fpt, sM); 			fclose(fpt); 		} 	} 	return 0; }

user: user-2
content: #include<stdio.h> #include<stdlib.h> #include<string.h>  int main(void) { 	 	int a, iY, iM, iD; 	char s[50]; 	char sY[5]; 	char sM[3]; 	char sD[3]; 	FILE *fpt; 	char szText[50];   	printf("%s\n", "请输入年月日（按格式20140101）"); 	 	memset(sY,0,5); 	memset(sM,0,3); 	memset(sD,0,3); 	memset(s,0,50);	 	 	while(1) { 		scanf("%s", s);  		if(strcmp(s , "q") == 0) break; 		a = strlen(s); 		if(a != 8) { 			printf("%s\n", "Error!输入位数错误"); 			return 1; 		} 		 		strncpy(sY, s, 4); 		iY = atoi(sY); 		strncpy(sM, &(s[4]), 2); 		iM = atoi(sM); 		strncpy(sD, &s[6], 2); 		iD = atoi(sD); 		 		if(iY<1 || iY>9999) { 			printf("%s\n", "Error!请检查年份输入"); 			return 1; 		}       		if(iM>12 || iM<1) { 			printf("%s\n", "Error!请检查月份输入"); 			return 1; 		}  		if(iD>31 || iD<1) { 			printf("%s\n", "Error!请检查日输入"); 			return 1; 		} else {   			printf("%d%s\n", iM, "月"); 			 			sprintf(szText, "%d月\n", iM); 			printf(szText); 			 			fpt = fopen("1.txt", "w"); 			fprintf(fpt, sM); 			fclose(fpt); 		} 	} 	return 0; }

user: user-3
content: if(s[0]=='q')

user: user-4
content: int main(void) {    printf("%s\n", "请输入年月日（按格式20140101）");     int a,iY,iM,iD;   char s[50];   char sY[5];   char sM[3];   char sD[3];  // s[8]=0;  //s[8]='\0'      memset(sY,0,5);   memset(sM,0,3);   memset(sD,0,3);   memset(s,0,50);       FILE *fpt;   while(1) {   scanf("%s",s);    if (s==q)break;   a= strlen(s);   if (a!=8) { printf("%s\n", "Error!输入位数错误"); return 1; }    strncpy(sY,s,4);   iY=atoi(sY);   strncpy(sM,&(s[4]),2);   iM=atoi(sM);   strncpy(sD,&s[6],2);   iD=atoi(sD);      if(iY<1||iY>9999) {  printf("%s\n", "Error!请检查年份输入");  return 1; }           if(iM>12||iM<1) {  printf("%s\n", "Error!请检查月份输入");  return 1; }      if(iD>31||iD<1) {  printf("%s\n", "Error!请检查日输入");  return 1; }     else  {   printf("%d%s\n",iM,"月");   //printf("%d月\n",iM);  char szText[50];    sprintf(szText,"%d月\n",iM);   printf(szText);       fpt = fopen("1.txt","w");   fprintf(fpt,sM);   fclose(fpt);   return 0;  } } }

user: user-0
content: 求助啊，大哥大姐