如何在C ++中找到问题的长度? [英] How do I find the length of a question in C++?

查看:61
本文介绍了如何在C ++中找到问题的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,343是一个数字,我们需要找到它的长度,我们该怎么做,我们如何在c ++程序中打印,它是3?

Suppose, 343 is a number, and we are required to find its length, what do we do, how do we print in a c++ program, that it is 3?

<pre>// Main function of the C++ program.

#include <iostream>
using namespace std;

int main()
{
 cout<<"Enter number to get the length of the number.";
 cin>>x;
 int xlen=0;
 for(int i!=0; i=x/10; xlen++)
  { cout<<"The length is:"<xlen;
    int l=xlen;
    cout<<l;
}
return 0;
}





我的尝试:



请告诉我错误是什么以及解决方案。由于我是初学者,请帮我一个代码。谢谢。



What I have tried:

Kindly, tell me what the error is and the solution. As I am a beginner, please help me with a code. Thanks.

推荐答案

不要猜猜代码是什么,想一想就行了!

首先,你需要读取一个数字。

然后你需要计算那个数字中的位数 - 所以你需要一个循环,你需要一个计数器,每次你绕循环上升一个。然后你需要在循环后打印计数器的值。

你有一些,差不多。声明 x 并且第一位将起作用:

Don't guess what the code should be, think about it and work it out!
First off, you need to read in a number.
Then you need to count the number of digits in that number - so you need a loop, and you need a counter that goes up by one each time you go round the loop. Then you need to print the value of the counter after the loop.
Some of that you have, almost. Declare x and the first bit'll work:
int x = 0;
cout << "Enter number to get the length of: ";
cin >> x;

现在你定义了长度,但是我把它设置为1,而不是零:

Now you defined the length, but I'd set it to one, not zero:

int xLen = 1;

为什么一个?因为0是一个数字,所以如果用户输入零,你应该说他的数字是一位数。

然后我会使用循环而不是

Why one? Because "0" is a digit, so if the user enters zero, you should say his number is one digit long.
Then I'd use a while loop instead of a for:

while (x != 0)
   {
   ...
   }

为什么同时?只是因为它更容易阅读!

在循环中,我将更改 x 的值并计算每个数字:

Why a while? Just because it's easier to read!
And inside the loop, I'd change the value of x and count each digit:

x = x / 10;
xLen++;

然后在循环之后,打印你计算出的长度:

Then after the loop, print the length you worked out:

cout << endl << "The length is:" << xlen << endl;


你的 for 语句是完全错误的。正确的格式是:

Your for statement is completely wrong. The correct format is:
for (initial expression; comparison expression; repeat expression)
{
// body
}



所以在你的情况下它应该是这样的:


So in your case it should be something like:

// Start by setting i to the input number
// repeat while i is not equal to zero
// at the end of each loop add 1 to xlen
for(int i = x; i != 0; xlen++)
{
    i/=10;    // divide i by 10
}
// print results here


解决方案1和2已经给出你解释你的代码有什么问题。



而不是猜测代码中的错误,使用DIY方法,使用调试器看看你的代码到底做了什么,你会发现它与应该做什么有什么不对。

建议:不要试图打包你的代码来保存一行或2,你是一个学习者,尝试编写清晰的代码并发表评论。

-----

有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

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

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



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



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

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

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

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Solution 1 and 2 already give you an explanation of what is wrong in your code.

Rather than guessing what can be wrong in your code, use the DIY method, use the debugger to see what your code really does, you will find what is wrong by comparing with what it should do.
Advice: don't try to pack your code to save a line or 2, you are a learner, try to write clear code and put comments.
-----
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.


这篇关于如何在C ++中找到问题的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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