473,386 Members | 1,830 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,386 software developers and data experts.

Jussssst a bit of help if you would with strings

Im having some difficulty with strings here, I hope someone is kind
enough to help, I do appreciate it. Im working on a grade point
average calculator for my intro to programming class and I thought I
would go a bit above and beyond the scope of the class and use
strings. But I ran into a snag with my getgrades function. The
compiler gives me the error: "81 ISO C++ forbids comparison between
pointer and integer"

here is the code for the function
float getgrades(int num) // gets the grade for the class
{
int i;
float grade;
char str[10];

for(i=0;i=9;i++) str[i]=0; //reset's the string

cout << "\n Please enter the grade for your number " << num << "
class. (from A to F) \n";
gets(str);

if((bool) str[1] == false) // is neither + or -, null terminator
located at str[1]
{
if(str[0] == "A") grade = 4.0;
else if(str[0] == "B") grade = 3.0;
else if(str[0] == "C") grade = 2.0;
else if(str[0] == "D") grade = 1.0;
else if(str[0] == "F") grade = 0;
}
else if(str[1] == "+") // entered +
{
if(str[0] == "B") grade = 3.4;
else if(str[0] == "C") grade = 2.4;
else if(str[0] == "D") grade = 1.4;
}
else if(str[1] == "-") // entered -
{
if(str[0] == "A") grade = 3.7;
else if(str[0] == "B") grade = 2.7;
else if(str[0] == "C") grade = 1.7;
else if(str[0] == "D") grade = 0.7;
}
/* failed switch statement-------->
switch(str[1]) {
case "A":
grade = 4.0;
/*case "A-":
grade = 3.7;
case "B+":
grade = 3.4;
case "B":
grade = 3.0;
case "B-":
grade = 2.7;
case "C+":
grade = 2.4;
case "C":
grade = 2.0;
case "C-":
grade = 1.7;
case "D+":
grade = 1.4;
case "D":
grade = 1.0;
case "D-":
grade = 0.7;
case "F":
grade = 0;
default:
cout << "Pease enter a valid grade A-F";
}
*/


return grade;
}


Any ideas? I would greatly appreciate it, thanks again
-Nick

Oct 29 '07 #1
5 1818
Kelth.Raptor wrote:
Im having some difficulty with strings here, I hope someone is kind
enough to help, I do appreciate it. Im working on a grade point
average calculator for my intro to programming class and I thought I
would go a bit above and beyond the scope of the class and use
strings. But I ran into a snag with my getgrades function. The
compiler gives me the error: "81 ISO C++ forbids comparison between
pointer and integer"

here is the code for the function
float getgrades(int num) // gets the grade for the class
{
int i;
float grade;
char str[10];

for(i=0;i=9;i++) str[i]=0; //reset's the string

cout << "\n Please enter the grade for your number " << num << "
class. (from A to F) \n";
gets(str);

if((bool) str[1] == false) // is neither + or -, null terminator
located at str[1]
{
if(str[0] == "A") grade = 4.0;
'str[0]' is a <char>. You're trying to compare it to <"A"which is
an array of <char>. You probably want to compare it to 'A' (note the
single quotation marks), which is also a <char>.

Same goes to the 'switch' statement below.
else if(str[0] == "B") grade = 3.0;
else if(str[0] == "C") grade = 2.0;
else if(str[0] == "D") grade = 1.0;
else if(str[0] == "F") grade = 0;
}
else if(str[1] == "+") // entered +
{
if(str[0] == "B") grade = 3.4;
else if(str[0] == "C") grade = 2.4;
else if(str[0] == "D") grade = 1.4;
}
else if(str[1] == "-") // entered -
{
if(str[0] == "A") grade = 3.7;
else if(str[0] == "B") grade = 2.7;
else if(str[0] == "C") grade = 1.7;
else if(str[0] == "D") grade = 0.7;
}
/* failed switch statement-------->
switch(str[1]) {
case "A":
grade = 4.0;
/*case "A-":
grade = 3.7;
case "B+":
grade = 3.4;
case "B":
grade = 3.0;
case "B-":
grade = 2.7;
case "C+":
grade = 2.4;
case "C":
grade = 2.0;
case "C-":
grade = 1.7;
case "D+":
grade = 1.4;
case "D":
grade = 1.0;
case "D-":
grade = 0.7;
case "F":
grade = 0;
default:
cout << "Pease enter a valid grade A-F";
}
*/


return grade;
}


Any ideas? I would greatly appreciate it, thanks again
-Nick
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 29 '07 #2

"Kelth.Raptor" <Ke**********@gmail.comwrote in message
news:11**********************@z9g2000hsf.googlegro ups.com...
Im having some difficulty with strings here, I hope someone is kind
enough to help, I do appreciate it. Im working on a grade point
average calculator for my intro to programming class and I thought I
would go a bit above and beyond the scope of the class and use
strings. But I ran into a snag with my getgrades function. The
compiler gives me the error: "81 ISO C++ forbids comparison between
pointer and integer"

