# can someone tell me whats wrong with my code?

This topic is 4867 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[i] = toupper (moreGradesToCalculate[i]);}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[i] = toupper(moreGradesToCalculate[i]);}

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[i] = toupper(moreGradesToCalculate[i]);}

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);

##### Share on other sites
Tell me your problem exactly, as I'm having a hard time replicating it...

Which line is causing the error? And what exactly is the error...?

##### Share on other sites
I think I know where your problem is, but it may be more helpful for you to find it, but here is a hint or two to help you out:

Input from the keyboard does not actually go directly to your program, it is buffered. The same goes for output from your program to the screen.

A common and simple way to find where your program is actually terminating is to use something like:

cout << "I made it this far!" << endl;

the endl here is important. Because of the buffer issue I mentioned above, if the program crashes before the buffer is flushed to screen you will never see your line, even though you wrote it. the endle will put a newline and flush the buffer.

Move this line around, following your flow of control. I would suggest starting it after your loop, and see if you get what you expect.

 BTW: cin >> will read up to the newline(or space), but will not read the newline, and this is the purpose the ignore Matt Apple suggested. remember that ignore will ignore upto and including the newline(or the number of characters specified).

##### Share on other sites
Okay, I found the problem. The only thing is I dont know why the program wouldn't register the cin.getline(response,256). It seemed like it skipped it, probably cause there is some space or something im assuming, and when it encountered the if statement, the program terminated. Also, can someone explain what exactly the ignore() meathod of cin does, as my book did not cover this. Thanks.

int WhatKindOfStudent(){        cout << "Enter student type  (1 = English, 2 = Math, 3 = Science): ";    cin.ignore(1,'\n'); //added this to fix cin.getline    cin.getline(response,256);        if (strlen(response) == 0) {        cout << "You must select a Student Type";        exit(1);    }cout << "I made it this far" << endl;    return atoi(response);}

##### Share on other sites
As explained in my previous post, input is buffered. cin.ignore will ignore the specified number of bytes up to a specific character. In your case:

cin.ignore(1, '\n');

ignore one character upto and including the newline. if you replaces the one with 5, it would still only read up to the newline, and not beyond.

Now, before you get to the get line you do a cin like this:

cin >> moreGradesToCalculate;

This skips leading white space and stops at the first whitespace after that, in your case the newline, but does not remove it from the buffer. What this means is that the newline is the next thing on the buffer and the next character to be read in.

This does not effect using the stream operator (>>) because it will be ignored. cin.getline, however does not ignore it.

cin.getline(response,256);

will read up to 256 character up to the newline, ignoring it. Without the ignore the next character to be read is a newline, therefore it stops there, and your string is length zero. So, you enter your if.

On another note, cin.getline can actually take a third parameter, which is the character to read to, if you don't like the newline:

cin.getline(responce,256, ';'); //for example `

##### Share on other sites
Okay, thanks again for all ur help, im sure ill be needing it again sometime as I learn more and more c++. Thanks again.

##### Share on other sites
Quote:
 Original post by Anonymous PosterThe 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.

The good old rule, if you want to copmletely understand it do it on foot.

##### Share on other sites

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