473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Difficulty with project

Hey guys its me again, having trouble with a project for class. We're
writing a program to Grade a test, the answer key and student answers r
in the same file and we have to read them in and score the test and
then assign a grade and output all of that to another file. I've got
everything done, just can't get the switch structure that assigns the
grades based on the score to work. This is the error I get :
error C2440: '=' : cannot convert from 'const char [2]' to 'char'

and this is the source for the function:

char examGrades(int& score)
{
char ch2;

switch(static_cast<int>(score /40.0 * 10))
{
case 0: case 1: case 2: case 3: case 4: case 5: ch2 = "F";
case 6: ch2 = "D";
case 7: ch2 = "C";
case 8: ch2 = "B";
case 9: case 10: ch2 = "A";
default: ch2 = "F";
break;
}
return ch2;

}

Any suggestions would be great as this is due by midnight tonight. I
know, I'm a horrible procrastinator! :(

Thanks Again,
Matt

Apr 26 '06 #1
15 1640
Nevermind, I figured the problem out. Thanks anyway!

Apr 26 '06 #2
On 25 Apr 2006 18:49:09 -0700 "ni**********@yahoo.com"
<ni**********@yahoo.com> waved a wand and this message magically
appeared:
case 0: case 1: case 2: case 3: case 4: case 5: ch2 =
"F"; case 6: ch2 = "D";
case 7: ch2 = "C";
case 8: ch2 = "B";
case 9: case 10: ch2 = "A";
default: ch2 = "F";


Your problem is that you are using double quotes (") when you should
have used single quotes (').

--
http://www.munted.org.uk

Take a nap, it saves lives.
Apr 26 '06 #3
ni**********@yahoo.com wrote:
Hey guys its me again, having trouble with a project for class. We're
writing a program to Grade a test, the answer key and student answers r
in the same file and we have to read them in and score the test and
then assign a grade and output all of that to another file. I've got
everything done, just can't get the switch structure that assigns the
grades based on the score to work. This is the error I get :
error C2440: '=' : cannot convert from 'const char [2]' to 'char'

and this is the source for the function:

char examGrades(int& score)
{
char ch2;

switch(static_cast<int>(score /40.0 * 10))
{
case 0: case 1: case 2: case 3: case 4: case 5: ch2 = "F";
case 6: ch2 = "D";
case 7: ch2 = "C";
case 8: ch2 = "B";
case 9: case 10: ch2 = "A";
default: ch2 = "F";
break;
}
return ch2;

}

FWIW there are more errors(actually same one repeated) in your function
which will cause it not to function as you expect. Read up about switch
statement to discover what you have dong wrong. I would also look up
when it is a good idea passing arguments by reference, const reference
or by value

regards
Andy Little

Apr 26 '06 #4


Do you want to return a single character (without a null terminator), or do
you want to return a string? Let's say you want a string:
char examGrades(int& score)
const char* examGrades(int score)
It's inefficient to pass by reference in the above;

{
char ch2;

const char* ch2;


switch(static_cast<int>(score /40.0 * 10))
{
case 0: case 1: case 2: case 3: case 4: case 5: ch2 = "F";

You need a "break" statment here.

case 6: ch2 = "D";

And here.

case 7: ch2 = "C";

And here.
case 8: ch2 = "B";

And here.
case 9: case 10: ch2 = "A";
And here.
default: ch2 = "F";
break;
}
return ch2;

}

There's no need for the "ch2" variable. I simply would have written:

case 7: return "C";

-Tomás
Apr 26 '06 #5
Tomas wrote:

There's no need for the "ch2" variable. I simply would have written:

case 7: return "C";

You could, but many coding style manuals frown on multiple returns.
There's nothing wrong with that part of the program.


Brian
Apr 26 '06 #6
Default User wrote:
You could, but many coding style manuals frown on multiple returns.
There's nothing wrong with that part of the program.


There's nothing wrong with multiple returns if you follow a more general
principle - no long functions.

Once you think you must have long functions, then you get lots of extra
rules to deal with them - no goto, single return, etc.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Apr 26 '06 #7
... many coding style manuals frown on multiple returns.

Only retarded people have to read those manuals. People with a fully-
functional brain have the intelligence to analyse and make these decisions
all by themselves.

-Tomás
Apr 26 '06 #8
Tomás wrote:
Only retarded people have to read those manuals. People with a fully-
functional brain have the intelligence to analyse and make these decisions
all by themselves.


Wow! You are smart enough to just read the ISO C++ Standard, and reconstruct
the book /C++ Coding Standards/ from it?

Glad I can read, 'cause I ain't so smart!

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Apr 26 '06 #9
Tomas wrote:
... many coding style manuals frown on multiple returns.

Only retarded people have to read those manuals.


Jackass.

Brian
Apr 26 '06 #10
Phlip wrote:
Default User wrote:
You could, but many coding style manuals frown on multiple returns.
There's nothing wrong with that part of the program.


There's nothing wrong with multiple returns if you follow a more
general principle - no long functions.


In my personal code, I try to keep the number of returns reduced but
I'm not dogmatic about it. There are occasions when it's the clearer
choice. I don't think the case given is one of those, but opinions will
vary. In my professional code, I have to follow the style guide. Single
return is a requirement.

My point to Tomas (who has demonstrated that he's not a person worth
dealing with after all) was that it's a stylistic choice and far from
clear-cut. I generally abhor style comments in responses here unless it
refers to something that badly affects readability. This case, again in
my opinion, was not that type of situation.


Brian
Apr 26 '06 #11
Default User wrote:
In my personal code, I try to keep the number of returns reduced but
I'm not dogmatic about it. There are occasions when it's the clearer
choice. I don't think the case given is one of those, but opinions will
vary. In my professional code, I have to follow the style guide. Single
return is a requirement.

My point to Tomas (who has demonstrated that he's not a person worth
dealing with after all) was that it's a stylistic choice and far from
clear-cut. I generally abhor style comments in responses here unless it
refers to something that badly affects readability. This case, again in
my opinion, was not that type of situation.


My brief experience and subjective guesswork about the single-return rule is
it helps refactoring. Generally because if you drag your mouse from here to
there, everything between should itself also be single-entry single-exit,
too.

Contrarily, 'return' answers the old chestnut "how to break out of nested
loops without goto?" very nicely!

I won't ask if your day job's style guide specifies short functions. But
ours does. ;-)

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Apr 26 '06 #12
Tomás wrote:
... many coding style manuals frown on multiple returns.

Only retarded people have to read those manuals. People with a fully-
functional brain have the intelligence to analyse and make these decisions
all by themselves.


So if you were working on a project with a MIL-STD-498 development
model, and you would feel free to ignore your Software Development Plan?

Nice.
Apr 26 '06 #13
Phlip wrote:
Default User wrote:
In my personal code, I try to keep the number of returns reduced but
I'm not dogmatic about it. There are occasions when it's the clearer
choice. I don't think the case given is one of those, but opinions
will vary. In my professional code, I have to follow the style
guide. Single return is a requirement.

My point to Tomas (who has demonstrated that he's not a person worth
dealing with after all) was that it's a stylistic choice and far
from clear-cut. I generally abhor style comments in responses here
unless it refers to something that badly affects readability. This
case, again in my opinion, was not that type of situation.


My brief experience and subjective guesswork about the single-return
rule is it helps refactoring. Generally because if you drag your
mouse from here to there, everything between should itself also be
single-entry single-exit, too.

Contrarily, 'return' answers the old chestnut "how to break out of
nested loops without goto?" very nicely!

I won't ask if your day job's style guide specifies short functions.
But ours does. ;-)


