C程序不会运行没有任何错误或警告。 [英] C program doesn't run without any errors or warnings.

查看:67
本文介绍了C程序不会运行没有任何错误或警告。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/* Program to accept marks and assign a class */

#include<stdio.h>
#include<conio.h>
void main()
{
 int m;
 char ch;
 clrscr();
 printf("ENTER MARKS (0-100) : ");
 scanf("%d",&m);

 s:
 if(m<40)
	printf("\nResult :\n %d is FAIL",m);
 else if(m>=40 && m<50)
	printf("\nResult :\n %d is THIRD CLASS",m);
 else if(m>=50 && m<60)
	printf("\nResult :\n %d is SECOND CLASS",m);
 else if(m>=60 && m<70)
	printf("\nResult :\n %d is FIRST CLASS",m);
 else if(m>=70 && m<=100)
	printf("\nResult :\n %d is DISTINCTION",m);
 else
	printf("\n %d is Invalid Marks",m);

	printf("\n Do you want to check another mark : Type (Y/N)");
	printf("\n Enter choice : ",ch);
	scanf("%c",&ch);
	if(ch == 'Y' || 'y')
		goto s;
	else if(ch == 'N' || 'n')
		goto end;
	else
		printf("\nInvalid selection",ch);

end:
getch();
 }





这是代码。程序工作正常,除了最后一部分。它显示'等级'然后要求另一个条目,然后重复相同的事情。我希望程序要求选择Y或N的另一个条目。请帮助我。我是这个编程的新手..



This is the code. The programs works fine except the last part. It shows the 'grade' and then asks for another entry and then same thing repeats. I want the program to ask for another entry with a choice Y or N. Please help me with this. I am new to this programming..

推荐答案

你的if语句错了,替换

Your if statements are wrong, replace
if(ch == 'Y' || 'y')



with the br />


with

if(ch == 'Y' || ch == 'y')





'y'将评估为真,因此条件始终为真。



和if语句一样检查没有。



希望这有帮助,

Fredrik



'y' will evaluate as true so the condition is always true.

And the same goes for the if-statements checking for no.

Hope this helps,
Fredrik


if(ch =='Y'||'y')



'y'总是如此。 :D $ / $




你可能想要:



if(ch =='Y'|| ch =='y')





请删除 goto s,我建议执行 /







if if(ch =='N'||'n')

goto end;

else

printf(\ nInvalid selection,ch);







这太傻了。
"if(ch == 'Y' || 'y')"

'y' is always true. :D


You probably want:

if(ch == 'Y' || ch == 'y')


Please eliminate the gotos, I recommend a do/while.


"
else if(ch == 'N' || 'n')
goto end;
else
printf("\nInvalid selection",ch);

"

That's silly.


第一件事:忘记你曾经看过关键字goto或如何创建标签。不要再使用它们至少四年的C编码 - 如果你需要它们,你设计的代码是错误的。四年之后,你应该知道你应该在哪里使用它们,但你也知道可能不需要...



如果你有使用循环结构完成此操作很明显你的问题是什么:

First things first: Forget you ever saw the keyword "goto" or how to create labels. Do not use them again for at least four years of "C" coding - if you need them, you have designed your code wrong. After four years, you should know enough to understand where you should use them, but you will also know enough to probably not need to either...

If you had done this using loop constructs it would be obvious what your problem is:
int main(void)
    {
    int m;
    char ch;
    bool done = false;
    clrscr();
    printf("ENTER MARKS (0-100) : ");
    scanf("%d",&m);

    while (!done)
        {
        if(m<40)
            printf("\nResult :\n %d is FAIL",m);
        else if(m>=40 && m<50)
            printf("\nResult :\n %d is THIRD CLASS",m);
        else if(m>=50 && m<60)
            printf("\nResult :\n %d is SECOND CLASS",m);
        else if(m>=60 && m<70)
            printf("\nResult :\n %d is FIRST CLASS",m);
        else if(m>=70 && m<=100)
            printf("\nResult :\n %d is DISTINCTION",m);
        else
            printf("\n %d is Invalid Marks",m);

        printf("\n Do you want to check another mark : Type (Y/N)");
        printf("\n Enter choice : ",ch);
        scanf("%c",&ch);
        if(ch == 'Y' || ch == 'y')
            done = false;
        else if(ch == 'N' || ch == 'n')
            done = true;
        else
            printf("\nInvalid selection",ch);
        }
    return 0;
    }



现在,移动循环结构,以便提示和输入新值在循环内...



错过了缺乏条件测试:doh: - OriginalGriff [/ edit]


Now, move the loop construct so that the prompt for and input of the new value are within the loop...

[edit]Missed the lack of conditional testing :doh: - OriginalGriff[/edit]


这篇关于C程序不会运行没有任何错误或警告。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