Public Group

# 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.

## 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"

using namespace std;

int WhatKindOfStudent();
char response[256];

int main()
{
int lresponse;

cout << "Do you want to calculate a grade? ";

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

{
lresponse = WhatKindOfStudent();

switch(lresponse)
{
case 1: //EnglishStudent
{
EnglishStudent eStudent;
eStudent.Calculate();
}
break;
case 2: //MathStudent
{
MathStudent mStudent;
mStudent.Calculate();
}
break;
case 3: //ScienceStudent
{
ScienceStudent sStudent;
sStudent.Calculate();
}
break;
} //End Switch

cout << endl << endl << "Do you have another grade to calculate? ";
for (int i = 0; i < moreGradesToCalculate.length(); i++) {
}// 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 on other sites
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 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 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 on other sites
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 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 on other sites

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 on other sites
Thank you, evolutional.

And happy bug hunting, xidis.

##### 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 on other sites
Quote:
 Original post by xidisDo 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);

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 76
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632968
• Total Posts
3009584
• ### Who's Online (See full list)

There are no registered users currently online

×