No, our coding standard it fairly black-and-white on most issues. It's
used a lot these days by the tools group to create rules for code
analyzers.

Brian

Apr 26 '06 #14
"Phlip" <ph******@yahoo.com> wrote in message
news:Pa******************@newssvr33.news.prodigy.c om...
Default User wrote:
You could, but many coding style manuals frown on multiple returns.
There's nothing wrong with that part of the program.


There's nothing wrong with multiple returns if you follow a more general
principle - no long functions.

Once you think you must have long functions, then you get lots of extra
rules to deal with them - no goto, single return, etc.


Actually, one of the coding standards came up with one entry point and one
exit point per function. I believe it was top down coding, but am not
positive, that was many years ago. I tend to agree with it, however.

The exception I make to the rule is in case of error conditions such as a
function/method used to load a table returning a bool value on success or
failure. If the file can't be loaded I will display an error, or log to a
file, then return false. The other exception I make is at the very bottom
of a function I may have a
return true;
else
return false;
Apr 27 '06 #15
Default User wrote:
Tomas wrote:
There's no need for the "ch2" variable. I simply would have written:

case 7: return "C";


You could, but many coding style manuals frown on multiple returns.
There's nothing wrong with that part of the program.


Laziness. Multiple returns are tricky if they're irregular. If EVERY
return is
inside a switch statement, and EVERY label has a return statement,
those
multiple return statements make a more regular & readable function. The
alternative is to define a return_ variable, which has no other purpose
but
to satisfy a dumb rule. Result: the compiler, the programmer and the
end
user are all worse off by the rule; the rule maker saves writing a few
sentences.

HTH,
Michiel Salters

Apr 27 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: rp_3 | last post by:
Hello, I am attempting to insert the following but am having difficulty with the "&" symbol being accepted as a literal character. If I preced the "&" with a backslash "\", when I execute via...
9
by: Adam | last post by:
Hey, I'm trying to write a script with two standard drop down boxes. One contains days one contains the month. I want to update the options in the days box everytime the month is changed......
1
by: Samridhi Kumar Shukla | last post by:
I am facing technical difficulti in using server connection control because we cannot change the path once fixed .. one way is to edit the code of the form generated code but though it allow to...
3
by: Vijay | last post by:
Dont study Linked List because I is quiet Difficult.
3
by: wheresjim | last post by:
I am trying this project out: http://www.codeproject.com/useritems/javacsharp.asp I am having difficulty building parts of it with Visual Studio .NET 2003 because of a post-build step that...
1
by: muchexie | last post by:
i' trying to build a login system and have the following error, which i'm finding difficult to fix. Fatal error: session_start() : Failed to initialize storage module: user (path:...
7
by: =?Utf-8?B?SmVmZkRvdE5ldA==?= | last post by:
I have an asp.net application using a multi-page wizard control that grabs user selected files from a database and allows the user to configure parameters using controls on the wizard pages. The...
0
by: Douglas Zuniga | last post by:
First at al Hi. I'm having some problems trying to connect my C# program-classes to Java, I've been reading about it and I found this:...
6
by: truezplaya | last post by:
Hi all I am currently in the situation of deciding what to do for my final year project. I was wondering if any had already done such a project and if they could shed any light on the level of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.