Jump to content
  • Advertisement
Sign in to follow this  
xidis

can someone tell me whats wrong with my code?

This topic is 5175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.



Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!