Thread: char problem

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    45

    char problem

    ok, i worked a while on this program and worked out all of the bugs myself except this one error i keep getting saying

    error C2440: '=' : cannot convert from 'char [2]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast


    why does it say that when it is only 1 letter i'm trying to put in the variable...and i even tried actually putting grade[1], and it didnt do a thing.

    Code:
    #include<iostream.h>
    
    class StudentGrade
    {
    	private:
    		int idNum;
    		char name[20];
    		int testScore;
    		int possible;
    		char grade;
    	public:
    		StudentGrade(const int id, const char lname[], const int score, const int possPoints);
    		StudentGrade(const int id, const char lname[], const int score);
    		void displayStudentGrade();
    		void findGrade();
    };
    
    StudentGrade::StudentGrade(const int id, const char lname[], const int score, const int possPoints)
    {
    	idNum=id;
    	findGrade();
    	
    }
    StudentGrade::StudentGrade(const int id, const char lname[], const int score)
    {
    	idNum=100;
    	findGrade();
    }
    void StudentGrade::displayStudentGrade()
    {
    	cout<<"Student #"<<idNum<<" "<<name<<" earned "<<testScore<<" out of "<<possible<<".  Letter grade is "<<grade<<endl;
    };
    void StudentGrade::findGrade()
    {
    	if(testScore/possible>=90)
    		grade="A";
    	else if(testScore/possible>=80 && testScore/possible<90)
    		grade="B";
    	else if(testScore/possible>=70 && testScore/possible<80)
    		grade="C";
    	else if(testScore/possible>=60 && testScore/possible<70)
    		grade="D";
    	else
    		grade="F";
    };
    void main()
    
    {
    
    	StudentGrade a(111,"McDevitt",98);
    	StudentGrade b(222,"Zimmerman",85);
    	StudentGrade c(333,"Feuger",70);
    	StudentGrade d(444,"Tripp",69);
    	StudentGrade f(555,"Baier",50);
    	StudentGrade a1(666,"Pfeiffer",237,250);
    	StudentGrade b1(777,"Faris",245,300);
    	StudentGrade c1(888,"Heisler",700,1000);
    	StudentGrade d1(999,"Cassidy",63,105);
    	StudentGrade f1(1000,"Freund",2,200);
    
    	a.displayStudentGrade();
    	b.displayStudentGrade();
    	c.displayStudentGrade();
    	d.displayStudentGrade();
    	f.displayStudentGrade();
    	a1.displayStudentGrade();
    	b1.displayStudentGrade();
    	c1.displayStudentGrade();
    	d1.displayStudentGrade();
    	f1.displayStudentGrade();
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    This is a string:
    "A"
    This is a char:
    'A'

    gg

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    >.< tons of friggin logic errors now. i'll figure it out. thanks codeplug.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    "A" is two characters in a C-style string. The second character is the null character, '\0'.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    why is my output comming out like it is? where the vairables should have been, are either really big or small numbers, or random characters.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    Code:
    StudentGrade::StudentGrade(const int id, const char lname[], const int score, const int possPoints)
    {
    	idNum=id;
    	findGrade();
    	
    }
    Not quite sure but don;t you wanna do something with the other variables? like set name = lname, or testScore = score? if you don;t then make sure you set them all equal to 0, that should get rid of your random nums and chars.

    P.S. The random chars and nums are 'junk' in the memory allocated for the variables, if you initialize em you might get nicer junk =]

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You might want to set "name", "testScore" and "possible" in your constructors.
    Use strcpy() to set the name;

    gg

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    i forgot i changed the code.
    Code:
    #include<iostream.h>
    
    class StudentGrade
    {
    	private:
    		int idNum;
    		char name[20];
    		double testScore;
    		double possible;
    		char grade;
    	public:
    		StudentGrade(const int id, const char lname[], const double score, const double possPoints);
    		StudentGrade(const int id, const char lname[], const double score);
    		void displayStudentGrade();
    		void findGrade();
    };
    
    StudentGrade::StudentGrade(const int id, const char lname[], const double score, const double possPoints)
    {
    	possible=possPoints;
    	findGrade();
    	
    }
    StudentGrade::StudentGrade(const int id, const char lname[], const double score)
    {
    	possible=100;
    	findGrade();
    }
    void StudentGrade::displayStudentGrade()
    {
    	cout<<"Student #"<<idNum<<" "<<name<<" earned "<<testScore<<" out of "<<possible<<".  Letter grade is "<<grade<<endl;
    };
    void StudentGrade::findGrade()
    {
    	if(testScore/possible>=.90)
    		grade='A';
    	else if(testScore/possible>=.80 && testScore/possible<.90)
    		grade='B';
    	else if(testScore/possible>=.70 && testScore/possible<.80)
    		grade='C';
    	else if(testScore/possible>=.60 && testScore/possible<.70)
    		grade='D';
    	else
    		grade='F';
    };
    void main()
    
    {
    
    	StudentGrade a(111,"McDevitt",98);
    	StudentGrade b(222,"Zimmerman",85);
    	StudentGrade c(333,"Feuger",70);
    	StudentGrade d(444,"Tripp",69);
    	StudentGrade f(555,"Baier",50);
    	StudentGrade a1(666,"Pfeiffer",237,250);
    	StudentGrade b1(777,"Faris",245,300);
    	StudentGrade c1(888,"Heisler",700,1000);
    	StudentGrade d1(999,"Cassidy",63,105);
    	StudentGrade f1(1000,"Freund",2,200);
    
    	a.displayStudentGrade();
    	b.displayStudentGrade();
    	c.displayStudentGrade();
    	d.displayStudentGrade();
    	f.displayStudentGrade();
    	a1.displayStudentGrade();
    	b1.displayStudentGrade();
    	c1.displayStudentGrade();
    	d1.displayStudentGrade();
    	f1.displayStudentGrade();
    }

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    ok, i see what i did wrong, thanks guys.

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    ehhhh..... no try something like this:

    Code:
    StudentGrade::StudentGrade(const int id, const char lname[], const double score, const double possPoints)
    {
    	possible=possPoints;
                    idNum = id;
    	strcpy(name, lname); //Not sure about this you might 
                                                      //wanna look the exact syntax up.
    	testScore = score;
    	findGrade();
    	
    }
    StudentGrade::StudentGrade(const int id, const char lname[], const double score)
    {
    	possible=100;
                    idNum = id;
    	strcpy(name, lname); //Not sure about this you might 
                                                      //wanna look the exact syntax up.
    	testScore = score;
    
    	findGrade();
    }
    always initialize all variables

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    And from what I noticed on my compiler (Visual C++ 6) you will need to change this:
    Code:
    #include <iostream.h>
    to this:
    Code:
    #include <iostream>
    using namespace std;
    Also:
    Code:
    void StudentGrade::displayStudentGrade()
    {
    	cout<<"Student #"<<idNum<<" "<<name<<" earned "<<testScore<<" out of "<<possible<<".  Letter grade is "<<grade<<endl;
    };   // Get rid of the ;
    and the same goes for this place:
    Code:
    void StudentGrade::findGrade()
    {
    	if(testScore/possible>=.90)
    		grade='A';
    	else if(testScore/possible>=.80 && testScore/possible<.90)
    		grade='B';
    	else if(testScore/possible>=.70 && testScore/possible<.80)
    		grade='C';
    	else if(testScore/possible>=.60 && testScore/possible<.70)
    		grade='D';
    	else
    		grade='F';
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM