简体   繁体   English

for循环未运行,尝试向后遍历数组元素

[英]for loop not running, trying to iterate backwards through array elements

To practice using pointers and arrays i'm trying to do a simple program capable of converting a binary input to denary.. i think i have a good idea for the logic but i haven't even got round to trying to implement it because im struggling to get my for loop running! 为了练习使用指针和数组,我试图做一个能够将二进制输入转换为denial的简单程序。.我认为我对逻辑有一个很好的主意,但是我什至无法尝试实现它,因为im努力让我的for循环运行!

It seems silly but i know the code inside the for loop works fine outside of it, so it must be something wrong with the condition..? 看起来很愚蠢,但是我知道for循环内的代码在它外面可以正常工作, 因此条件一定有问题。 im trying to start at the back of the char array (navigating using pointers) and output each char(as an int) up to the first element. 我试图从char数组的后面开始(使用指针进行导航),并将每个char(作为int形式)输出到第一个元素。

So the desired output is "0 - 1 - 0 - 1 -" 因此,所需的输出为“ 0-1-0-1-”

#include <iostream>

using std::cout;
using std::endl;

//prototypes
void binaryToDenary(const char* input, int& inputLength);

int main(){

    const char binaryInput[] = {1,0,1,0};
    int inputLength = sizeof(binaryInput)/sizeof(binaryInput[0]);

    binaryToDenary(binaryInput, inputLength);

    return 0;
}
void binaryToDenary(const char* input, int& inputLength){
    //testing some stuff--- this all works as expected
    //cout << input[2] << " " << (int)*(input+2) << " " << inputLength <<endl;

    int i;
    for(i = inputLength; i < 0; i--){
        cout << (int)*(input+i) << " - ";
    }

}

Your for loop should be this: 您的for循环应为:

for(i = inputLength -1 ; i  >= 0; i--)
{
    cout << (int)*(input+i) << " - ";
}

There are two problems in your code: 您的代码中有两个问题:

  • i = inputLength which should be i = inputLength -1 i = inputLength应该是i = inputLength -1
  • i < 0 which should be i >= 0 i < 0应该是i >= 0

Also, change the second parameter type from int & to int : 另外,将第二个参数类型从int &更改为int

void binaryToDenary(const char* input, int inputLength) //now its better!

The type int& reduces the use cases, and benefits almost nothing. int&类型减少了用例, 几乎没有任何好处。 If you use int & , then all of these would give compilation error: 如果使用int & ,那么所有这些都将导致编译错误:

const int inputLength = sizeof(binaryInput)/sizeof(binaryInput[0]);
^^^^ note this

binaryToDenary(binaryInput, inputLength); //compilation error
binaryToDenary(binaryInput, sizeof(binaryInput)/sizeof(binaryInput[0])); //error
binaryToDenary(binaryInput, 4); ////compilation error

So use int , and all of the above would compile fine! 因此,使用int ,以上所有内容都可以正常编译!

Array indexes start from zero, so the last element is at inputLength - 1 . 数组索引从零开始,因此最后一个元素在inputLength - 1 With i < 0 you exit from the loop immediately as that never be true... i < 0您将立即退出循环,因为那永远都不是真的...

for(i = inputLength - 1; i >= 0; i--){
    cout << (int)*(input+i) << " - ";
}
for(i = inputLength; i < 0; i--)

will run only if inputLength is less than 0, which is not possible? 仅在inputLength小于0时才能运行,这是不可能的吗?

You need: 你需要:

for(i = (inputLength-1); i >= 0; i--)
         ^^^^^^^^^^^^^^    ^^

C arrays are 0 based so a valid index is given by C数组基于0,因此有效索引为

(0 <= i) && (i < array_length)

In your program, this means that the position of the last digit in your initialization should be inputLength - 1 and the loop condition should be i >= 0 . 在您的程序中,这意味着初始化中最后一位的位置应为inputLength - 1 ,循环条件应为i >= 0

(As for why you loop is not running, at the start you have i == inputLength , so i is positive, failing the i < 0 condition immediately). (关于为什么循环未运行的原因,一开始我有i == inputLength ,所以我为正数,立即使i < 0条件失效)。

You want to run as long as i is bigger (or maybe equal) to zero. 只要i大于(或等于)零,就可以运行。 You were trying to run the loop as long as i was less than zero, and beginning with a value greater than zero results to that you never enter the loop. 只要i小于零,就一直在尝试运行循环,并且从大于零的值开始会导致您永远不会进入循环。

for(i = inputLength; i > 0; i--){
        cout << (int)*(input+i) << " - ";
}

You must check for iteration loop variable i to be positive.. 您必须检查迭代循环变量i是否为正。

However you should use a STL iterator on the binary input vector and not loop on it's content in ac fashion, if you want to practice C++, possible solution could be: 但是,您应该在二进制输入向量上使用STL迭代器,而不要以ac方式循环其内容,如果您想练习C ++,可能的解决方案可能是:

vector<char> binaryInput;

binaryInput.push_back(1);
binaryInput.push_back(0);
binaryInput.push_back(1);
binaryInput.push_back(0);

vector<char>::iterator it;

for ( it=myvector.begin() ; it < myvector.end(); it++ ){
    cout << " " << *it << endl; //or whatever you need to do with vector content
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM