Thread: Dealing with functions and reading char

  1. #1
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    Dealing with functions and reading char

    Hi , i am required to use stupid visual c++. somehow there is a bug with reading strings, and im am not allowed to fix it.

    so i have to use char instead . .

    this is part of of the codes:

    Code:
    //function prototypes
    char read_course_name(char* course_name);
    
    void main(void)
    {
    	
        char course_name[80];
        read_course_name(course_name);
    }
    
    char read_course_name(char* course_name)
    {
    	
    	
    	cout<< setw(50);
    	cout<<"Course and Subject Entry"; 
    	cout<< endl;
    	cout<< endl;
    	cout<<"\tCourse ==> ";
    	
    
    	cin.getline(course_name, 80);
    
    	return course_name;
    }
    im getting two errors:
    error C2440: 'return' : cannot convert from 'char *' to 'char'
    error C2664: 'read_course_name' : cannot convert parameter 1 from 'char [80]' to 'char &'

    how do i write a function to read an array of characters? the funny thing is ,it ran perfectly using g++ , argghhh
    Last edited by hermit; 06-12-2002 at 12:23 AM.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The function read_course_name returns a character pointer.

    char *read_course_name(char* course_name);

  3. #3
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    Monster: thanking you for that but i get a memory error when i try to pass the course name somewhere else . .

    it says something with hex-number and memory could not be written on the alert box

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Not related to this thread in any way...

    Hermit, are you a professional programmer?

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by hermit
    Monster: thanking you for that but i get a memory error when i try to pass the course name somewhere else . .

    it says something with hex-number and memory could not be written on the alert box
    You are probably writing data to memory that is not allocated.
    Post the code where you get the memory error...

    B.t.w. void main is wrong, use int main

  6. #6
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    here is the entire code

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <string>
    
    // function prototypes
    void SortByGPAs( 			// Sort arrays by GPAs
    	int NextIndex,       // IN: First unused position in arrays
    	int StudIds[],			// IN/OUT: array of student nos - to be sorted
    	int SumOfGrades[],	// IN/OUT: array of sums of grades - to be sorted
    	int NumSubjs[]);
    
    void Swap(					// Swap the two values
    	int &Candidate1,
    	int &Candidate2);
    
    void SimpleMindedClrScr(void);// Clear the screen
    
    bool MoreSubjects(void);		// Prompt user for more subjects Y/N?
    
    // routines from Study Guide
    void ReadInteger(int &Integer);	// function definition
    void Clear(void);			
    void ReadChar(char &Char); 	
    
    //my prototypes
    char *read_course_name(char* course_name);
    void print_welcome(void);
    void read_subjects(char* course_name);
    void read_subject(char* course_name);
    void print_grade_distribution(
    	char* course_name,
    	char* subject_name, 
    	int A, 
    	int B, 
    	int C, 
    	int D);
    void check_student_id();
    
    const int MIN_STUDENT_ID = 1000;
    const int MAX_STUDENT_ID = 9999;
    const int SUBJ_LEN = 7;
    
    int StudIds[MAX_STUDENT_ID - MIN_STUDENT_ID];
    int NumSubjs[MAX_STUDENT_ID - MIN_STUDENT_ID];
    int SumOfGrades[MAX_STUDENT_ID - MIN_STUDENT_ID];
    int next_free = 0;
    
    //function implementations
    
    void main(void)
    {
    	
        char course_name[80];
    
    
    	print_welcome();
    	read_course_name(course_name);
    	read_subjects(course_name);
    	
    	
    	//print_order_of_merit(course_name);
    }
    
    void print_welcome(void)
    {
    	SimpleMindedClrScr();
    
    	cout<<setw(50);
    	cout<<"Course Profiling System\n"; 
    	cout<<endl;
    	cout<<endl;
    	cout<<"\t Generates a grade distribution and GPA for each subject and";
    	cout<<endl;
    	cout<<"\tstudent GPAs with recommendations for commendations & exclusion.\n";
    	cout<<endl;
    	cout<<endl;
    }
    
    char *read_course_name(char* course_name)
    {
    	
    
    	cout<< setw(50);
    	cout<<"Course and Subject Entry"; 
    	cout<< endl;
    	cout<< endl;
    	cout<<"\tCourse ==> ";
    	
    
    	cin.getline(course_name, 80);
    
    	return course_name;
    }
    
    void read_subjects(char* course_name)
    {
    	do
    	{
    		read_subject(course_name);
    	} while (MoreSubjects());
    
    }
    
    void read_subject(char* course_name)
    {
    	char* subject_name;
    	int no_of_A = 0;
    	int no_of_B = 0;
    	int no_of_C = 0;
    	int no_of_D = 0;
    
    
    	cout<<endl;
    	cout<< "\tSubject ==>";
    	cin >> setw(SUBJ_LEN) >> subject_name;
    
    	SimpleMindedClrScr();
    	cout<<"\t\t";
    	cout<<course_name;
    	cout<<"\t";
    	cout<<subject_name;
    	cout<<endl;
    	cout<<endl;
    	cout<<setw(40);
    	cout<<"Student Grade Entry" << endl;
    	cout<< endl;		
       
    	while (1)
    	{
    		int student_id;
    		char grade;
    		
    		cout<<"\t Student Number (0 to exit) ==> ";
    		Clear();
    		check_student_id();
    		ReadInteger(student_id);
    
    		// Break out of the loop when a 0 student id is entered
    		if (student_id == 0)
    			break;
    		
    		cout<<endl;
    		cout<<"\t Grade (A,B,C or D) ==> ";
    		ReadChar(grade);
    
    		// Search for student id in array
    		int i;
    		bool found = false;
    		for (i=0; i < next_free && !found; i++)
    		{
    			if (StudIds[i] == student_id)
    				found = true;
    		}
    
    		if (found)
    		{
    			// This student id has been used before
    			NumSubjs[i]++;
    			SumOfGrades[i] +=
    				static_cast<int>('C') - static_cast<int>(grade) + 1;
    		}
    		else
    		{
    			// This student id is new
    			StudIds[next_free] = student_id;
    			NumSubjs[next_free] = 1;
    			SumOfGrades[next_free] =
    				static_cast<int>('C') - static_cast<int>(grade) + 1;
    		}
    
    		// Increment the count of different grades
    		switch (grade)
    		{
    		case 'A': no_of_A++; break;
    		case 'B': no_of_B++; break;
    		case 'C': no_of_C++; break;
    		case 'D': no_of_D++; break;
    		}
    
    	} // Loop and read another student_id/grade
    
    	print_grade_distribution(course_name, subject_name, no_of_A, no_of_B, no_of_C, no_of_D);
    }
    
    void check_student_id()
    {
    
    	while(StudIds==false)
    	{
    
    		cout<<"ERROR:Must in range 1000....9999 (0 to exit)";
    		cout<<endl;
    		cout<<endl;
    		cout<<"\t Student Number (0 to exit) ==> ";
    
    	}
    }
    
    void print_grade_distribution(
    	char* course_name, char* subject_name, int A, int B, int C, int D)
    {
    	int total = A + B + C + D;
    	int gpa = ((3 * A) + (2 * B) + (1 * C) + (0 * D)) / total;
    
    	cout << course_name << " " << subject_name << endl;
    	cout << endl;
    	cout << "Grade Distribution" << endl;
    	cout << endl;
    	cout << "A  B  C  D   Total" << endl;
    	cout << A << " " << B << " " << C << " " << D << " ";
    
    	cout << A + B + C + D << endl;
    	cout << endl;
    
    	cout << "Subject GPA: " << gpa << endl;
    	cout << endl;
    }
    
    
    
    
    /****************************************************************************
     SortByGPAs
     Process:				Sort arrays in descending order by GPA - insertion sort
     							When GPAs are equal, sort in descending order of NumSubjs 
     input:  				int NextIndex: First unused position in arrays
     						   int StudIds[]: array of student nos - to be sorted
     						   int SumOfGrades[]: array of sums of grades - to be sorted 
     						   int NumSubjs[]: array of number of subjects - to be sorted
     output:  				int StudIds[]: array of student nos - sorted
     						   int SumOfGrades[]: array of sums of grades - sorted 
     						   int NumSubjs[]: array of number of subjects - sorted	 
     ****************************************************************************/
    void SortByGPAs( 			// Sort arrays by GPAs
    	int NextIndex,       // IN: First unused position in arrays
    	int StudIds[],			// IN/OUT: array of student nos - to be sorted
    	int SumOfGrades[],	// IN/OUT: array of sums of grades - to be sorted
    	int NumSubjs[])
    {  
    	int
    		GPABy100,         // GPA multiplied by 100 as an int
    		GPABy100Prev;     //     "         "
    	
    	// NextIndex is first unused position in arrays
    	for (int i = 1; i < NextIndex; i++)
    	{  
    		// Sort into descending GPA order - when GPAs are equal sort 
    		// into descending number-of-subjects order
    		// To avoid floating-point comparisons, multiply GPAs by 100 and
    		// do integer comparisons
    	 	for (int j = i; j > 0 && 
    	 	  (GPABy100 = SumOfGrades[j] * 100 / NumSubjs[j]) >= 
    	 	  (GPABy100Prev = SumOfGrades[j-1] * 100 / NumSubjs[j-1]); j--)
    	 	{  
    	 		// Swap elements if:  1) GPABy100 is greater
    	 		//							 2) GPABy100s are equal and NumSubjs greater
    	 		if (GPABy100 > GPABy100Prev || (GPABy100 == GPABy100Prev && 
    	 									 NumSubjs[j] > NumSubjs[j-1]))
    	 		{  // swap elements in all arrays
    		 		Swap(StudIds[j], StudIds[j-1]);    
    		 		Swap(NumSubjs[j], NumSubjs[j-1]); 	
    		 		Swap(SumOfGrades[j], SumOfGrades[j-1]);			   
    		 	}
    	 	}
    	}	
    }
    
    /****************************************************************************
     Swap
     Process:				Swap 2 integer values
     input:  				int &Candidate1: value to be swapped
    							int &Candidate2: value to be swapped
     output:  				int &Candidate1: value swapped - old Candidate2
    							int &Candidate2: value swapped - old Candidate1	 
     ****************************************************************************/
    void Swap(
    	int &Candidate1,
    	int &Candidate2)
    {
     	int TmpSwap;
     	
     	TmpSwap = Candidate1;
     	Candidate1 = Candidate2;
     	Candidate2 = TmpSwap;
    }
    
    /****************************************************************************
     MoreSubjects
     Process:				Determine if the user wants to continue. Validate
     							respones (Y/y/N/n valid)
     on entry:				
     on exit:										 							
     input:  							
     output: 				bool function return value: true to continue, false to exit
     ****************************************************************************/
    bool MoreSubjects(void)
    {  
    	char 
    		More = ' ';
    		
    	// loop until user enters Y/y or N/n, printing 
    	// error msgs for invalid responses
       while (More != 'Y' && More != 'N') {
    	   cout << "Another Subject? "; 
    		ReadChar(More);
    	   cout << endl;   
    		More = toupper(More);       // change to upper case
    	   cout << endl; 
    	     
    	   // output error message if response invalid
    	   if (More != 'Y' && More != 'N')
    	   	cout << "ERROR: Valid responses are 'Y' or 'N'" << endl << endl;
    	} // end while
     	
     	// return user response
    	return (More == 'Y');
    }  
    
    /****************************************************************************
     SimpleMindedClrScr
     Process:				Clear the screen.
     input:  					 								
     output: 					
     ****************************************************************************/
    void SimpleMindedClrScr(void)
    {
    	const int
    		SCREEN_LINES = 24;	
    	 
     	for (int i = 0; i < SCREEN_LINES; i++)
     		cout << endl;
    }
     
    /****************************************************************************
     Clear
     process:				Clear standard input to end of line
     input:  					 								
     output: 			
     ****************************************************************************/
    void Clear(void)			
    {
    	char Char = ' ';
    	while (Char != '\n')
    	{
    		cin.get(Char);
    	}
    }
     
    /****************************************************************************
     ReadChar
     process:				Read a character value from standard input
     input:  					 								
     output: 				int Integer: the value read
     ****************************************************************************/
    void ReadChar(char &Char) 	
    {
    	cin >> Char;
    	Clear(); 					// clear to end of line
    }
    
    /****************************************************************************
     ReadInteger
     process:				Read (and validate) an integer value from standard input
     input:  					 								
     output: 				int Integer: the value read
     ****************************************************************************/
    void ReadInteger(int &Integer)	// function definition
    {
    	cin >> Integer;
    	while (cin.fail())
    	{
    		cin.clear(); 					// clear input stream flags
    		Clear();							// clear to end of line
    		cout << "ERROR: You must enter an integer ==> ";
    		cin >> Integer;				// try again
    	}
    	Clear(); 							// clear to end of line
    }

    Facemaster: nah, im just a beginner . .

  7. #7
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    dont worry about the #include <string>

    it is an error from the microsoft C++ student edition

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    In your function read_subject you are trying to store the subject name in the variable subject_name.
    But the subject_name var is a char pointer an there is no memory allocated to store the value.
    Chage it to: char subject_name[80];

    Also check the total var in your function print_grade_distribution.
    You can get a 'divide by zero' error when total is zero.

  9. #9
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    can u please explain further on the print_grade function?

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Okay, when I run your program and stop at the Student number prompt (enter 0), the print_grade_distribution is called.
    Because no grade is filled in the next formula will generate an 'Integer divide by zero' error (because total is zero):
    Code:
    int gpa = ((3 * A) + (2 * B) + (1 * C) + (0 * D)) / total;

  11. #11
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    so what do i do ?

  12. #12
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Only perform the formula when total is not zero.

    Code:
      int gpa = 0;
    	
      if(total)
        gpa = ((3 * A) + (2 * B) + (1 * C) + (0 * D)) / total;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. How do reading functions mark an end?
    By albert3721 in forum C Programming
    Replies: 11
    Last Post: 05-13-2007, 08:03 AM
  3. Signed Char Overflow
    By coder8137 in forum C Programming
    Replies: 5
    Last Post: 11-17-2006, 08:25 AM
  4. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM