C ++编程错误:C4703,C4700

时间:2014-03-01 03:37:44

标签: c++ visual-c++

我对编程比较陌生,我刚刚学习了c ++,而且我从学校获得了2个错误的硬件分配;

错误2错误C4700:未初始化的局部变量'searchValueptr'使用了第83行 和 错误3错误C4703:使用了未初始化的本地指针变量'createdArray' 第70行

我不能为我的生活弄清楚为什么或如何解决它可以帮助我。

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

// prototypes
int *createArray(int &size);
void findStats(int *arr, int size, int &min, int &max, double &average);
int *searchElement(int *arr, int size, int *element);


int main ()
{
    int choice, size;

    bool menuOn = true;

    while (menuOn == true)
    {
        cout << "1 - Create and initialize a dynamic array.\n";
        cout << "2 - Display statistics on the array.\n";
        cout << "3 - Search for an element.\n";
        cout << "4 - Exit.\n";
        cout << "Enter your choice and press return: ";

        cin >> choice;

        cout << endl;

        switch (choice)
        {

            case 1:
                int *createdArray;
                cout << "Please enter a size for the array: ";
                cin >> size;
                createdArray = createArray(size);

                for (int x=0; x < size; x++)
                {
                    cout << "Value of array at index " << x << ": " << createdArray[x] << endl;
                }

                cout << endl;

                break;

            case 2:
                int minNum;
                int maxNum;
                double avgNum;
                findStats(createdArray, size, minNum, maxNum, avgNum);
                cout << endl << "The maximum number is: " << maxNum;
                cout << endl << "The minimum number is: " << minNum;
                cout << endl << "The average number is: " << avgNum;
                cout << endl << endl;
                break;

            case 3:
                int *searchValueptr;
                int searchValue;
                cout << "Enter a value to search for: ";
                cin >> searchValue;
                *searchValueptr = searchValue;
                searchElement(createdArray, size, searchValueptr);
                break;


            case 4:
                cout << "Thanks for using this program";
                menuOn = false;
                break;

            default:
                cout << "Not a Valid Choice. \n";
                cout << "Choose again.\n";
                cin >> choice;
                break;

        }

    }
    cout << endl;
    system("pause"); 
    return 0; 

} // end function main () 

int *createArray(int &size) 
{
    unsigned seed = time(0);
    srand(seed);
    int *randArray = new int [size];
    for (int x=0; x < size; x++)
    {
        randArray[x] = rand();
    }

    cout << endl;
    return randArray;
}

void findStats(int *arr, int size, int &min, int &max, double &average)
{
    min = arr[0];
    max = arr[0];
    double sum = 0;
    for (int count = 0; count < size; count++)
    {
        if (min >= arr[count])
        {
            min = arr[count];
        }

        if (max <= arr[count])
        {
            max = arr[count];
        }
        sum += arr[count];
    }

    average = sum/size;
}

int *searchElement(int *arr, int size, int *element)
{
    bool match = false;
    for (int count = 0; count < size; count++)
    {
        if (arr[count] == *element)
        {
            match = true;         
        }

    }
    if (match)
    {
        cout << "Match Found at: " << element;
        return element;
    }
    else 
    {
        cout << "No Found";
        return 0;
    }
}

1 个答案:

答案 0 :(得分:0)

使用

searchValueptr = &searchValue;

或发送passValue

的地址
searchElement(createdArray, size, &searchValue);
break;