user-0 提问: 请教问题，函数返回不成功。

回复:
user: user-0
content: 引用 19 楼 zhao4zhong1 的回复:在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。 //请今后要用 int c; scanf("%c",&c); //时，都改为 char s[2]; int c; scanf("%1s",s); c=s[0];  感谢赵老师指教！ 另外最后这段，我个人理解的是：将输入的整数型字符 看成一个数组，其中s[0]储存整数型字符，s[1]及s[2]储存‘\n'。 不知道这样理解是否正确。

user: user-1
content: 在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。 //请今后要用 int c; scanf("%c",&c); //时，都改为 char s[2]; int c; scanf("%1s",s); c=s[0];

user: user-1
content: Format Specification Fields: scanf and wscanf Functions A format specification has the following form:  % [width] [{h | l | I64 | L}]type  The format argument specifies the interpretation of the input and can contain one or more of the following:   White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.   Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates.   Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.  The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read.  When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.  An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.  Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number.   The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.  An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.

user: user-0
content: 引用 21 楼 zhao4zhong1 的回复:scanf("%c",&c);不能自动跳过若干个空白字符（包括空格，Tab字符，换行符）， scanf("%1s",s);能跳过若干个空白字符（包括空格，Tab字符，换行符），读取1个非空白字符到s[0]中，s[1]里面保存'\0‘  感谢赵老师为我这初学者解释这么初浅的问题。争取之后不再问这种问题。

user: user-1
content: scanf("%c",&c);不能自动跳过若干个空白字符（包括空格，Tab字符，换行符）， scanf("%1s",s);能跳过若干个空白字符（包括空格，Tab字符，换行符），读取1个非空白字符到s[0]中，s[1]里面保存'\0‘

user: user-0
content: 引用 17 楼 zhao4zhong1 的回复:我承认我有点为炫而炫了。  别说对新人了，对十几年的老人都不一定看得懂啊！（借机又炫了一次）  赵老师，以及各位。 12楼的程序在vs2015中出现了这个问题。。

user: user-1
content: 我承认我有点为炫而炫了。  别说对新人了，对十几年的老人都不一定看得懂啊！（借机又炫了一次）

user: user-0
content: 赵老师，这个我还看不懂。 暂仅学到字符输入/输出与输入确认。   引用 15 楼 zhao4zhong1 的回复:偶特来炫一下C和C++挑出N重循环和函数调用的招： #include <stdio.h> #include <setjmp.h> jmp_buf mark; int i=0; void c() {     printf( "c in\n");     if (1==i%2) longjmp( mark, i );     printf( "c out\n"); } void b() {     printf( "b in\n");     c();     printf( "b out\n"); } void a() {     printf( "a in\n");     b();     printf( "a out\n"); } void main( void ) {    int jmpret;    jmpret = setjmp( mark );    printf( "Start %d\n",jmpret);    if( jmpret == 0 ) {        printf( "First\n");        a();        i++;        a();    } else {        printf( "Second\n");    } } //Start 0 //First //a in //b in //c in //c out //b out //a out //a in //b in //c in //Start 1 //Second //  #include <iostream> using namespace std; struct MyStruct {     int Nodedata;     MyStruct *pLeft;     MyStruct *pRight; } s1,s2,s3,s4,s5,s6,s7,s8; void ashow(MyStruct * p) {     if (p != nullptr) {         ashow(p->pLeft);         ashow(p->pRight);         cout << p->Nodedata << endl;         if (p->Nodedata == 4) {             throw 1;//退出递归.直接返回.         }     } } int main() {     MyStruct *pRoot;//根      pRoot = &s1;     s1.Nodedata = 1;     s2.Nodedata = 2;     s3.Nodedata = 3;     s4.Nodedata = 4;     s5.Nodedata = 5;     s6.Nodedata = 6;     s7.Nodedata = 7;     s8.Nodedata = 8;      s1.pLeft = &s2;     s1.pRight = &s3;      s2.pLeft = &s4;     s2.pRight = &s5;      s3.pLeft = &s6;     s3.pRight = &s7;     try {         ashow(pRoot);     } catch (int err) {         cout<<"catch err "<<err<<endl;     }     return 0; } //4 //catch err 1 //

