Solved

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

Posted on 2004-05-02
1
975 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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now