Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   'C-' and 'C+' (http://www.programmingforums.org/showthread.php?t=3721)

TecBrain May 4th, 2005 2:14 AM

'C-' and 'C+'
 
:

char ch;


After I input the character:
:

std::cin>> ch;

And check for the value

EX:

if((ch=='C-') || (ch=='C+'))

do .......

But it not seems to be working, I get an error that C- and C+ are dublicated values.....

Berto May 4th, 2005 2:35 AM

try double quotes instead of single quotes

:


if((ch=="c-") || (ch=="c+"))


iignotus May 4th, 2005 3:30 AM

Would reading more than one character into a char have anything to do with it?

Berto May 4th, 2005 4:59 AM

good point you will have to use strcmp to compare the strings

BaroN NighT May 4th, 2005 5:58 AM

char allocates memory for only one character. While each C- and C+ consists of two characters. So if you want to compare C- with C+, you will have to declare ch as a string not a char. And then you will have to use double quotes in the if, because its a string.

TecBrain May 4th, 2005 10:35 AM

Thanks Guys that was helpful. I'll Post the code soon

TecBrain May 4th, 2005 10:51 AM

Ok, here is the code; I would appreciate any comments and feedback, Thanks.

Its little program that calculate your GPA

:

#include <iostream>
#include <stdlib.h>

using namespace std;


int main()
{
        int i,credit=0,classes=0,totalcredit=0;
        char ch;
        float newavg=0,total=0,oldGpa=0,result=0;
        string grade;
       
        float const max1=4;
        float const max2=3.7;
        float const max3=3.3;
        float const max4=3;
        float const max5=2.7;
        float const max6=2.3;
        float const max7=2;
        float const max8=1.7;
        float const max9=1.3;
        float const max11=1;
        float const max12=0.7;
       
       
        std::cout << "How Many courses Are You Taking: " << '\n';
        std::cin >> classes;

       
        for(i=1;i<=classes;i++)
        {
                std::cout << "Enter Your " << i << " Grade " << '\n';
                std::cin >> grade;
                std::cout << "What is the Credits of this Course: " << '\n';
                std::cin >> credit;
               
               
               
                if((grade=="A") || (grade=="a"))
               
                        total = float(max1 * credit);
                       
                       
                        else if((grade == "A-") || (grade=="a-"))
               
                       
                        total = float(max2 * credit);
                       
                                       
                        else if((grade == "B+") || (grade=="b+"))
                       
                        total = float(max3 * credit);
                       

                        else if((grade == "B") || (grade=="b"))
                       
                        total = float(max4 * credit);
                       

                        else if((grade == "B-") || (grade=="b-"))
                       
                        total = float(max5 * credit);
               

                        else if((grade == "C+") || (grade=="c+"))
                       
                        total = float(max6 * credit);
                       

                        else if((grade == "C") || (grade=="c"))
                       
                        total = float(max7 * credit);
               

                        else if((grade == "C-") || (grade=="c-"))
                       
                        total = float(max8 * credit);
                       

                        else if((grade == "D+") || (grade=="d+"))
                       
                        total = float(max9 * credit);
               

                        else if((grade == "D") || (grade=="d"))
                       
                        total = float(max11 * credit);
               

                        else if((grade == "D-") || (grade=="d-"))
                       
                        total = float(max12 * credit);
                       
               
                totalcredit += credit;       
                result += total;
               
        }
       
       
        std::cout << "Your Total GPA is: " << (float(result) / totalcredit);
       
        std::cout << "Do You To have the AVG of more than one GPA (Y,N)" << '\n';
        std::cin >> ch;

       
       
        if((ch=='y') || (ch=='Y'))
        {

                std::cout << "Enter Your Previous GPA: " << '\n';
                std:: cin >> oldGpa;
                newavg = (float((result / totalcredit) + oldGpa) / 2);
        }
        else if((ch=='n') || (ch=='N'))
        {
                newavg = (float(result) / totalcredit);
        }
        std::cout << "Your GPA is:  " << newavg << "  Study harder " << '\n';
       

  system("PAUSE");       
  return 0;
}


Ooble May 4th, 2005 10:54 AM

Looks good. However, I would suggest putting all those grade boundaries in an array.

TecBrain May 4th, 2005 11:07 AM

mmm... u mean sth like this:

EX: int A[10] grades{A,A-,B+,.....}

if so how will I assign values.

Ooble May 4th, 2005 12:54 PM

This is how I would do it:
:

#include <iostream>
#include <stdlib.h>

using namespace std;


int main()
{
        int i = 0;
        int classes = 0;
       
        string grade = "";
        int credit = 0;
       
        int totalcredit = 0;
        float total = 0;
        float result = 0;
        float oldGPA = 0;
       
        char ch = '\0';
       
        float boundaries[] = { 4.0,  3.7,  3.3,  3.0,  2.7,  2.3,  2.0,  1.7,  1.3,  1.0,  0.7 };
        string grades[] =    { "A",  "A-", "B+", "B",  "B-", "C+", "C",  "C-", "D+", "D",  "D-" };
       
        std::cout << "How many courses are you taking? ";
        std::cin >> classes;

       
        for(i = 1; i <= classes; i++)
        {
                std::cout << "Enter Grade #" << i << ": ";
                std::cin >> grade;
                std::cout << "Enter the Course Credit: ";
                std::cin >> credit;
               
                grade[0] &= 0xDF;
               
                for (int j = 0; j < 11; j++)
                {
                        if (grade == grades[j])
                        {
                                total += boundaries[j] * credit;
                                totalcredit += credit;       
                        }
                }
               
        }
       
        std::cout << "Your Total GPA is: " << (total / totalcredit) << endl << endl;
       
        std::cout << "Do you want to know the average of more than one GPA? (y/n) ";
        std::cin >> ch;
        ch &= 0xDF;

        if (ch == 'Y')
        {

                std::cout << "Enter Your Previous GPA: ";
                std:: cin >> oldGPA;
                result = (((total / totalcredit) + oldGPA) / 2);
        }
        else
        {
                result = (float(total) / totalcredit);
        }
        std::cout << endl;
       
        std::cout << "Your GPA is: " << result << endl << endl;
        std::cout << "Keep studying!" << endl << endl;
       
        std::cout << "Press any key to continue...";
        getchar();
        return 0;
}



All times are GMT -5. The time now is 3:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC