Thread: Need Help, cannot get char to display A+,F-, etc.

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    Need Help, cannot get char to display A+,F-, etc.

    I can get 'A' to display as a char, but not 'A-', what do I have to do to correct this problem. Thank YOu.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int	exam1, exam2, exam3,
    		homework1, homework2, homework3, homework4, homework5, homework6,
    		quiz1, quiz2, quiz3,
    		ExamScore,
    		HomeworkScore,
    		QuizScore,
    		FinalScore;
    	 char grade;
    
    	cout << "What is your grade on Exam 1?\n";
    	cin >> exam1;
    	cout << "What is your grade on Exam 2?\n";
    	cin >> exam2;
    	cout << "What is your grade on Exam 3\n";
    	cin >> exam3;
    
    	cout << "What is your grade on Homework 1?\n";
    	cin >> homework1;
    	cout << "What is your grade on Homework 2?\n";
    	cin >> homework2;
    	cout << "What is your grade on Homework 3?\n";
    	cin >> homework3;
    	cout << "What is your grade on Homework 4?\n";
    	cin >> homework4;
    	cout << "What is your grade on Homework 5?\n";
    	cin >> homework5;
    	cout << "What is your grade on Homework 6?\n";
    	cin >> homework6;
    	
    	cout << "What is your grade on Quiz 1?\n";
    	cin >> quiz1;
    	cout << "What is your grade on Quiz 2?\n";
    	cin >> quiz2;
    	cout << "What is your grade on Quiz 3?\n";
    	cin >> quiz3;
    
    	ExamScore = ((exam1 + exam2 + exam3) / 3) * .6;
    	HomeworkScore =((homework1 + homework2 + homework3 + homework4 + homework5 + homework6) / 6) * .3;
    	QuizScore = ((quiz1+quiz2+quiz3)/3)*0.1;
    	FinalScore= ExamScore + HomeworkScore + QuizScore;
    
    
    	if (FinalScore < 60)
    		grade = 'F';
    	else if (FinalScore >=60)
    		grade = 'D-';
    	else if (FinalScore >=62)
    		grade = 'D';
    	else if (FinalScore >=67)
    		grade = 'D+';
    	else if (FinalScore >=70)
    		grade = 'C-';
    	else if (FinalScore >=72)
    		grade = 'C';
    	else if (FinalScore >=77)
    		grade = 'C+';
    	else if (FinalScore >=80)
    		grade = 'B-';
    	else if (FinalScore >=82)
    		grade = 'B';
    	else if (FinalScore >=87)
    		grade = 'B+';
    	else if (FinalScore >=90)
    		grade = 'A-';
    	else if (FinalScore >=92)
    		grade = 'A';
    	else if (FinalScore >97)
    		grade = 'A+';
    
    	cout<< "Your final score is "<<FinalScore<<endl;
    	cout<< "Your letter grade is " <<grade<<endl;
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    "A+" and "A-" is NOT one character, but a string. you need to enclose it in double quotes. That means variable grade needs to be a char*, not char because it can take on more than one character at a time.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    or char grade[2];

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    D's are 60-69 where you are at? Around here a D is 65-69!...Not that I have ever gotten a D...errr

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by maxorator
    or char grade[2];
    you forgot the null-terminator!
    char grade[3];

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's C++
    Use
    std::string grade;

    Then
    grade = "A+";

    etc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    1
    Thanks Ancient Dragon. It works on my work.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Forum rule #3
    You should have just clicked the "like" button, rather than digging up a 6 year old thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  5. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM