简体   繁体   中英

Pointers/References in C++ Project

I've been learning C++ for only a couple of months. We had a project that was due about pointers/reference. This is my code below that I submitted. However, My teacher said I didn't include pointers/references in my code. I was certain that I did on line 31-42. I'm having trouble understanding pointers. Please give feedback on what I should do differently.

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

int main()

{
  //Opening File
  ifstream cafefile;
  cafefile.open("cafeteria.txt");

  //Check for errors
  if (!cafefile)
  {
      cout << "cafeteria.txt did not open properly" << endl;
      exit(9999);
  }

//String & char 
  string choice;
  string array[4][3];
  char out;

  //Pointers to the food 
  string food[] { "Cheese Pizza", "Hamburger", "Fish Sticks", "Mystery Meat" };
  string *foodptr {food};

  //Set yes & no to 4 array 
  int yes[4]{ 0 };
  int no[4]{ 0 };

  //pointer for integer Yes / NO 
  int* yesptr{ yes };
  int* noptr{ no };


  //Counters of Likes & Dislikes set = 0 
  int like = {0};
  int dislike = {0};


  for (int i = 0; i < 4; i++)

  {
      //Reopen the file
      ifstream file("cafeteria.txt");

      while (getline(file, choice))

      {

          istringstream pick(choice); //istringstream is input stream class, which operates on string

          getline(pick, choice, '\t');
          if (foodptr[i] == choice)

          {

              pick >> choice;
              if (choice == "Y")

                  like++;     //increment

              else if (choice == "N")

                  dislike++;  //increment

          }
      }

      //Setting the yes = like 

      yesptr[i] = like;
      noptr[i] = dislike;

      //Set to 0
      like = 0;
      dislike = 0;
  }

  for (int m = 0; m < 4; m++)

  {
      array[m][0] = foodptr[m];
  }

  for (int j = 0; j < 4; j++)

  {
      array[j][1] = to_string(yesptr[j]);
  }

  for (int i = 0; i < 4; i++)

  {
      array[i][2] = to_string(noptr[i]);
  }

  //Display of the Program
  cout << "School Cafeteria Survey!" << endl; 
  cout << endl; 
  cout << "Food Item" << "\t" << "Like" << "\t" << "Dislike" << endl; //Gives the tab space Horizontally in your output 

  for (int i = 0; i < 4; i++)
  {
      for (int j = 0; j < 3; j++)
      {
          cout << array[i][j] << "\t";
      }
      cout << endl;
  }

  //Background color, making it unique
  system("color 6");

  //Exit the Prgram
  cout << endl; 
  cout << "Please enter 'E' to exit!";
  cin >> out; 
  if (out == 'e' || out == 'E')
  {
      exit(0);
  }

  system("pause");    //pause the program

  return 0;

}

Please provide solutions on what I should fix.

It would be better if you create your arrays dynamically ie make the 2D array dynamically using pointers

string **array;
array = new int *[4];
for(int i=0; i<4; i++){
    array[i] = new int[3];
}

Other than this, your teacher must have wanted your to access these array not using indexing, but rather by using pointers and references. This means that you shouldn't access arrays using yesptr[i] but rather by using *(yesptr + i) . So for eg if you are in a loop do the following

for (int m = 0; m < 4; m++)
{
    *(*(array+m)+0) = *(foodptr+m);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM