This program is 'Calculator'
Do you want to continue?
Type 'y' for yes or 'n' for no
invalid input
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
//program
//first to get two numbers
//second to get choice
int x=0,y=0,n=0;
char choice;
//clrscr(); does no work in devc++
system("cls"); //you may also use system("clear");
while(x==0)
{
puts("\t\tThis program is 'Calculator'\n\n");
puts("Do you want to continue?");
puts("Type 'y' for yes or 'n' for no ");
scanf("%c",&choice);
x++;
if(choice=='y')
{
y++;
puts("if this worked then we would continue to calculate the 2 no");
}
else if(choice=='n')
exit(0);
else
{
puts("invalid input");
x=0;
}
}
getch();
}
回答:如果你输入y和n以外的字符,
else
{
puts("invalid input");
x=0;
}
重置x为0,现在循环条件:
while(x == 0)
如果为真,因此再次进入循环
也许你想要跳过结尾回车字符当这样读取时:
scanf(" %c", &choice );
温馨提示:您的支持与理解,是我们工作最大的动力。如果您是从网上购买的图书,请在对应的网店,给予本书一个好评,我们将衷心感谢!!!
以下是《轻松学C语言》对应的购书评论地址:
当当网:http://product.dangdang.com/23246575.html
亚马逊:http://www.amazon.cn/dp/B00CIXGTOS
京东网:http://item.jd.com/11235318.html
评论