none
C0142 and C2109 errors on C++ RRS feed

  • Question

  • I keep getting these error on my amtDue array in the description of the functions that use the array. Can some tell me what it is and how to fix?

    Thanks,

    Brian

    # include <iostream>
    # include <fstream>
    # include <string>
    using namespace std;
    void loadArrays(string[], double[], int);
    void showArrays(string [], double[], int);
    void lookUpTaxes(string[], double[], int);
    void sortTaxes(string [], double[], int);
    void biggestTaxes(string[],double[], int);
    int showMenu();
    void sortTaxes(string address, double amtDue, int SIZE);
    void swap(string a, string b);
    void swap(double a, double b);
    int main()
    {
     int const SIZE = 8;
     string address[SIZE];
     double amtDue[SIZE];
     ifstream inputFile;
     inputFile.open("prog3.txt");
     int count;
     for (count=0;count<SIZE; count++)
     {
      inputFile >> address[count] >> amtDue[count];
     }
     inputFile.close();
     loadArrays(address, amtDue, SIZE);
     int choice;
     showMenu();
     while (choice != 5)
     {
      if (choice == 1)
      {
       showArrays(address, amtDue, SIZE);
      }
      else if (choice == 2)
      {
       lookUpTaxes(address, amtDue, SIZE);
      }
      else if (choice == 3)
      {
       sortTaxes(address, amtDue, SIZE);
      }
      else if (choice == 4)
      {
       biggestTaxes(address, amtDue, SIZE);
      }
      else
       cout << "Invalid choice- try again: ";
      choice = showMenu();
     }
    return 0;
    }
    int showMenu()
    {
     int choice;
     cout << "County Auditor Property TaxSearch and Sort" << endl;
     cout << "1. Display Property Tax Data" << endl;
     cout << "2. Look up taxes for a particular address" << endl;
     cout << "3. Sort tax amounts in ascending order" << endl;
     cout << "4. Display property with largest tax due" << endl;
     cout << "5. Exit" << endl;
     cout << "Please enter your selection; " << choice << endl;
     return choice;
    }
    void loadArrays(string address, double amtDue, int SIZE)
    {
     int count;
     for (count = 0; count < SIZE; count++)
      address[count]>>amtDue[count];
     
    }
    void showArrays(string address, double amtDue, int SIZE)
    {
     for (int count = 0; count < SIZE; count++)
      cout << address[count] << "-" << "$" << amtDue[count] << endl;
    }
    void lookUpTaxes(string address, double amtDue, int SIZE)
    {
     cout << "Enter the address you are looking up : " << address << endl;
     int first = 0, number, last = number - 1, middle, position = -1,value;
     bool found = false;
     while (!found && first < +last)
     {
      middle = (first + last) / 2;
      if (address[middle] == value)
      {
       found = true;
       position = middle;
      }
      else if (address[middle] > value)
       last = middle - 1;
      else
       first = middle + 1;
     }
     cout << "The taxes for " << address[middle] << " is $" << amtDue[middle];
    }
    void sortTaxes(string address, double amtDue, int SIZE)
    {
     int start, minIndex;
     string tempStart;
     double minValue;
     for (start = 0; start < (SIZE - 1); start++)
     {
      minIndex = start;
      tempStart = address[start];
      minValue = amtDue[start];
      for (int index = start + 1; index < SIZE; index++)
      {
       if (address[index] < minValue)
       {
        tempStart = address[index];
        minValue = amtDue[index];
        minIndex = start;
       }
       swap(address[minIndex], address[start]);
       swap(amtDue[minIndex], amtDue[start]);
      }
     }
     
    }
    void swap(string a, string b)
    {
      string temp= a;
      a = b;
      b = temp;
    }
    void swap(double a, double b)
    {
     double temp=a;
     a = b;
     b = temp;
    }
     
    void biggestTaxes(string address, double amtDue, int SIZE)
    {
     int start, maxIndex;
     double maxValue;
     string tempStart;
     for (start = 0; start < (SIZE - 1); start++)
     {
      maxIndex = start;
      tempStart = address[start];
      maxValue = amtDue[start];
      for (int index = start + 1; index < SIZE; index++)
      {
       if (amtDue[start] > maxValue)
       {
        tempStart = address[index];
        maxValue = amtDue[index];
        maxIndex = index;
       }
       
       swap(address[maxIndex], address[start]);
       swap(amtDue[maxIndex], amtDue[start]);
       cout << "The property with the largest tax due is : " << amtDue[maxValue] << "at " << address[maxValue] << endl;
      }
      
     }
    }

    Saturday, September 7, 2019 10:14 PM

All replies

  • For example, this declaration is for a function taking an array of string objects, an array of doubles and an int.

    void loadArrays(string[], double[], int);


    However, the loadArrays function definition is different. This definitions is for a  one string object,  one double and one int.  The compiler sees this as a different function from the one that was declared but not defined.

    void loadArrays(string address, double amtDue, int SIZE)
    {
     int count;
     for (count = 0; count < SIZE; count++)
      address[count]>>amtDue[count];
    }

    In the function body, the double parameter amtDue is treated as if it was an array, but it is not.  That is why the compiler complains about subscripts needing a pointer or array type.  The compiler doesn't complain about the address variable having a subscript because it interprets that as referring to a single character in the string object.

    To correct these types of errors conform the function definition with the function declarations which declare array types.

    Once you correct these types of errors the compiler will likely identify additional errors.


    • Edited by RLWA32 Sunday, September 8, 2019 12:48 AM
    Sunday, September 8, 2019 12:45 AM
  • Hi,

    Thank you for posting here.

    1,According to the code I agree with RLWA32,you should conform the function definition with the function declarations which declare array types.

    2,

    cout << "The property with the largest tax due is : " << amtDue[maxValue] << "at " << address[maxValue] << endl;

    The array subscript is a noninteger expression.

    3, In C and C++, local variables are not initialized by default. Uninitialized variables can contain any value, and their use leads to undefined behavior. You could initialize local variables when they are declared, or assign a value to them before they are used. You need to initialize "choice","number","value" before you use them.

    Best Regards,

    Jeanine Zhang

    Monday, September 9, 2019 3:23 AM
    Moderator
  • Hi,

    Have you got any updates? If your case has been solved, please help to mark answers. If not, just feel free to contact us.

    Your understanding and cooperation will be grateful.

    Best Regards,

    Jeanine Zhang

    Wednesday, September 11, 2019 8:22 AM
    Moderator