can someone tell me whats wrong with my code?

Started by
14 comments, last by fireblade 19 years, 8 months ago
I get the following error when i compile the below code: 27 C:\Program Files\Dev-Cpp\Projects\Grade\Grades.cpp could not convert `(&moreGradesToCalculate)->std::basic_string<_CharT, _Traits, I dont know what is wrong considering i checked the code and i copied it out of a book.

//Grades.cpp

#include <iostream>
#include <string>
#include "EnglishStudent.cpp"
#include "MathStudent.cpp"
#include "ScienceStudent.cpp"
#include "DisplayGrade.cpp"

using namespace std;

int WhatKindOfStudent();
char response[256];
string moreGradesToCalculate;

int main()
{
    int lresponse;
    
    cout << "Do you want to calculate a grade? ";
    cin >> moreGradesToCalculate;
    
    for (int i = 0; i < moreGradesToCalculate.length(); i++) {
        moreGradesToCalculate = toupper (moreGradesToCalculate);
        }
        
    while (moreGradesToCalculate = "YES") 
    {
        lresponse = WhatKindOfStudent();
        
        switch(lresponse)
        {
            case 1: //EnglishStudent
            {
                EnglishStudent eStudent;
                eStudent.Calculate();
                DisplayGrade x(eStudent.midterm, eStudent.finalExamGrade,
                                eStudent.research, eStudent.presentation, eStudent.finalNumericGrade,
                                eStudent.finalLetterGrade);
            }
            break;
            case 2: //MathStudent
            {
                MathStudent mStudent;
                mStudent.Calculate();
                DisplayGrade y(mStudent.midterm, mStudent.finalExamGrade, 
                mStudent.finalNumericGrade, mStudent.finalLetterGrade);
            }
            break;
            case 3: //ScienceStudent
            {
                ScienceStudent sStudent;
                sStudent.Calculate();
                DisplayGrade z(sStudent.midterm, sStudent.finalExamGrade, 
                sStudent.research, sStudent.finalNumericGrade, sStudent.finalLetterGrade);
            }
            break;
        } //End Switch
                        
        cout << endl << endl << "Do you have another grade to calculate? ";
        cin >> moreGradesToCalculate;
        for (int i = 0; i < moreGradesToCalculate.length(); i++) {
        moreGradesToCalculate = toupper(moreGradesToCalculate);}
    }// end of while
    
    cout << "Thanks for using the Grades Calculation program!";
    system("PAUSE");
    return 0;
}

int WhatKindOfStudent()
{
    cout << "Enter student type " << "(1 = English, 2 = Math, 3 = Science): ";
    cin.getline(response,256);
    
    if (strlen(response) == 0) {
        cout << "You must select a Student Type";
        exit(1);
    }
    return atoi(response);
}

[Edited by - xidis on August 19, 2004 8:27:43 PM]
Advertisement
It looks like the problem is this line:

while (moreGradesToCalculate = "YES") 


As you can see, you're using the assignment operator, not the equality operator. Change it to:

while (moreGradesToCalculate == "YES") 


See if that helps.

-hellz
 for (int i = 0; i < moreGradesToCalculate.length(); i++) {moreGradesToCalculate = toupper (moreGradesToCalculate);}should be:transform(moreGradesToCalculate.begin(), moreGradesToCalculate.end(), moreGradesToCalculate.begin(), toupper);


 while (moreGradesToCalculate = "YES") 

is not going to do what you want it to. Change the "=" to "==".

If you need help with an error, it helps to show the entire error string, not just the first snippet of it. Also, please use source tags when posting lots of source code.

EDIT: fixed my transform call
We are the music makers and we are the dreamers of the dreams. - WonkaAsking Smart Questions | BookPool
Thanks for your help. That got rid of the error. Now I have another problem. I've figured out that it is in this part of the code.

for (int i = 0; i <= moreGradesToCalculate.length(); i++) {        moreGradesToCalculate = toupper(moreGradesToCalculate);}


Scrime, I know u said use the transform function, but being a C++ newb, i dont know what that is or how to make it work. With the code above, it seems it just causes the program to quit. I took that out and just initialized the moreGradesToCalculate to "YES" and it worked fine until it reached the same loop at the bottom. Thanks for your help thus far.
Since you are still learning at this level, as apposed to telling you what is wrong, I will give you this hint:

The string "yes" has 3 letters in it.
How many times do you go through the loop?

step through the loop your self, instruction by instruction like you were the computer. . . no cheating here, or you could miss your solution.

remember do every thing the computer would do, including comparing i <= moreGradesToCalculate.length(); every time through the loop.
in line with the person above, I will also tell you that this is what is known as an "off by one error" and is VERY VERY common for programmers to make, especially during their first 2 years.

Basically, whenever you see a loop, you need to ask yourself either "how many times do I want this to happen?" or "upon which items do I want this to act?" ... and then sit down with pen and paper for a a small example (like say, a 3 item list), and make sure that it happens EXACTLY 3 times, not 1, not 2, and not 4 (think monty python skit). and also that the times it does happen, the values of the items are what you want them to be.

In your case your loop simply happens the wrong number of times ... but you always need to look for bugs with either the 'how many', or the 'what value' side of things.

good luck
Good answer AP [smile].

The solution is staring you right in the face and it's so tiny it's easily missed. See if you can spot the error and come back to collect your cookie.

Learning to debug is a valuable programming skill at any level :)
Thank you, evolutional.

And happy bug hunting, xidis.
P.S. Look I remembered to log in!
Okay, I was messing around with it and forgot to change it back to this:
for (int i = 0; i < moreGradesToCalculate.length(); i++) {        moreGradesToCalculate = toupper(moreGradesToCalculate);}


I think thats what you all were talking about, because the other one i had was looking for 4 letters to uppercase. This one is looking for three, yet it still causes the program to shut down whenever i enter any form (capped/uncapped) of the word yes.

Any other word buy yes causes the program to terminate like it should.

Do you think it could be the compiler, Dev C++ 5? Thanks.



Quote:Original post by xidis
Do you think it could be the compiler, Dev C++ 5? Thanks.


Try inserting this line....
cin.ignore(1,'\n');
Just above this line...
cin.getline(response,256);

This topic is closed to new replies.

Advertisement