7 Replies - 1017 Views - Last Post: 30 November 2009 - 01:27 PM Rate Topic: -----

#1 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

C++ help with error in my menu option

Posted 30 November 2009 - 11:59 AM

Hey, I'm having some problems setting up my menu. I keep getting an error in the switch menu choice (which i marked) telling me that a expected primary-expression before toke "[" token.

 #include <iomanip>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

struct StudentRecord
{
	   char   lastName  [16];
	   char   firstName [16];
	   char   hometown  [16];
	   char   major[5];
	   int	studentNumber;
	   double balance;
	   int	earnedHours;
	   double gpa;
};

int main ()
{
	char textFilename[50];
	char binaryFilename[50];
	StudentRecord nextStudent;
	StudentRecord student[100];
	StudentRecord *ptr;
	int n;
	fstream  studentFile;
	ifstream textFile;
	char	 lastIn [16];
	char	 firstIn[16];
	char	 townIn [16];
	char	 majorIn[5];
	int	  numberIn;
	double   balanceIn;
	int	  hoursIn;
	double   gpaIn;
	bool menuActive = true;
	char menuChoice;
	while(menuActive)
		  {
			 displayMenu ();
			 cin >> menuChoice; cin.ignore(80,'\n');
			 cout << endl;
			 StudentRecord s;
			 switch(menuChoice)
			 {
				case '1': case 'P': case 'p':
					 printList (StudentRecord[], n); [b]// This is where the errors occur.[/b]
					 break;
				case '2': case 'A': case 'a':
					 ascending (StudentRecord[], n); [b]// This is where the errors occur.[/b]
					 break;
				case '3': case 'D': case 'd':
					 descendingGPA (StudentRecord[], n); [b]// This is where the errors occur.[/b]
					 break;
				case '4': case 'B': case 'b':
					 balanceThrehold (StudentRecord[], n); [b]// This is where the errors occur.[/b]
					 break;
				case '5': case 'M': case 'm':
					 majorAndGpaThreshold (StudentRecord[], n); [b]// This is where the errors occur.[/b]
					 break;
				case '6': case 'Q': case 'q':
							menuActive = false; 
							break;
				default:
						cout << " Invalid menu choice: try again!" << endl << endl;
			 }
	
	cout << "Enter filename of source text file: ";
	cin >> textFilename;
	cout << endl;
	textFile.open (textFilename);
	
	cout << "Enter filename of binary file of student records: ";
	cin >> binaryFilename;
	cout << endl;
	studentFile.open (binaryFilename, ios:: out | ios:: binary);
	n = 0;
	
	while (!textFile.eof())
	{
		  textFile >> lastIn >> firstIn >> townIn >> majorIn >> numberIn >> balanceIn >> hoursIn >> gpaIn;
		  if (!textFile.eof())
		  {
			   strcpy (nextStudent.lastName, lastIn);
			   strcpy (nextStudent.firstName, firstIn);
			   strcpy (nextStudent.hometown, townIn);
			   strcpy (nextStudent.major, majorIn);
			   nextStudent.studentNumber = numberIn;
			   nextStudent.balance = balanceIn;
			   nextStudent.earnedHours = hoursIn;
			   nextStudent.gpa = gpaIn;
			   studentFile.write (reinterpret_cast<char *>(&nextStudent), sizeof(nextStudent));
			   n++;
		  }
	}
	cout << "Records created in binary student file = " << n << endl;
	cout << endl;
	
	textFile.close();
	studentFile.close();
	studentFile.clear();
	
	studentFile.open (binaryFilename, ios:: in | ios:: binary);
	ptr = student;
	n = 0;
	
	while (!studentFile.eof())
	{
		  studentFile.read (reinterpret_cast<char *>(ptr), sizeof(nextStudent));
		  if (!studentFile.eof())
		  {
			 ptr++;
			 n++;
		  }
	}
	studentFile.close();
	
	cout << "Records read from binary student file = " << n << endl;
	cout << endl << endl;
	  return 0;
}


void printList (StudentRecord s[], int n)
{
	 for(int i=0; i < n; i++)
	 cout << setw(16) << left << student[i].lastName << "  " << student[i].firstName << endl;
	 cout << endl << endl;
	 if (i % 10 = 0) pause;
   }
} 

This post has been edited by react05: 30 November 2009 - 03:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ help with error in my menu option

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6257
  • View blog
  • Posts: 24,025
  • Joined: 23-August 08

Re: C++ help with error in my menu option

Posted 30 November 2009 - 12:07 PM

You're supposed to pass a VARIABLE to a function. What's the first argument to the function here?
printList (StudentRecord[], n);

Was This Post Helpful? 0
  • +
  • -

#3 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

Re: C++ help with error in my menu option

Posted 30 November 2009 - 12:17 PM

Oh ok. How would I pass a variable to a function?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6257
  • View blog
  • Posts: 24,025
  • Joined: 23-August 08

Re: C++ help with error in my menu option

Posted 30 November 2009 - 12:20 PM

How did they teach you to do it in school?
Was This Post Helpful? 0
  • +
  • -

#5 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: C++ help with error in my menu option

Posted 30 November 2009 - 12:29 PM

You're trying to pass an array into a function, which can be done. Do you want the array in the function, or one variable?

Here's an example of passing a variable to a function:
void quadratic (double, double, double);  //function prototype, indicate the variable type to be passed, here it is 3 doubles

main (){
	 double a=0, b=0, c=0;  //declare variables
	 //code here
	 quadratic(a, b, c);  //call the function, passing in a, b, and c
}

void quadratic(double a, double b, double c){  //include type and variable name

//code for whatever the function does, here
}


Here's an example of passing an array into a function:
void printArray(string strArray[], int length){ //pass array and length into function
	
	//code for whatever the function does	   

}

int main()
{	 
	// initialize array
	string strArray[]={"initialize","array"};

	//call function, pass array and its length size into function
	printArray(strArray, 5); 
}

Was This Post Helpful? 0
  • +
  • -

#6 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

Re: C++ help with error in my menu option

Posted 30 November 2009 - 12:35 PM

I think we need to pass the array to the function.

This post has been edited by react05: 30 November 2009 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#7 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: C++ help with error in my menu option

Posted 30 November 2009 - 01:20 PM

View Postreact05, on 30 Nov, 2009 - 01:35 PM, said:

I think we need to pass the array to the function. My professor told us that all of the functions we have to do for this assignment have the same arguement pattern which is
void functionName (StudentRecord s[], int n)


So that would be the function prototype (or definition) and then to pass the arguments into the function when you call it, you'd follow this pattern:
	//call function, pass array and its length size into function
	printArray(strArray, 5); 


Which is probably why you get an error at '[' . They are omitted when passing the array in. I understand why that is a mite confusing at first. It took me a while to figure it out, and I never would have if Amadeus hadn't helped me. :wub:
//ok, I probably would have, but it would have taken a lot longer
Was This Post Helpful? 0
  • +
  • -

#8 react05   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-September 09

Re: C++ help with error in my menu option

Posted 30 November 2009 - 01:27 PM

lol alright, i got it now. thanks for the help!!

This post has been edited by react05: 30 November 2009 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1