user: user-1
content: 偶特来炫一下C和C++挑出N重循环和函数调用的招： #include <stdio.h> #include <setjmp.h> jmp_buf mark; int i=0; void c() {     printf( "c in\n");     if (1==i%2) longjmp( mark, i );     printf( "c out\n"); } void b() {     printf( "b in\n");     c();     printf( "b out\n"); } void a() {     printf( "a in\n");     b();     printf( "a out\n"); } void main( void ) {    int jmpret;    jmpret = setjmp( mark );    printf( "Start %d\n",jmpret);    if( jmpret == 0 ) {        printf( "First\n");        a();        i++;        a();    } else {        printf( "Second\n");    } } //Start 0 //First //a in //b in //c in //c out //b out //a out //a in //b in //c in //Start 1 //Second //  #include <iostream> using namespace std; struct MyStruct {     int Nodedata;     MyStruct *pLeft;     MyStruct *pRight; } s1,s2,s3,s4,s5,s6,s7,s8; void ashow(MyStruct * p) {     if (p != nullptr) {         ashow(p->pLeft);         ashow(p->pRight);         cout << p->Nodedata << endl;         if (p->Nodedata == 4) {             throw 1;//退出递归.直接返回.         }     } } int main() {     MyStruct *pRoot;//根      pRoot = &s1;     s1.Nodedata = 1;     s2.Nodedata = 2;     s3.Nodedata = 3;     s4.Nodedata = 4;     s5.Nodedata = 5;     s6.Nodedata = 6;     s7.Nodedata = 7;     s8.Nodedata = 8;      s1.pLeft = &s2;     s1.pRight = &s3;      s2.pLeft = &s4;     s2.pRight = &s5;      s3.pLeft = &s6;     s3.pRight = &s7;     try {         ashow(pRoot);     } catch (int err) {         cout<<"catch err "<<err<<endl;     }     return 0; } //4 //catch err 1 //

user: user-2
content: 引用 13 楼 qcx1221 的回复:唯一的缺陷： default里面的q to quit无法启动。。 当输入q的时候可以返回一个特殊值，在main函数中检测这个值，当该值被返回时，退出main函数。不过因为层级比较深，操作起来有点麻烦，视情况而定吧

user: user-0
content: 唯一的缺陷： default里面的q to quit无法启动。。

user: user-0
content: 根据版主qq423399099及ghx287524027及的指教： 特修正如下: #include <stdio.h> #include <windows.h> void meum(void); double xuanze(char ch); void jisuan(double base); int main(void) { 	double base = 0.00; 	char ch;  	meum(); 	while((ch = getchar()) != 'q'){ 		base = xuanze(ch); 		jisuan(base); 		fflush(stdin); 		meum(); 	} 	system("pause"); 	return 0; }  void meum(void) { 	printf("*************************************************************************\n"); 	printf("a)$8.75/hr                          b)$9.33/hr\n"); 	printf("c)$10.00/hr                         d)$11.20/hr\n"); 	printf("q)quit  \n"); 	printf("*************************************************************************\n"); 	printf("请根据以上选项输入您的每小时的基本工资（q to quit)： "); }  double xuanze(char ch) { 	double base; 	int flag = 1;  	while(1) 	{ 		flag = 1; 		switch (ch) { 			case 'a': 			case 'A': base = 8.75; break; 			case 'b': 			case 'B': base = 9.33; break; 			case 'c': 			case 'C': base = 10.00; break; 			case 'd': 			case 'D': base = 11.20; break; 			default:  				{ 				printf("请输入a、b、c、d，q to quit.\n"); flag = 0; fflush(stdin); ch = getchar();break; 				 } 		} 		if(flag) 			break; 	} 	printf("您的基本工资为%.2f/H.\n", base); 	return base; }  void jisuan(double base) { 	const int jbsj = 40; 	const double shui15 = 0.15; 	const double shui20 = 0.20; 	const double shui25 = 0.25; 	const int jd1 = 300; 	const int jd2 = 450; 	double gz, sj, shui, jgz; 	gz = sj = shui = jgz = 0.00;  	printf("请输入您工作的时间： \n"); 	scanf_s("%lf", &sj); 	if (sj > jbsj) 		sj = 40 + (sj - 40)*1.5; 	gz = sj * base; 	if (gz <= 300)  		shui = gz * shui15; 	else if (gz <= 450)  		shui = jd1 * shui15 + (gz - jd1) * shui20; 	else  		shui = jd1 * shui15 + (jd2 - jd1) * shui20 + (gz - jd1) * shui25; 	jgz = gz -shui; 	printf("您的工资为%.2f，税金为%.2f，净工资为%.2f.\n\n\n", gz, shui, jgz); }

