while循环中的奇怪错误(C ++)

时间:2017-02-02 21:36:34

标签: c++ algorithm for-loop visual-c++ while-loop

我试图制作一个使用简单算法的程序。

但由于某种原因,我得到一个奇怪的错误(下面是该程序的简化版本)。

#include "stdafx.h"
#include <iostream>
#include <string>

using std::cout;
using std::string;

void find(int arr[], string name)
{
    int t = 8;
    int i = 0;
    int v = 0;

    // t should become equal to the smallest int of the array after this.


    while (arr[i])
    {
        if (arr[i] < t)
        {
            t = arr[i];
        }
        ++i;
    }

    /* When this statement below gets executed t gets what looks like a                 
    random value for some reason */
    cout << arr[t] << '\n';
    for (int b = 0; b < 2; ++b)
    {
        if (t == arr[b])
        {
            v = b;
        }
    }

    /* Again, arr[v] gets what looks like a random number */
    cout << "The cheapest " << name << " is number " << arr[v] << ".";
}



int main()
{

    /* [0] = "Cost for Steve"
       [1] = "Cost for Mark"
       [2] = "Cost for Andrew" */

    int cleaning[] = { 5, 4, 7 };
    int cooking[] = { 3, 6, 4 };
    int babysitting[] = { 7, 6, 3 };

    cout << "Number 1: Steve, Number 2: Mark, Number 3: Andrew.\n";

    find(cleaning, "cleaner");
    find(cooking, "cook");
    find(babysitting, "babysitter");




/* This is to prevent the console application from quitting */

    while (true)
    {

    }

}

我确定for和while循环中有问题,但是什么?

如果您正在阅读我的代码并且某些文字或变量名称对您来说似乎很陌生,那么我可能会忘记翻译它(这最初是用意大利语写的)。

感谢您花时间阅读本文。

编辑:感谢@Tar我修复了程序的第一部分,但是部分说The (name) that costs less is n. (arr[v]).仍然给我一个随机数,我编译并运行程序,输出是:

Number 1: Steve, Number 2: Mark, Number 3: Andrew.
4
The cheapest cleaner is number 4.
3
The cheapest cook is number 3.
3
The cheapest babysitter is number 7.

这显然是错误的,因为应该说最便宜的清洁工是2号,最便宜的厨师是1号,最便宜的保姆是3号。

PS:一旦一切都修好了,我会拿出打印出最便宜价格的部分。

2 个答案:

答案 0 :(得分:3)

问题出在$(document).ready(function domReady() { $.getJSON('https://world.openfoodfacts.org/cgi/search.pl?search_terms=<?php echo $_POST["search"]; ?>&search_simple=1&action=process&json=1') .done(function success(response) { var links; links = $(response.products.map(function toProductLink(product, index) { var id; id = index + 1; return $('<a href="#">') .text(product.generic_name_fr) .attr('id', 'product-link-' + id) .attr('name', 'recup'), .data('calories', product.energy); })); links.appendTo($('#push')); }) .fail(function fail() { console.error(arguments); }); }); 的第一个while循环中:

find

在这里,您不断评估while (arr[i]) // while the element at arr[i] is NOT 0 { if (arr[i] < t) { t = arr[i]; } i++; } 中的元素是否不是arr。这是不正确的。您已将数组声明为:

0

这些都不包含int cleaning[3] = { 5, 4, 7 }; int cooking[3] = { 3, 6, 4 }; int babysitting[3] = { 7, 6, 3 }; ,因此您的0循环将无限期地运行,并且您将读取每个数组的内存,这不是好消息。

请考虑使用std::vector,并查看您的代码变得更清晰,更安全:

while

答案 1 :(得分:1)

最重要的是,我想发表一个声明:我不是讲英语的人,所以如果我说错话,请原谅。
我认为这个问题并不是很困难。我修改了你的算法和输出格式。实际上,我差不多重写了它 在我看来,你的代码似乎有点幼稚。如果您只学习了C ++语法,那么研究算法还有很长的路要走。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int find(const vector<int>& numbers) {
    int minVul = numbers[0];
    int rank = 0;

    for (int i = 1; i < numbers.size(); i++)
    {
        if (minVul > numbers[i])
        {
            minVul = numbers[i];
            rank = i;
        }
    }
    return rank;
}



int main() {

    vector<string> name = { "steve","mark","andrew" };
    /* [0] = "Cost for steve"
    [1] = "Cost for mark"
    [2] = "Cost for andrew" */

    vector<int> cleaning = { 5, 4, 7 };
    vector<int> cooking = { 3, 6, 4 };
    vector<int> babysitting = { 7, 6, 3 };

    int cleaner = find(cleaning);
    cout << "Cleaning:" << name[cleaner] << " costs least in " << cleaning[cleaner] << endl;
    int cooker = find(cooking);
    cout << "Cooking:" << name[cooker] << " costs least in " << cooking[cooker] << endl;
    int babysitter = find(babysitting);
    cout << "Babysitter:" << name[babysitter] << " costs least in " << babysitting[babysitter] << endl;

    system("pause"); //This is a common skill to prevent the console application from quitting.
    return 0;
}

输出:

Cleaning:mark costs least in 4
Cooking:steve costs least in 3
Babysitter:andrew costs least in 3