GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Aug-2008, 09:22
bcompt143 bcompt143 is offline
Awaiting Email Confirmation
 
Join Date: Apr 2008
Posts: 22
bcompt143 is on a distinguished road
Angry

Urgent question


My C++ program is Turbo. When I write the following program my compiler display the following error, and I cant able to recognize my error. Please tell me some suggestion.



CPP / C++ / C Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define max 200

struct date {
	int day;
	int month;
	int year;
	};
struct school {
	char name[40];
	int rno;
	struct date dob;
	struct date doa;
	}student[max];
void output (school student, int n);
void sort (school student, int n);

int n;

void main ()
{
	int n;
	school student[max];
	cout<<"enter no of student"<<endl;
	cin>>n;
	for(int i=0; i<n; i++)
	{
	 cout<<"Name"<<endl;
	 cin>>student[i].name;
	 cout<<"roll number"<<endl;
	 cin>>student[i].rno;
	 cout<<"dob"<<endl;
	 cin>>student[i].dob.day;
	 cin>>student[i].dob.month;
	 cin>>student[i].dob.year;
	 cout<<"doa"<<endl;
	 cin>>student[i].doa.day;
	 cin>>student[i].doa.month;
	 cin>>student[i].doa.year;
	}
	cout<<"unsorted"<<endl;
	output(student, n);
	sort(student, n);
	cout<<"unsorted"<<endl;
	output(student, n);

	getch();
}
void output (school student[max], int n)
{
	cout<<"Name         rollnumber       dob     doa\n";
	cout<<"__________________________________________\n";
	for(int i=0; i<n; i++)
	{
	cout<<student[i].name<<'t';
	cout<<student[i].rno<<'t';
	cout<<student[i].dob.day<<'t';
	cout<<student[i].dob.month<<'t';
	cout<<student[i].dob.year<<'t';
	cout<<student[i].doa.day<<'t';
	cout<<student[i].doa.month<<'t';
	cout<<student[i].doa.year<<endl;
}
}
void sort(school student[max], int n)
{
	struct school temp;
	for(int i=0; i<n; i++)
	{
	 for(int j=i+1; j<n; j++)
	 {
	  if(strcmp(student[i].name, student[j].name) <=0)
	  {
	   temp=student[i];
	   student[i] = student[i];
	   student[j] = temp;
	  }
	 }
	}
}




The error are

Compiling SCHOOL.CPP:
Error SCHOOL.CPP 44: Cannot convert 'school *' to 'school' in function main()
Error SCHOOL.CPP 44: Type mismatch in parameter 'student' in call to 'output(school,int)' in function main()
Error SCHOOL.CPP 45: Cannot convert 'school *' to 'school' in function main()
Error SCHOOL.CPP 45: Type mismatch in parameter 'student' in call to 'sort(school,int)' in function main()
Error SCHOOL.CPP 47: Cannot convert 'school *' to 'school' in function main()
Error SCHOOL.CPP 47: Type mismatch in parameter 'student' in call to 'output(school,int)' in function main()
  #2  
Old 24-Aug-2008, 10:50
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Urgent question


Quote:
Originally Posted by bcompt143
The error are

Compiling SCHOOL.CPP:
Error SCHOOL.CPP 44: Cannot convert 'school *' to 'school' in function main()
Error SCHOOL.CPP 44: Type mismatch in parameter 'student' in call to 'output(school,int)' in function main()
Error SCHOOL.CPP 45: Cannot convert 'school *' to 'school' in function main()
Error SCHOOL.CPP 45: Type mismatch in parameter 'student' in call to 'sort(school,int)' in function main()
Error SCHOOL.CPP 47: Cannot convert 'school *' to 'school' in function main()
Error SCHOOL.CPP 47: Type mismatch in parameter 'student' in call to 'output(school,int)' in function main()
The error is telling you exactly what is the problem. Line 44 is the following:
CPP / C++ / C Code:
output(student, n);
...yet the prototype for function output() states:
CPP / C++ / C Code:
void output (school student, int n);
The error message points out that the function expects a school instance, but you are passing it an array of school. Given that the function itself treats the incoming argument as an array as well, change the prototype to the following:
CPP / C++ / C Code:
void output (school student[], int n);
...& make the corresponding change in the function's parameter list.
  #3  
Old 03-Sep-2008, 02:55
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Urgent question


Please learn array and pointer properly.
  #4  
Old 03-Sep-2008, 07:38
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 157
dlp has a spectacular aura about

Re: Urgent question


Part of your problem is your code is a little unclear with the number of variables sharing the same name. Let me show you something with a stripped down version of your code:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

const int MAX = 200;

struct date {
    int day;
    int month;
    int year;
};

struct school {
    char name[40];
    int rno;
    struct date dob;
    struct date doa;
} student[MAX];

int main()
{
    student[0].rno = 1;
    cout << student[0].rno << endl;
}
Now, I can reference student[0] without having written
CPP / C++ / C Code:
...
int main()
{
    school student[MAX];
...
due to the fact that you declare an array of school struct with this last bit of your struct definition:
CPP / C++ / C Code:
} student[MAX];
Now, when you have
CPP / C++ / C Code:
...
} student[MAX];

int main()
{
    school student[MAX];
...
Which array called student are you referring to throughout your program? Likewise, when you start saying:
CPP / C++ / C Code:
...
} student[MAX];

void output( school student, int n );
void sort( school student, int n );

int main()
{
    school student[MAX];
...
The compiler is going to be confused. For your functions, it's asking whether school student means a single variable of type school called student or is it saying that student means that array of school you created above is to be put in the function prototype?

Here's my question: Why have this line?
CPP / C++ / C Code:
} student[MAX];
at the end of your struct definition? What purpose does it serve? All it really is doing right now is confusing the compiler. Let's do this instead (replace any ... with your code):
CPP / C++ / C Code:
#include <iostream>
using namespace std;

const int MAX = 200;

struct date {
    int day;
    int month;
    int year;
};

struct school {
    char name[40];
    int rno;
    struct date dob;
    struct date doa;
}; // <- remove extra redefinition

void output( school student[], int n ); // <- use [] as ocicat suggested
void sort( school student[], int n );

// <- remove extra redefinition of int n as well

int main()
{
    int n;
    school student[MAX];

    ...
}

void output( school student[], int n ) // <- use [] as ocicat suggested
{
    ...
}

void sort( school student[], int n )
{
    ...
}
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please, This is Difficult question try for me! bcompt143 C++ Forum 4 24-Aug-2008 09:14
Urgent help regarding window service slogeshwaran Java Forum 2 14-Jul-2008 02:22
[GRAPHICS] - SymbolDesignerTool - URGENT HELP PLS metan Java Forum 0 15-Mar-2006 00:32
Urgent !question about read file(variable length) robin1977 C Programming Language 2 13-Mar-2005 17:01
question of practice magiccreative C++ Forum 1 06-Feb-2004 07:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:36.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.