user: user-2
content: 从优化角度上来说，第一，变量定义后最好初始化；第二，应该加个循环，以增加鲁棒性；第三，对于字符型输入，应设置为不区分大小写

user: user-3
content: 类似这样：  double xuanze(char ch) { 	double base; 	int flag; 	while(1) 	{ 		flag = 1; 		switch (ch) { 		case 'a': base = 8.75; break; 		case 'b': base = 9.33; break; 		case 'c': base = 10.00; break; 		case 'd': base = 11.20; break; 		default: 			{ 				printf("请输入a、b、c、d，q to quit.\n"); flag = 0;fflush(stdin);ch = getchar(); break; 			} 		} 		if(flag) 			break; 	}  	printf("您的基本工资为%.2f/H.\n", base); 	return base; }

user: user-1

user: user-0
content: 引用 7 楼 qq423399099 的回复:第五行的声明有问题，改成这样就行了： void jisuan(double base);  另外，在 xuanze定义函数中输入非abcd的值都导致错误，这里面是否应该添加个循环。正确时候跳出，错误时循环。

user: user-3
content: 第五行的声明有问题，改成这样就行了： void jisuan(double base);

user: user-0
content: 最终根据两位给予的指点，完成以下代码，请继续指点： #include <stdio.h> #include <windows.h> void meum(void); double xuanze(char ch); void jisuan(void); int main(void) { 	double base; 	char ch;  	meum(); 	while((ch = getchar()) != 'q'){ 		base = xuanze(ch); 		jisuan(base); 		fflush(stdin); 		meum(); 	} 	system("pause"); 	return 0; }  void meum(void) { 	printf("*************************************************************************\n"); 	printf("a)$8.75/hr                          b)$9.33/hr\n"); 	printf("c)$10.00/hr                         d)$11.20/hr\n"); 	printf("q)quit  \n"); 	printf("*************************************************************************\n"); 	printf("请根据以上选项输入您的每小时的基本工资（q to quit)： "); }  double xuanze(char ch) { 	double base; 	switch (ch) { 		case 'a': base = 8.75; break; 		case 'b': base = 9.33; break; 		case 'c': base = 10.00; break; 		case 'd': base = 11.20; break; 		default: printf("请输入a、b、c、d，q to quit.\n"); break; 	} 	printf("您的基本工资为%.2f/H.\n", base); 	return base; }  void jisuan(double base) { 	const int jbsj = 40; 	const double shui15 = 0.15; 	const double shui20 = 0.20; 	const double shui25 = 0.25; 	const int jd1 = 300; 	const int jd2 = 450; 	double gz, sj, shui, jgz;  	printf("请输入您工作的时间： \n"); 	scanf_s("%lf", &sj); 	if (sj > jbsj) 		sj = 40 + (sj - 40)*1.5; 	gz = sj * base; 	if (gz <= 300)  		shui = gz * shui15; 	else if (gz <= 450)  		shui = jd1 * shui15 + (gz - jd1) * shui20; 	else  		shui = jd1 * shui15 + (jd2 - jd1) * shui20 + (gz - jd1) * shui25; 	jgz = gz -shui; 	printf("您的工资为%.2f，税金为%.2f，净工资为%.2f.\n\n\n", gz, shui, jgz); }

