在这种情况下,如何使scanf功能正常工作? [英] How can I make scanf function work in this case?

查看:72
本文介绍了在这种情况下,如何使scanf功能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre>
/*
This project displays number of days in a month

Assumption:- February is assumed to have 28 days
             All other months have 30 or 31 days
*/

#include <stdio.h>
#include <stdlib.h>

int main()

{
    int month_value;
    char user_choice='y';

    while(user_choice=='y'||user_choice=='Y')
    {
        system("cls");
        printf("Enter the serial number of month:- ");
        scanf("%d",&month_value);
        if(month_value>=1&&month_value<=12)
        {
            if(month_value%2!=0)
            {
                printf("The number of days in this month is:- 31\nDo you want to check for another month?(y/n)\n");
                scanf("%c",&user_choice);  // this statement is not getting executed even if condition is true

            }
            else if(month_value%2==0 && month_value!=2)
            {
                printf("The number of days in this month is:- 30\nDo you want to check for another month?(y/n)\n");
                scanf("%c",&user_choice);   // this statement is not getting executed even if condition is true
            }
            else
            {
                printf("The number of days in this month is:- 28\nDo you want to check for another month?(y/n)\n");
                scanf("%c",&user_choice);   // this statement is not getting executed even if condition is true
            }

        }
        else
        {
            printf("ERROR\nWould you like to re-enter the value?(y/n)\n");
	        scanf("%c",&user_choice);   // this statement is not getting executed even if condition is true
        }
    }
    return 0;
}





我的尝试:



我已经花了4-5个小时才想到可能存在什么逻辑错误,但我无法理解。请帮忙。在我看来,它应该有效。



What I have tried:

I have given 4-5 hours just thinking what logical mistake might be present but I couldn't figure it out. Please help.In my opinion, It should work.

推荐答案

嗯。查看每个月的天数。是否遵循绝对规律的模式?
Um. Look at the number of days in each month. Does it follow an absolutely regular pattern?
Month :  J  F  M  A  M  J  J  A  S  O  N  D
Number:  1  2  3  4  5  6  7  8  9 10 11 12
Days  : 31 28 31 30 31 30 31 31 30 31 30 31

即所有偶数月都有30天?







那你为什么要假设(除了例外) 2月的特殊情况)他们这样做了吗?







I.e. do all even numbered months have 30 days?



So why do you assume (with the exception of a special case for February) that they do?


[edit]

引用:

所以写一个带有提示的函数,然后得到一个响应。

So write a function that takes a prompt, and gets a response.



你知道怎么做!这是一个返回yes / no值的函数(在C中,这是 int ,0是 true / 并且任何其他值 false / )并且接受一个字符串,这是一个 char 指针:


You know how to do this! It's a function that returns a yes / no value (in C, that's an int, and 0 is true / yes and any other value is false / no) and that accepts a string, which is a char pointer:

#define true (1==1)
#define false (1==1)
...
int YesOrNo(const char* prompt)
   {
   ...
   }

const 表示无法在函数内部更改 - 如果你尚未涉及,请不要担心它

在函数内部,打印字符串,并将用户响应作为字符串 - 你知道如何做到这一点! br />
查看你得到的字符串中的字符,并决定是否输入y或n。如果是这样,返回 true false

如果没有,继续阅读,直到他做。

你想知道怎么做这个东西!

[/ edit]

The const means "it can't be changed inside the function" - if you haven't covered that yet, don't worry about it
Inside the function, print the string, and get the users response as a string - you know how to do those!
Look at the characters in string you get back, and decide if you types "y" or "n". If so, return true or false.
If not, keep reading until he does.
You know how to do this stuff if you think about it!
[/edit]


有一个工具可以允许您查看代码正在执行的操作,其名称为调试程序。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近一个bug。



[更新]

首先,make确定你认为匹配现实。

尝试

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[Update]
First of all, make sure that what you think match reality.
Try
printf("The number of days in this month is:- 31\nDo you want to check for another month?(y/n)\n");
scanf("%c",&user_choice);  // this statement is not getting executed even if condition is true
printf("The number of days in this month is:- 31\nDo you want to check for another month?(y/n)\n");



如果您获得一次打印,则执行停止。

如果您获得打印两次,则执行扫描但是不要产生你期望的结果。使用调试器在扫描后检查变量的值。


If you get the print once, execution stopped in between.
If you get the print twice, the scan is executed but don't produce the result you expected. Use the debugger to inspect the value of variable after scan.

引用:

即使该语句也未执行条件为真

this statement is not getting executed even if condition is true



只有条件为真时才能执行该语句。


The statement can be executed only if condition is true.


这篇关于在这种情况下,如何使scanf功能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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