如何获得我在下面提到的程序输出。 [英] How do I get the output of the program that I had mentioned below.

查看:92
本文介绍了如何获得我在下面提到的程序输出。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有得到输出。请使用代码帮助我。这是一个程序,用于打印小于或等于给定整数的所有数字中存在的总数。

例如如果n = 10

则1,2,3,4,5,6,7,8,9,10

输出为2,即总数为2



我尝试过:



#include< iostream>

使用命名空间std;

int nuz(int num)

{int i,count = 0;

for (i = 1; i< = num; i ++)

{while(i> 0)

{if(i%10 == 1)

{count ++;

num = num / 10;

}}}

返回计数;}

int main()

{int x,c;

cout<<输入否;

cin>> x;

c = nuz(x);

cout<<零是<< c;

返回0;

}

I am not getting the output.Help me with code.This is a program to print the total number of ones present in the all numbers less than or equal to a given integer.
for example if n=10
then 1,2,3,4,5,6,7,8,9,10
output is 2 that is total one's are 2

What I have tried:

#include <iostream>
using namespace std;
int nuz(int num)
{int i,count=0;
for(i=1;i<=num;i++)
{while(i>0)
{if(i%10==1)
{count++;
num=num/10;
}}}
return count;}
int main()
{int x,c;
cout<<"enter no";
cin>>x;
c=nuz(x);
cout<<"zeroes are"<<c;
return 0;
}

推荐答案

您的导师指派的部分任务涉及使代码正常工作 - 而且它是真实的这是整个工作中最长的一部分。但它是最重要的学习内容之一 - 没有它,你根本就没有学到太多东西。这是一项技能,就像所有技能一样,你只能通过练习来培养它们。



所以,它将取决于你。

在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一种新的(非常非常有用的)技能:调试!



和BTW:我将要编辑您的问题并添加格式以保留缩进。但是你根本没有缩进它,所以它只是一个PITA来阅读并试图理解。缩进你的代码,它更容易遵循 - 当我看到这样的行:
Part of the task you have been assigned by your tutor involves getting the code working - and it's often the longest part of the whole job. But it'#s one of the most important things to learn - without it, you aren't really learning much at all. And it's a skill, and like all skills you only develop them by practice.

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

And BTW: I was going to edit your question and add formatting to preserve the indentation. But you haven't indented it at all, so it's just a PITA to read and try to understand. Indent your code, it make s it easier to follow - and when I see lines like this:
}}}

我开始颤抖,真的不想看在它任何进一步!我知道你只是一个初学者,但是从一开始就学习好的做法 - 他们会让你的工作变得更轻松!

I start to shudder and really don't want to look at it any further! I know you are only a beginner, but learn good practices to start with - they will make your job a whole lot easier!


学会正确缩进你的代码,它显示它的结构和它帮助阅读和理解。

Learn to indent properly your code, it show its structure and it helps reading and understanding.
#include <iostream>
using namespace std;
int nuz(int num)
{
  int i,count=0;
  for(i=1;i<=num;i++)
  {
    while(i>0)
    {
      if(i%10==1)
      {
        count++;
        num=num/10;
      }
    }
  }
  return count;
}
int main()
{
  int x,c;
  cout<<"enter no";
  cin>>x;
  c=nuz(x);
  cout<<"zeroes are"<<c;
  return 0;
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]

-----

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

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

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



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



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

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

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

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


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
-----
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.


这篇关于如何获得我在下面提到的程序输出。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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