Solved

"Cannot add two pointers error"- program using 4 parrellel array's & reading from file.

Posted on 2004-05-02
1
987 Views
Last Modified: 2013-11-17
Hi, I have written this program and keep getting these two errors:
Compiling...
Lab14.cpp
C:\students\Lab14.cpp(91) : error C2110: cannot add two pointers
C:\students\Lab14.cpp(119) : error C2110: cannot add two pointers
Error executing cl.exe.

Lab14.exe - 2 error(s), 0 warning(s)

The errors show up on the same line-- average = (test1 + test2 + test3) / 3;  in the  searFor and displayClass functions.  

I have no clue on this one the program is as follows


//displays and averages students grades

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//function prototype
short loadArray(string [], int [], int [], int []);
char getSelection ();
void displayClass(string [], int [], int [], int []);
void searchFor (string [], int [], int [], int []);

void main()
{
      char selection = ' ';



      //declare arrays
      string name[7] = {""};
      int test1[7] = {0};
      int test2[7] = {0};
      int test3[7] = {0};

      selection = getSelection();
      while (selection != '0')
      {
            switch (selection)
            {
            case '1': displayClass(name, test1, test2, test3);
                  break;
            case '2': searchFor (name, test1, test2, test3);
                  break;
            default: cout << "Invalid selection" << endl;
            }
            selection = getSelection();
      
      }
      cout << "Program has ended, thank you, and goodbye!" << endl;

}

//*******************************************************************
short loadArray(string name[], int test1[], int test2[], int test3[])
{
      int x = 0;
      ifstream inFile;
      inFile.open("student.dat", ios::in);
      if (inFile.is_open())
      {
            while (x < 7 && !inFile.eof())
            {
                  inFile >> name[x];
                  inFile.ignore();
                  getline(inFile, name[x], '#');
                  inFile >> test1[x];
                  inFile >> test2[x];
                  inFile >> test3[x];
                  x = x + 1;
            }//end while
            inFile.close();

      }else
            cout << "Error in opening file." << endl;

      return 0;
}

//********************************************************************
char getSelection()
{
      char selection = ' ';
      system ("cls");
      cout <<"                              Student Menu                          " << endl;
      cout <<"1. Display class names, grades, and averages" << endl;
      cout <<"2. Search by student name" << endl;
      cout <<"0 to Exit" << endl;
      cin >> selection;
      cin.ignore();
      return selection;
} // end of getSelection function

//*********************************************************************
void displayClass(string name[], int test1[], int test2[], int test3[])
{
      int x = 0;
      float average = 0.0;

      average = (test1 + test2 + test3) / 3;

      while (x < 7)
      {
            cout << name[x] << "\t" << test1[x] << "\t" << test2[x] << "\t" <<test3[x] << "\t" << average << endl;
            x = x + 1;
      }//end while
      
} //end of displayClass function

//**********************************************************************
void searchFor(string name[], int test1[], int test2[], int test3[])
{
      float average = 0.0;
      string studentName = " ";
            
      cout << "Enter the student name. (1 to return to menu): ";
      getline(cin,studentName);

      while (studentName != "1" )
      {
            int x = 0;
            while (x < 7 && name[x] != studentName)
                  x = x + 1;
            //end while

            if (x < 7)
            {
                  average = (test1 + test2 + test3) / 3;
                  cout << name[x] << "\t" << test1[x] << "\t" << test2[x] << "\t" <<test3[x] << "\t" << average << endl;
            }
            else
                  cout << "Student name was not found" << endl;
            // end if
      }
      if (studentName == "1")
      cout << "Press Enter to return to the Menu";
      getSelection();

      
}// end searchFor function


I would greatly appreciate any insight you can give me!  Thanks!
0
Comment
Question by:SinSual
1 Comment
 
LVL 16

Accepted Solution

by:
George Tokas earned 500 total points
ID: 10972491
The errors you have is because at:
>>average = (test1 + test2 + test3) / 3;
you are adding 3 integers and divide them to get the average BUT
test1,2,3 are declared as arrays with 7 items(integer).
So:
to get the average you have to declare WHICH int from the array you want to add..
i.e.:
average = (test1[x] + test2[y] + test3[z]) / 3;
where x,y,z is the index in the array(0...6).

Regards,
gtokas.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question