I'm writing a command line based C++ program which will add some details to a struct array and which will retrieve them needed I'm only facing one compiler error which I don't know how to solve below is my full code.
// e.cpp : Defines the entry point for the console application.
/********************************************************************************
Crated by Sadaruwan Samaraweera
In behalf of New Eastern Campus
Coding Start Date 22nd April 2013
*********************************************************************************/
#include<iostream>
#include<string>
usingnamespace std;
//Struct Declaration for stdDetails
struct stdDetails {
int stdNum; //Student Registration Number
char fName[25]; // Student First Name
char lName[25]; // Student Last Name
string resAdd; //Resident Address
char dBirth[10]; // Student Date Of Birth
int stdAge; // Student Age
string nicNum; // Sudent National ID number
int conNum; // Student Contact Number
char dateEnroll[10]; // Student Enrollment date
char corsSelect[25]; // Course Selected
char payMethod; // Fee payment method
double amtPaid; // Amount Student Paid for the course
char bksIssue[25]; //Issued Books
};
//Declartion of methods usend in this program
void ui(); //Method for displaying the UI
void output(stdDetails stdDetailsStructs_1);
stdDetails enterDetails (stdDetails stdDetailsStructs_1); //Method for entering student details to the prgoram
void entLine(); //To make interface more nice
void stars(); //Draws a line of stars to make CLI look more nice
//Declaration of i as a Variable for the counter
int i=0;
//Start of main method
int main(){
ui();
return 0;
}
//CLI method start from here on
void ui (){
int choice;
stdDetails stdDetailsStructs_1;
stdDetails stdDetailsStructs_2[1000];
//char ansr = 'y';
char ansr;
int search = 0;
stars();
cout<<" Welcome to New Eastern Campus "<<endl;
cout<<endl;
stars();
entLine();
//Beginning of the while loop which will display the options that user have to enter
do{
cout<<" Press Number [1] for New Student Registration : "<<endl;
cout<<" Press Number [2] to Search Student Details : "<<endl;
cout<<" Press Number [3] to Delete Student Details : "<<endl;
cout<<" Press Number [4] to View Student Details: "<<endl;
entLine();
cout<<" Enter your choice here: ";
cin>>choice;
entLine();
//Start of the switch case for every choice selected will perform an action
switch(choice){
//This Case will add the details entered to the stdDetails array
case 1:
stdDetailsStructs_1 = enterDetails(stdDetailsStructs_1);
stdDetailsStructs_2[i]=stdDetailsStructs_1;
i++;
break;
//This Case will search the array for a given keyword
case 2:{
stdDetails stdDetailsStructs_0;
int kWord;
stdDetails stdFindRes;
cout<<"Enter the Student Registration Number of the Student: ";
cin>>kWord;
for(int x=0;x<i;x++){
stdFindRes = stdDetailsStructs_0[x];
if(kWord == stdFindRes.stdNum){
search=1;
break;
}
}
if(search==1){
output(stdFindRes);
}else{
cout<<"Student details not found please try again."<<endl;
search = 0;
}
}
break;
//This case will delete the students details form the array
case 3:{
search=0;
int delStd,m;
stdDetails stdDel;
cout<<"Enter the Registration Numeber of the student: ";
cin>>delStd;
for(m=0;m<i;m++){
stdDel = stdDetailsStructs_2[m];
if(delStd==stdDel.stdNum){
search=1;
break;
}
}
if(search==1){
for(int z=m;z<i;z++){
stdDetailsStructs_2[z]=stdDetailsStructs_2[z+1];
i--;
}
}else{
cout<<"Std not found"<<endl;
}
}
break;
//This case will display the student details
case 4:
cout<<"=================================="<<endl<<endl;;
for(int x=0;x<i;x++){
stdDetails stdView = stdDetailsStructs_2[x];
output(stdView);
cout<<endl;
cout<<"***********************************"<<endl;
cout<<endl;
}
cout<<"=================================="<<endl;
break;
//The default message if any thing goes wrong
default:
cout<<"Invalid Option Please Enter a Valid Entry"<<endl;
break;
}
//The option case list ends from here
entLine();
cout<<"Continue [y/n]: ";
cin>>ansr;
entLine();
}while(ansr == 'y'||ansr == 'Y');
//While Loop Ends here
cout<<"Thank You"<<endl;
}
//Ending of the UI system
//This method is used to decorate the interface
void entLine(){
cout<<endl;
cout<<endl;
}
//Method End
//Data Display Method Starts from here
void output(stdDetails stdDetailsStructs_1){
cout<<"Student Registration Number: "<<stdDetailsStructs_1.stdNum<<endl;
cout<<"Student First Name: "<<stdDetailsStructs_1.fName<<endl;
cout<<"Student Last Name: "<<stdDetailsStructs_1.lName<<endl;
cout<<"Student Resident Address: "<<stdDetailsStructs_1.resAdd<<endl;
cout<<"Student Date Of Birth: "<<stdDetailsStructs_1.dBirth<<endl;
cout<<"Student Age: "<<stdDetailsStructs_1.stdAge<<endl;
cout<<"Student National ID number: "<<stdDetailsStructs_1.nicNum<<endl;
cout<<"Student Contact Number: "<<stdDetailsStructs_1.conNum<<endl;
cout<<"Student Enrollment Date: "<<stdDetailsStructs_1.dateEnroll<<endl;
cout<<"Selected Course: "<<stdDetailsStructs_1.corsSelect<<endl;
cout<<"Payment Mthod: "<<stdDetailsStructs_1.payMethod<<endl;
cout<<"Paid Amount: "<<stdDetailsStructs_1.amtPaid<<endl;
cout<<"Books Issued: "<<stdDetailsStructs_1.bksIssue<<endl;
}
//Data Display Method Ends
//Data Entery Method start from here
stdDetails enterDetails (stdDetails stdDetailsStructs_1){
cout<<"Enter Student Registration Number : ";
cin>>stdDetailsStructs_1.stdNum;
cout<<"Enter Sutdent Firs Name : ";
cin>>stdDetailsStructs_1.fName;
cout<<"Enter Student Last Name : ";
cin>>stdDetailsStructs_1.lName;
cout<<"Enter Student Resident Address : ";
cin>>stdDetailsStructs_1.resAdd;
cout<<"Enter Student DoB : ";
cin>>stdDetailsStructs_1.dBirth;
cout<<"Enter Student Age : ";
cin>>stdDetailsStructs_1.stdAge;
cout<<"Enter Student NIC number : ";
cin>>stdDetailsStructs_1.nicNum;
cout<<"Enter Student Contact number : ";
cin>>stdDetailsStructs_1.conNum;
cout<<"Enter Student Enrollment Date : ";
cin>>stdDetailsStructs_1.dateEnroll;
cout<<"Selected Course : ";
cin>>stdDetailsStructs_1.corsSelect;
cout<<"Payment Method : ";
cin>>stdDetailsStructs_1.payMethod;
cout<<"Amount Paid : ";
cin>>stdDetailsStructs_1.amtPaid;
cout<<"Books Issued : ";
cin>>stdDetailsStructs_1.bksIssue;
return stdDetailsStructs_1;
}
//Data Entry Method End
//CLI Line of stars
void stars(){
for (int i=1; i<=50; i++) {
cout << "*";
}
}
This is the compiler error I'm getting
e:\users\sinux\dropbox\assigenments\pp assignment\program\vs2012\stdreg2\stdreg2\source.cpp(103): error C2676: binary '[' : 'stdDetails' does not define this operator or a conversion to a type acceptable to the predefined operator
This error is generated due to this part of the program
//This Case will search the array for a given keyword
case 2:{
stdDetails stdDetailsStructs_0;
int kWord;
stdDetails stdFindRes;
cout<<"Enter the Student Registration Number of the Student: ";
cin>>kWord;
for(int x=0;x<i;x++){
stdFindRes = stdDetailsStructs_0[x];
if(kWord == stdFindRes.stdNum){
search=1;
break;
}
}
if(search==1){
output(stdFindRes);
}else{
cout<<"Student details not found please try again."<<endl;
search = 0;
}
}
break;
Can some please help me to solve this this the only issue I have if this get solved I'm all good.
Thank you for your quick reply I'm fairly new to C++ programing can you help me to fix this please I searched all over the net to find a solution but got nothing in return.
You've declared stdDetailsStructs_0 as a single instance of a stdDetails object. As it's a single instance, you can't use the [] operator to access it - it isn't an array.
This will fix your compilation issue. Whether it offers the functionality you want is another matter. :-)
Well, the question is - what do you want stdDetailsStructs_0 to be? Do you want it to be a single structure? Or do you want it to be an array of structures? What's it supposed to be for?
I notice that at line 103, you're trying to use it without ever having initialised it. That won't end well.
I want it be searching data one by one in an array of data stored in struct array.
But there's nothing in the array to search. You're creating an array with:
stdDetails stdDetailsStructs_0[1000];
but you're not setting the values of any of the members of the array. The contents of that array are undefined. So when you copy an element of the array into a new structure with
stdFindRes = stdDetailsStructs_0[x];
the contents of stdFindRes will also be undefined.
Ok, I see so how can I correct this I'm mainly a PHP person C++ I just got started that's why I'm being such a noob please show me how to make this right
Well, at risk of stating the obvious, you need to actually set the values of the contents of that array. The thing is, I don't know what that array is supposed to contain - you do.
Did you really intend to create a new array here, and populate it? Or is this supposed to be searching an array that's been populated somewhere else in the code?