here is the code for the function
float getgrades(int num) // gets the grade for the class
{
int i;
float grade;
Use type 'double' for floating-point values unless you have
a compelling reason to do otherwise. This includes
this 'grade' object as well as your return type.
char str[10];
This is C++. Instead of "C-style" strings, why not use the
much easier-to-use std::string type?
>
for(i=0;i=9;i++) str[i]=0; //reset's the string
This loop will first set str[0] to zero, and then
set str[9] to zero over and over again, forever.

You probably meant:
for(i = 0; i < 10; i++) str[i] = 0;

However that loop would be completely unnecessary if you
simply initialize your array when you define it:

char str[10] = {0}; /* initialize all ten elements to zero */
>
cout << "\n Please enter the grade for your number " << num << "
class. (from A to F) \n";
gets(str);
Never, never, NEVER use gets(). There's no way to prevent overflowing
your array if the user enters too many characters. You could use fgets()
instead, which can control this. Better yet, using 'std::getline()'
function and a 'std::string' object. The string will grow and shrink
as needed. C++ has a large and very useful standard library. Why not
take advantage of it?
>
if((bool) str[1] == false) // is neither + or -, null terminator
located at str[1]
There's no need for the cast to type 'bool'. The comparison
operator will take care of it for you.

if(str[1] == false)

or

if(!str[1])

{
if(str[0] == "A") grade = 4.0;
The expression "A" evaluates to a pointer (type 'char *').

You're trying to compare it to a charater (str[0]).

if(str[0] == 'A') grade = 4.0; /* expression 'A' has type 'char' */
If you have your compiler warnings set to a reasonable level,
I'd expect a warning here. The expression 4.0 has type double.
You're assigning it to a type 'float' object. Type 'float' is not
guaranteed to be able to represent all possible type 'double' values.
else if(str[0] == "B") grade = 3.0;
else if(str[0] == "C") grade = 2.0;
else if(str[0] == "D") grade = 1.0;
else if(str[0] == "F") grade = 0;
}
[snip]

Finally, your code looks like a mixture of C and C++.
Most C constructs are legal in C++, but often will have
different semantics, possibly causing unnecessary confusion.
Pick a language.

-Mike
Oct 29 '07 #3
On Oct 29, 2:20 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>...
Finally, your code looks like a mixture of C and C++.
Most C constructs are legal in C++, but often will have
different semantics, possibly causing unnecessary confusion.
Pick a language.

-Mike
Mike:
Thanks alot for the tips and pointers. Those concepts are new to me
and I will quickly explore them. In class we are learning purely off
of the professor who teaches things in a dumbed down sort of way i.e.
declaring that localized variables in functions are "gym lockers in
California" and explaining everything to the class in such terms. I
thought I would learn better from a textbook so I picked up a copy of
"C++ from the ground up 3rd edition" (ISBN: 0072228970) and have been
progressing though it Perhaps I should pick up something to supplement
it now. I really appreciate your comments.

Thanks again,
-Nick

Oct 29 '07 #4
Kelth.Raptor wrote:
[..] I
thought I would learn better from a textbook so I picked up a copy of
"C++ from the ground up 3rd edition" (ISBN: 0072228970) and have been
progressing though it Perhaps I should pick up something to supplement
it now. I really appreciate your comments.
Oh, no!... Run away from that book! Now! Or it's gonna poison you
and you're never gonna recover!

Go to http://accu.org/index.php/book_reviews and find a book that is
*recommended* and can be found either in your college's library or in
the store. While you're there, check all the books your prof suggests.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 29 '07 #5
On Oct 29, 7:20 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"Kelth.Raptor" <Kelth.Rap...@gmail.comwrote in message
news:11**********************@z9g2000hsf.googlegro ups.com...
Im having some difficulty with strings here, I hope someone is kind
enough to help, I do appreciate it. Im working on a grade point
average calculator for my intro to programming class and I thought I
would go a bit above and beyond the scope of the class and use
strings. But I ran into a snag with my getgrades function. The
compiler gives me the error: "81 ISO C++ forbids comparison between
pointer and integer"
here is the code for the function
float getgrades(int num) // gets the grade for the class
{
int i;
float grade;
Use type 'double' for floating-point values unless you have
a compelling reason to do otherwise. This includes
this 'grade' object as well as your return type.
char str[10];
This is C++. Instead of "C-style" strings, why not use the
much easier-to-use std::string type?
for(i=0;i=9;i++) str[i]=0; //reset's the string
This loop will first set str[0] to zero, and then
set str[9] to zero over and over again, forever.
You probably meant:
for(i = 0; i < 10; i++) str[i] = 0;
However that loop would be completely unnecessary if you
simply initialize your array when you define it:
char str[10] = {0}; /* initialize all ten elements to zero */
Or if he'd declared:

std::string input ;
cout << "\n Please enter the grade for your number " << num << "
class. (from A to F) \n";
gets(str);
Never, never, NEVER use gets(). There's no way to prevent
overflowing your array if the user enters too many characters.
You could use fgets() instead, which can control this. Better
yet, using 'std::getline()' function and a 'std::string'
object. The string will grow and shrink as needed. C++ has a
large and very useful standard library. Why not take
advantage of it?
if((bool) str[1] == false) // is neither + or -, null terminator
located at str[1]
There's no need for the cast to type 'bool'. The comparison
operator will take care of it for you.
if(str[1] == false)
or
if(!str[1])
Actually, both are bad, because you don't have a boolean value,
you have a character. Although there are implicit conversions
(for reasons of compatibility with C, which had them because
there was no boolean type), you really should write:

if ( str[1] != '\0' )

It's a lot clearer that you're testing for the end of the
string.

Of course, in any real code, you'd want to allow leading and
trailing white space, verify that the syntax was correct,
without any trailing garbage, etc., etc. This is trivial to do
with boost::regex, but that may be getting ahead of the PO for
the moment (although he really should master regular expressions
fairly early in his curriculum).

For the moment, I'd probably pre-check the syntax somewhat, with
something like:

if ( input.size() == 1
|| (input.size() != 2
&& (input[ 1 ] == '-' || input[ 1 ] == '+' ) ) ) {
// Ok!
} else {
// error...
}

Using std::string, of course. Alternatively:

switch ( input.size() ) {
case 1 :
grade = pickUpLetterGrade( input[ 0 ] ) ;
break ;

case 2 :
grade = pickUpLetterGrade( input[ 0 ] ) ;
grade = adjustForModifier( grade, input[ 1 ] ) ;
break ;

default:
// Error handling ...
}

This would require some error handling in the two functions as
well, however. (Both functions are also easily implemented with
a switch: the first returning 0.0, 1.0, etc., the second
returning the first argument +/- 0.3, or whatever.

Or you could just use a statically initialized map< std::string,
double (or a simple manual table lookup---there are few enough
values that a linear search is fine).
{
if(str[0] == "A") grade = 4.0;
The expression "A" evaluates to a pointer (type 'char *').
Not really. It evaluates to a char const[2]; an array of 2 char
const. In this context, of course, an array converts implicitly
to a pointer, the address of its first element. But it's better
if the OP learns working array and string types (std::vector and
std::string) first, and only attacks this misfeature once he's
got some more basic knowledge.
You're trying to compare it to a charater (str[0]).
if(str[0] == 'A') grade = 4.0; /* expression 'A' has type 'char' */
If you have your compiler warnings set to a reasonable level,
I'd expect a warning here. The expression 4.0 has type double.
You're assigning it to a type 'float' object. Type 'float' is not
guaranteed to be able to represent all possible type 'double' values.
Still, since it occurs almost anytime you're using float (or
short, or char as an int), such warnings would just be so much
noise. I doubt very many compilers will generate them (although
you never know---some of the warnings one sees are pretty
silly).
else if(str[0] == "B") grade = 3.0;
else if(str[0] == "C") grade = 2.0;
else if(str[0] == "D") grade = 1.0;
else if(str[0] == "F") grade = 0;
}
[snip]
Finally, your code looks like a mixture of C and C++.
Most C constructs are legal in C++, but often will have
different semantics, possibly causing unnecessary confusion.
Use std::string and std::vector to start with, rather than C
style arrays (which are very difficult to handle). Then you can
just write:

if ( input == "A" ) ...
else if ( input == "A-" ) ...
else if ( input == "B+" ) ...
...

Which, of course, will immediately suggest a table to anyone
with any experience.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 30 '07 #6

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

Similar topics

1
by: Jack Smith | last post by:
Can some one help me with this proof? Let L be the language of strings over {a,b} recursively defined by the following set of rules (i) the string ^ is in L (^ is the null string). (ii) for...
5
by: ArShAm | last post by:
Hi there Please help me to optimize this code for speed I added /O2 to compiler settings I added /Oe to compiler settings for accepting register type request , but it seems that is not allowed...
3
by: Mahmood Ahmad | last post by:
Hello, I have written a program that reads three types of records, validates them acording to certain requirements and writes the valid records into a binary file. The invalid records are...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
4
by: Robin Haswell | last post by:
Okay I'm getting really frustrated with Python's Unicode handling, I'm trying everything I can think of an I can't escape Unicode(En|De)codeError no matter what I try. Could someone explain to...
2
by: almurph | last post by:
Hi, Hope you can help me. This is a trick one - at least I think so. I have 2 strings. I have to calculate the "score" of the strings. Score is determined by matching patterns of letters within...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
4
by: Debbiedo | last post by:
My software program outputs an XML Driving Directions file that I need to input into an Access table (although if need be I can import a dbf or xls) so that I can relate one of the fields...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.