Solved

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

Posted on 2004-05-02
1
988 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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 Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

734 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