Incorrect values returned from functions, have 2 warnings I don't understand

This is a discussion on Incorrect values returned from functions, have 2 warnings I don't understand within the C++ Programming forums, part of the General Programming Boards category; Environment: Desktop, Compiler: MS VC++ 6.0 My program is not putting out the correct result and I don't understand what ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Incorrect values returned from functions, have 2 warnings I don't understand

    Environment: Desktop, Compiler: MS VC++ 6.0

    My program is not putting out the correct result and I don't understand what those warnings means. All I am trying to find is the minimum and maximum value from the array.
    Is there MS library that explain those mysterious messages?
    Thank you anyone for your help
    My codes and results below:

    #include <iostream>
    using namespace std;

    #define num_elems 8

    // function prototype
    int max_price(int priceList[num_elems], int);
    int min_price(int priceList[num_elems], int);

    // last upated: Jan. 29, 2002 08:45 p.m - Jan. 30 09:15 p.m

    void main()
    {
    int lowestPrice;
    int highestPrice;
    // The members of the array
    int priceList[num_elems] = {8, 25, 36, 44, 52, 60, 75, 89};
    //int minPrice = priceList[0];
    //int maxPrice = priceList[0];
    //int a = 8;

    // Announce the result
    lowestPrice = min_price(priceList, num_elems);

    cout << "The lowest price in the array is: ";
    cout << lowestPrice;
    cout << endl;

    // Announce the result
    //Display the highest price << Maximum << "." << endl;

    highestPrice = max_price(priceList, num_elems);
    cout << "The highest price in the array is: ";
    cout << highestPrice << endl; // num_elems being the # of elements in the array

    }// end of main

    // =============== ========================= =========== =======
    // FUNCTION DEFINITIONS

    int max_price(int priceList[], int)
    {
    //cout << "Finding the Maximum" << endl;
    int maxIndex = 0;
    int max = priceList[maxIndex]; // assume first element ismax
    // now go through the rest of the array
    // Compare the members
    for ( int i = 1; i < num_elems; ++i)
    {
    if (priceList[i] > max)
    {
    max = priceList[i];
    maxIndex = i;
    }
    return maxIndex;
    }
    }

    // find the minimum pice
    int min_price(int priceList[], int)
    {
    int minIndex = 0;
    int min = priceList[minIndex]; // assume first element is min
    // now go through the rest of the array
    // Compare the members
    for (int i = 1; i < num_elems; ++i)
    {
    if (priceList[i] < min)
    {
    min = priceList[i];
    minIndex = i;
    }
    return minIndex;
    }
    }

    /*
    error list & warnings:
    -----------Configuration: find_min_max - Win32 Debug------------
    Compiling...
    find_min_max.cpp
    H:\ject\find_min_max.cpp(60) : warning C4715: 'max_price' : not all control paths return a value
    H:\ject\find_min_max.cpp(78) : warning C4715: 'min_price' : not all control paths return a value

    find_min_max.obj - 0 error(s), 2 warning(s)


    // Result of test

    The lowest price in the array is: 0 <---WRONG RESULT
    The highest price in the array is: 1 <---WRONG RESULT
    Press any key to continue

    */

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Try this
    Code:
    int max_price(int priceList[], int) 
    { 
        int maxIndex = 0; 
        int max = priceList[maxIndex];
        for ( int i = 0; i < num_elems; ++i) 
        {
            // What if priceList[i] is == to max? 
            if (priceList[i] > max) 
            { 
                max = priceList[i]; 
                maxIndex = i; 
            }  
        }
        return maxIndex;
    }
    Since you're going through the entire array searching for max then why return inside the loop? Wait until the loop has completed and return the result then, that will clear the error and you'll find that the results are more as you expected.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    int max_price(int priceList[], int)
    int min_price(int priceList[], int)

    What is the point of having an int inputted if it cannot be used(no title in which u can use it)?




    int max_price(int priceList[num_elems], int);
    int min_price(int priceList[num_elems], int);

    Try omitting the size of the matrix in the first declaration and then you can have the size be inputted as the second parameter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions are Still Not Understood.
    By errigour in forum C Programming
    Replies: 6
    Last Post: 04-09-2009, 03:54 PM
  2. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  3. Some Error messages I don't understand...
    By Shogun in forum C Programming
    Replies: 3
    Last Post: 08-05-2004, 04:35 PM
  4. Some errors and warnings i dont understand
    By lakai02 in forum C Programming
    Replies: 6
    Last Post: 10-18-2002, 12:16 PM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 02:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21