none
Data type issues? RRS feed

  • Question

  • I'm very new to c++ (it's my first programming language) and I keep getting error about pointers. The errors are:

    argument of type "double" is incompatible with parameter of type "double**" (line 21)

    argument of type "int" is incompatible with parameter of type "int**" (line 21)

    expression must have pointer-to-object-type (line 40)

    expression must have pointer-to-object-type (line 62)

    And here's the code:

    using namespace std;
    #include <iostream>
    double scores;
    int highIndex, average;

    int main()
    {
     //Assigns the values to the studentIds array.
     int studentIds[6] = { 1234,2333,4432,3323,2143,3425 };
     //Assigns values.
     inputAnswers(studentIds, scores);
     //Finds and displays average score.
     average = getAverage(scores);
     cout << "The average score of the students is" << average << endl;
     //Finds index of the highest scores value.
     getHighIndex(scores, studentIds);

     return 0;
    }
    //This function allows for the user to assign specific scores to the studentIds array.
    void inputAnswers(int studentIds[], double scores)
    {
     int index;
     index = -1;
     while (index < 5)
     {
      index = index + 1;
      cout << "Enter the score for student #" + studentIds[index] << endl;
      cin >> scores[index];
     }
    }
    //Averages out the values in the scores array and displays the average.
    double getAverage(double scores)
    {
     int sum, index, count;
     double average;
     sum = 0;
     index = -1;
     count = 0;
     while (count < 6);
     {
      index = index + 1;
      count = count + 1;
      sum = sum + scores[index];
     }
     average = (sum / 6);
    }
    int getHighIndex(double** scores, int** studentIds)
    {
     int highnumber, index, count, highIndex;
     highnumber = 0;
     index = -1;
     count = 0;
     highIndex = 1;
     while (count < 6);
     {
      count = count + 1;
      index = index + 1;
      if (scores[index] > scores[highIndex])
      {
       highIndex = index;
      }
      else
      {
       //Nothing will happen,
      }
     }
     cout << "The high score was student #" << studentIds[highIndex] << "with a " << scores[highIndex] << endl;
    }

    Tuesday, July 30, 2019 6:52 AM

