c++ - "return"没有从数组函数返回正确的值

标签 c++ arrays function loops return

我试图按行设置每个数组值的价格。 每次程序启动时都必须输入每行的价格。

程序符合但输出不正确,显示如下:

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]。这就是你得到垃圾的原因。

关于c++ - "return"没有从数组函数返回正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19121492/

相关文章:

python - 如何在python中的不同文件中导入嵌套函数?

JavaScript 变量/字符串函数?

c++ - 生成n个随机变量,并将它们相乘C++

c++ - libc++abi.dylib:以 std::__1::system_error 类型的未捕获异常终止:互斥锁失败:参数无效

c++ - 柔软、透明的笔画纹理没有像我预期的那样混合

c++ - C++中的抽取

c++ - 可以使用 <、>、= 比较双字段吗?

arrays - 使用 "+="运算符连接到可选数组

java - 在java中解析嵌套的json数组

c++ - 如何将函数从 C++ 可执行文件公开给 LuaJIT