user: user-2
content: 引用 3 楼 qcx1221 的回复:[quote=引用 2 楼 ghx287524027 的回复:] 将第二层while循环放到第一层循环之中，如下：  while((ch = getchar()) != 'q'){     base = xuanze(ch);     printf("请输入您工作的时间： \n"); //  while ((scanf_s("%lf", &sj)) == 1.00) {     while ((sj = getchar()) != 'q'){         if (sj > jbsj)             sj = 40 + (sj - 40)*1.5;         gz = sj * base;         if (gz <= 300) {             shui = gz * shui15;             jgz = gz - shui;         }         else if (gz <= 450) {             shui = jd1 * shui15 + (gz - jd1) * shui20;             jgz = gz - shui;         }         else {             shui = jd1 * shui15 + (jd2 - jd1) * shui20 + (gz - jd1) * shui25;             jgz = gz - shui;         }         printf("您的工资为%.2f，税金为%.2f，净工资为%.2f.\n\n\n", gz, shui, jgz);           fflush(stdin);         meum();     } }   另外，你输入的时间为什么要和‘q’比较，这样没什么意义吧。 时间可以以 scanf("%lf",&sj) 的形式输入，不过要在输入之前清空一下缓冲区，即应该是： fflush(stdin);  scanf("%lf",&sj)；   感谢您的回复与指教。 根据您的指点，我修改成为以下代码： #include <stdio.h> #include <windows.h> void meum(void); double xuanze(char ch); int main(void) { 	const int jbsj = 40; 	const double jb = 1.5; 	const double shui15 = 0.15; 	const double shui20 = 0.20; 	const double shui25 = 0.25; 	const int jd1 = 300; 	const int jd2 = 450; 	double sj, base, gz, shui, jgz; 	char ch;  	meum(); 	while((ch = getchar()) != 'q'){ 		base = xuanze(ch); 		printf("请输入您工作的时间： \n"); 		fflush(stdin); 		scanf_s("%lf", &sj); 		if (sj > jbsj) 				sj = 40 + (sj - 40)*1.5; 			gz = sj * base; 		if (gz <= 300) { 				shui = gz * shui15; 				jgz = gz - shui; 		} 		else if (gz <= 450) { 				shui = jd1 * shui15 + (gz - jd1) * shui20; 				jgz = gz - shui; 		} 		else { 				shui = jd1 * shui15 + (jd2 - jd1) * shui20 + (gz - jd1) * shui25; 				jgz = gz - shui; 		} 		printf("您的工资为%.2f，税金为%.2f，净工资为%.2f.\n\n\n", gz, shui, jgz); 		fflush(stdin); 		meum(); 		} 	system("pause"); 	return 0; }[/quote]  这样可以，应该就是你想要的吧

user: user-2
content: 这是vc环境下的写法：  #include <stdio.h> #include <windows.h> void meum(void); double xuanze(char ch); int main(void) {     const int jbsj = 40;     const double jb = 1.5;     const double shui15 = 0.15;     const double shui20 = 0.20;     const double shui25 = 0.25;     const int jd1 = 300;     const int jd2 = 450;     double sj, base, gz, shui, jgz;     char ch;       meum();      while((ch = getchar()) != 'q'){ 		base = xuanze(ch); 	 		printf("请输入您工作的时间： \n"); 		fflush(stdin); 		scanf("%lf", &sj); 		if (sj > jbsj) 			sj = 40 + (sj - 40)*1.5; 		gz = sj * base; 		if (gz <= 300) { 			shui = gz * shui15; 			jgz = gz - shui; 		} 		else if (gz <= 450) { 			shui = jd1 * shui15 + (gz - jd1) * shui20; 			jgz = gz - shui; 		} 		else { 			shui = jd1 * shui15 + (jd2 - jd1) * shui20 + (gz - jd1) * shui25; 			jgz = gz - shui; 		} 		printf("您的工资为%.2f，税金为%.2f，净工资为%.2f.\n\n\n", gz, shui, jgz); 		meum(); 		fflush(stdin); 	}     system("pause");     return 0; }   void meum(void) {     printf("*************************************************************************\n");     printf("a)$8.75/hr                          b)$9.33/hr\n");     printf("c)$10.00/hr                         d)$11.20/hr\n");     printf("q)quit  \n");     printf("*************************************************************************\n");     printf("请根据以上选项输入您的每小时的基本工资(q to quit)： "); }   double xuanze(char ch) {     double base;     switch (ch) {         case 'a': base = 8.75; break;         case 'b': base = 9.33; break;         case 'c': base = 10.00; break;         case 'd': base = 11.20; break;         default: printf("请输入a、b、c、d，q to quit.\n"); break;     }     printf("您的基本工资为%.2f/H.\n", base);     return base; }