All replies

  • Hello,

    you use the variable scores as a normal double variable and as an array.

    Implement the scores-variable as type array in the same way as you use the array students.

    If you need help with your array coding then, we will help you more. (I don't give you the correct source code because you should learn it by yourself.)

    Regards, Guido

    Edit: here is a tutorial about pointers: http://www.cplusplus.com/doc/tutorial/pointers/ 
    But by now, you only need an array.
    Tuesday, July 30, 2019 7:03 AM
  • Hello,

    Thank you for posting here.

    It seems that there are some other errors in your code. I correct them and you can try the code below.

    #include <iostream>
    using namespace std;
    
    
    
    //This function allows for the user to assign specific scores to the studentIds array.
    void inputAnswers(int studentIds[], double *scores)
    {
    	int index;
    	index = -1;
    	while (index < 5)
    	{
    		index = index + 1;
    		cout << "Enter the score for student #" + studentIds[index] << endl;
    		cin >> scores[index];
    	}
    	return;
    }
    //Averages out the values in the scores array and displays the average.
    double getAverage(double *scores)
    {
    	int sum, index;
    	double average;
    	sum = 0;
    	index = -1;
    	while (index < 5)
    	{
    		index = index + 1;
    		sum = sum + scores[index];
    	}
    	average = (sum / 6);
    	return  average;
    }
    int getHighIndex(double* scores, int* studentIds)
    {
    	int highnumber, index, count, highIndex;
    	highnumber = 0;
    	index = -1;
    	count = 0;
    	highIndex = 1;
    	while (count < 6)
    	{
    		count = count + 1;
    		index = index + 1;
    		if (scores[index] > scores[highIndex])
    		{
    			highIndex = index;
    		}
    	}
    	cout << "The high score was student #" << studentIds[highIndex] << "with a " << scores[highIndex] << endl;
    	return 0;
    }
    int main()
    {
    	double *scores = new double[6];
    	int highIndex, average;
    	//Assigns the values to the studentIds array.
    	int studentIds[6] = { 1234,2333,4432,3323,2143,3425 };
    	//Assigns values.
    	inputAnswers(studentIds, scores);
    	//Finds and displays average score.
    	average = getAverage(scores);
    	cout << "The average score of the students is" << average << endl;
    	//Finds index of the highest scores value.
    	getHighIndex(scores, studentIds);
    
    	return 0;
    }

    ERROR LIST:

    1. It doesn't need ; after while(), you can refer this link.

    https://docs.microsoft.com/en-us/cpp/cpp/while-statement-cpp?view=vs-2019

    2. The relationship between pointers and arrays: In some ways, you can think of a pointer as pointing to the first element of an array. So you don't need to use double **. It means that a pointer points to a double type pointer. In your case, using double * is enough. Like the code below:

    #include <iostream>
    using namespace std;
    
    int main()
    {
    	double *pointer;
    	double array1[10] = { 0,1,2,3,4,5,6,7,8,9 };
    	pointer = array1;
    	cout << pointer[6];
    	return 0;
    }

    this pointer (pointer) points to the first element of array1. if you make pointer equal to array1, you can just view pointer as array1. 

    Refer to the follow links to arrays and pointers for detailed instructions:

    https://docs.microsoft.com/en-us/cpp/cpp/arrays-cpp?view=vs-2019

    https://docs.microsoft.com/en-us/cpp/cpp/pointers-cpp?view=vs-2019Best Regards,

    Suarez Zhou



    Tuesday, July 30, 2019 8:58 AM
  • Hello,

    Thank you for posting here.

    It seems that there are some other errors in your code. I correct them and you can try the code below.

    #include <iostream>
    using namespace std;
    
    
    
    //This function allows for the user to assign specific scores to the studentIds array.
    void inputAnswers(int studentIds[], double *scores)
    {
    	int index;
    	index = -1;
    	while (index < 5)

    You would be better served using the common idiom

         for(index = 0; index < 6; index++)

    and deleting the next assignment statement.

    	{
    		index = index + 1;
    		cout << "Enter the score for student #" + studentIds[index] << endl;
    		cin >> scores[index];
    	}
    	return;
    }
    //Averages out the values in the scores array and displays the average.
    double getAverage(double *scores)
    {
    	int sum, index;
    	double average;
    	sum = 0;
    	index = -1;
    	while (index < 5)

    The same here.

    	{
    		index = index + 1;
    		sum = sum + scores[index];
    	}
    	average = (sum / 6);
    	return  average;
    }
    int getHighIndex(double* scores, int* studentIds)
    {
    	int highnumber, index, count, highIndex;
    	highnumber = 0;
    	index = -1;
    	count = 0;

    If you use the common idiom as above, count is not needed.

    	highIndex = 1;
    	while (count < 6)
    	{
    		count = count + 1;
    		index = index + 1;
    		if (scores[index] > scores[highIndex])
    		{
    			highIndex = index;
    		}
    	}
    	cout << "The high score was student #" << studentIds[highIndex] << "with a " << scores[highIndex] << endl;

    This statement belongs in main.  After this function determines the index of the high score, what to do with that index is determined by the calling function.  You should handle this the same way you handle average.

    	return 0;
    }
    int main()
    {
    	double *scores = new double[6];

    Is there a reason you made scores a pointer and dynamically allocated memory rather than simply defining an array as you do for studentIds below?

    	int highIndex, average;
    	//Assigns the values to the studentIds array.
    	int studentIds[6] = { 1234,2333,4432,3323,2143,3425 };
    	//Assigns values.
    	inputAnswers(studentIds, scores);
    	//Finds and displays average score.
    	average = getAverage(scores);
    	cout << "The average score of the students is" << average << endl;
    	//Finds index of the highest scores value.
    	getHighIndex(scores, studentIds);
    
    	return 0;
    }

    ERROR LIST:

    1. It doesn't need ; after while(), you can refer this link.

    https://docs.microsoft.com/en-us/cpp/cpp/while-statement-cpp?view=vs-2019

    2. The relationship between pointers and arrays: In some ways, you can think of a pointer as pointing to the first element of an array. So you don't need to use double **. It means that a pointer points to a double type pointer. In your case, using double * is enough. Like the code below:

    #include <iostream>
    using namespace std;
    
    int main()
    {
    	double *pointer;
    	double array1[10] = { 0,1,2,3,4,5,6,7,8,9 };
    	pointer = array1;
    	cout << pointer[6];
    	return 0;
    }

    this pointer (pointer) points to the first element of array1. if you make pointer equal to array1, you can just view pointer as array1. 

    This is terrible advice.  There is a very definite relationship between arrays and pointers, especially when passing an array to a function, but in no way should you consider a pointer to be the same as an array in the general sense nor should you make unwarranted assumptions about where a pointer points to.

    Refer to the follow links to arrays and pointers for detailed instructions:

    https://docs.microsoft.com/en-us/cpp/cpp/arrays-cpp?view=vs-2019

    https://docs.microsoft.com/en-us/cpp/cpp/pointers-cpp?view=vs-2019Best Regards,

    Suarez Zhou

    Tuesday, July 30, 2019 6:31 PM
  • So I've made all of the changes that you guys told me to make, but now I'm getting a few error messages. They say "'inputAnswers': identifier not found", "'getAverage': identifier not found", and "'=': conversion from 'double' to 'int', possible loss of data". How do I define my functions?
    Tuesday, July 30, 2019 7:11 PM
  •  now I'm getting a few error messages. They say "'inputAnswers': identifier not found", "'getAverage': identifier not found", 

    The compiler doesn't know what those functions are, since you have not declared
    or defined them before trying to call them. When you have the definition of a
    function *after* main (or any other function that invokes the function) you need
    to provide a function prototype (signature) before calling the function.

    Typically such function prototypes (or forward declarations) are placed just 
    after the #include statements.

    - Wayne

    Tuesday, July 30, 2019 9:24 PM
  •  "'=': conversion from 'double' to 'int', possible loss of data". 

    So fix the definitions of your variables so they are of the right type.

    Foe example: "average" probably should be declared as a double not an int as 
    you have it. The same with the variable "sum".

    For specific corrections you need to post the *exact* code as you now have it
    after making changes to what you posted originally. Don't make us guess what
    you may have done.

    - Wayne

    Tuesday, July 30, 2019 9:31 PM
  • Here's what I have as of this moment:

    using namespace std;
    #include <iostream>
    int highIndex;
    double average;

    int main()
    {
     double* scores = new double[6];
     //Assigns the values to the studentIds array.
     int studentIds[6] = { 1234,2333,4432,3323,2143,3425 };
     //Assigns values.
     inputAnswers(studentIds, scores);
     //Finds and displays average score.
     average = getAverage(scores);
     cout << "The average score of the students is" << average << endl;
     //Finds index of the highest scores value.
     getHighIndex(scores, studentIds);
     cout << "The high score was student #" << studentIds[highIndex] << "with a " << scores[highIndex] << endl;
     return 0;
    }
    //This function allows for the user to assign specific scores to the studentIds array.
    void inputAnswers(int studentIds[], double *scores)
    {
     int index;
     index = -1;
     for (index = 0; index < 6; index++)
     {
      cout << "Enter the score for student #" + studentIds[index] << endl;
      cin >> scores[index];
     }
     return;
    }
    //Averages out the values in the scores array and displays the average.
    double getAverage(double *scores)
    {
     int index;
     double average, sum;
     sum = 0;
     index = -1;
     for (index = 0; index < 5; index++);
     {
      sum = sum + scores[index];
     }
     average = (sum / 6);
     return average;
    }
    int getHighIndex(double* scores, int* studentIds)
    {
     int highnumber, index, highIndex;
     highnumber = 0;
     index = -1;
     highIndex = 1;
     for (index = 0; index < 5; index++);
     {
      index = index + 1;
      if (scores[index] > scores[highIndex])
      {
       highIndex = index;
      }
      else
      {
       //Nothing will happen,
      }
     }
    }


    Tuesday, July 30, 2019 9:55 PM
  • Here's what I have as of this moment:

    using namespace std;
    #include <iostream>
    int highIndex;
    double average;

    int main()
    {
     

    using namespace std;
    #include <iostream>
    int highIndex;
    double average;
    
    void inputAnswers(int studentIds[], double *scores);
    double getAverage(double *scores);
    int getHighIndex(double* scores, int* studentIds);
    
    int main()
    {
    

    - Wayne

    Tuesday, July 30, 2019 10:07 PM
  • Now it says ""CL.exe" exited with code 2"

    This is the code right now:

    using namespace std;
    #include <iostream>
    int highIndex;
    double average;
    void inputAnswers(int studentIds[], double* scores);
    double getAverage(double* scores);
    int getHighIndex(double* scores, int* studentIds);

    int main()
    {
     double* scores = new double[6];
     //Assigns the values to the studentIds array.
     int studentIds[6] = { 1234,2333,4432,3323,2143,3425 };
     //Assigns values.
     inputAnswers(studentIds, scores);
     //Finds and displays average score.
     average = getAverage(scores);
     cout << "The average score of the students is" << average << endl;
     //Finds index of the highest scores value.
     getHighIndex(scores, studentIds);
     cout << "The high score was student #" << studentIds[highIndex] << "with a " << scores[highIndex] << endl;
     return 0;
    }
    //This function allows for the user to assign specific scores to the studentIds array.
    void inputAnswers(int studentIds[], double *scores)
    {
     int index;
     index = -1;
     for (index = 0; index < 6; index++)
     {
      cout << "Enter the score for student #" << studentIds[index] << endl;
      cin >> scores[index];
     }
     return;
    }
    //Averages out the values in the scores array and displays the average.
    double getAverage(double *scores)
    {
     int index;
     double average, sum;
     sum = 0;
     index = -1;
     for (index = 0; index < 5; index++);
     {
      sum = sum + scores[index];
     }
     average = (sum / 6);
     return average;
    }
    int getHighIndex(double* scores, int* studentIds)
    {
     int highnumber, index, highIndex;
     highnumber = 0;
     index = -1;
     highIndex = 1;
     for (index = 0; index < 5; index++);
     {
      index = index + 1;
      if (scores[index] > scores[highIndex])
      {
       highIndex = index;
      }
      else
      {
       //Nothing will happen,
      }
     }
    }

    Tuesday, July 30, 2019 10:42 PM
  • Now it says ""CL.exe" exited with code 2"

    This is the code right now:


    int getHighIndex(double* scores, int* studentIds)
    {
     int highnumber, index, highIndex;
     highnumber = 0;
     index = -1;
     highIndex = 1;
     for (index = 0; index < 5; index++);
     {
      index = index + 1;
      if (scores[index] > scores[highIndex])
      {
       highIndex = index;
      }
      else
      {
       //Nothing will happen,
      }
     }
    }

    Which compiler are you using? Which version?

    I would expect you to get an error because the function getHighIndex doesn't
    return a value when it ends. It is defined as having a return of type int.
    So why aren't you returning an int from that function? *Think* about the code
    you write. This error is pretty well self-explanatory.

    - Wayne

    Tuesday, July 30, 2019 11:53 PM
  • Now it says ""CL.exe" exited with code 2"

    This is the code right now:

    using namespace std;
    #include <iostream>
    int highIndex;
    double average;
    void inputAnswers(int studentIds[], double* scores);
    double getAverage(double* scores);
    int getHighIndex(double* scores, int* studentIds);

    int main()
    {
    Using global (file scope) variables like this is a bad habit which you should unlearn as quickly as possible.
    Wednesday, July 31, 2019 6:27 AM