簡體   English   中英

“返回”沒有從數組函數返回正確的值

[英]“return” isn't returning the correct value from an array function

我試圖按行設置每個數組值的價格。 每次程序啟動時必須輸入每一行的價格。

程序符合要求,但輸出不正確,顯示如下:

Please enter price for row  0 = 66

please enter row number 0

please enter seat number 0

0  1  2  3  4  5  6  7  8  9  10 11 12 13 14
---------------------------------------------
#  *  *  *  *  *  *  *  *  *  *  *  *  *  *  |0
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  |1
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  |2
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  |3
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  |4
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  |5
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  |6
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  |7
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  |8
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  |9

-858993460

Press any key to continue . . .

不正確的是“ -858993460”,它應該顯示66。有人幫助我

我的代碼看起來像

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

using namespace std;

void drawgrid(int ai[10][15], int ji, int ii); // draw grid proto

int numb(int num[10], int tps)
// this function returns back the array possition 
// that matchest "tps" ' number
{
    return num[0];
}

void numbee(int nu[10])
// this function prompts the user for the price of each row
{

        for (int i = 0; i < 10; i++)
        {
            cout << " Please enter price for row  " << i << endl;
            cin >> nu[i];

        }

        return;
}

int printArray(int a[10][15],int tp,int pp)
// this function asks the user what seat to buy

{
        int num[10];
        cout << "please enter row number ";
        cin >> tp;
        cout << "please enter seat number";
        cin >> pp;
        a[tp][pp] = numb(num, tp);

        return 0;
}

int main() // this is the main
// calls numbee, drawgrid, and printArray
// sets all to 0 using memset
{  
        int n[10];
        numbee(n);
        int love;
        int a[10][15];
        int i = 0, j = 0;
        memset(a, 0, sizeof(a[10][15]) * 10 * 15); // set everything in gridto 0
        drawgrid(a, i, j);

        love = printArray(a, i, j);
        numb(n, love);

        drawgrid(a, i, j);

        cout << a[0][0];

        system("PAUSE");
        return 0;

}

void drawgrid(int ai[10][15], int ji, int ii)
// this function draws the gridd
{
        ii = 0; ji = 0;
        cout << "0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 ";
        std::cout << std::endl;
        cout << "---------------------------------------------";
        std::cout << std::endl;

        for (int ii = 0; ii < 10; ii++)    //This loops on the rows.
        {
            for (int ji = 0; ji < 15; ji++) //This loops on the columns
            {
                if (ai[ii][ji] == 0)
                {
                    cout << "*" << "  ";
                }
                else
                {
                    cout << "#" << "  ";
                }

            }
            cout << "|"  << ii;
            cout << endl;
        }
}

在此功能中:

int printArray(int a[10][15],int tp,int pp) 
// this function asks the user what seat to buy
{
int num[10];
cout << "please enter row number ";
cin >> tp; 
cout << "please enter seat number";
cin >> pp; 
a[tp][pp] = numb(num, tp); 

return 0;
}

您使用了delcare num [10],但未對其進行初始化。 然后在這一行:

a[tp][pp] = numb(num, tp); 

通過一個函數:

int numb(int num[10], int tps) 
// this function returns back the array possition 
// that matchest "tps" ' number
{
    return num[0];
}

您將未初始化的num [0]寫入a [0] [0]。 這就是為什么你會垃圾。

由於命名,您的代碼非常混亂-“ numb”和“ numbee”是胡說八道,要求席位的函數稱為“ printArray”,但從不打印數組。 您可能應該對命名進行一些工作。

關於這個問題:

printArray有一個局部數組變量num
此數組未初始化,因此可以包含任何隨機數據。
然后,將此數組的第一個元素的值(從numb(num, tp) )分配給a[tp][pp]; ,這就是奇怪數據的來源。

很難提出補救措施的建議,因為還不清楚您打算使用什么numb和代碼,例如

love = printArray(a, i, j);
numb(n, love);

去完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM