C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can I get some help please "warning: multi-character charact

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Mauricio
Guest





PostPosted: Wed May 03, 2006 10:06 am    Post subject: Can I get some help please "warning: multi-character charact Reply with quote



I'm having problem with my homework. I have to use if/else if and
switch. If I only use if the programs run, and I get some sort of
result incorrect but something. However if I used switch the program
ask for the input and return 0 without any result ?????
other thing that I am getting is "warning: multi-character character
constant"

/* Premium Insurance Cost*/

#include <iostream>
using namespace std;
int main ()

{
int age;
int ticket;
int car;
double result;
//char choice;
cout << " How many ticket do you have?";
cin >> ticket;
cout << " Your current age is:";
cin >> age;
cout << " The value of your car is:";
cin >> car;

//switch (choice)
//{
//case 1:
if ((ticket == '1') && (age >= '16' || age <= '25')){
result = car*0.05*1.15*1.10;
cout << "Your Premium is $" << result << endl;
return 0;
//break;

}else if ((ticket == '1') && (age >= '25' || age <= '29'))
result = (car*0.05*1.10*1.10);
cout << "Your Premium is $" << result << endl;
return 0;
//break;
//}

}
/*case 2: if ((ticket == '2') && (age >= '16' || age <= '25')){
result = car*0.05*1.15*2.25;
cout << "Your Premium is $" << result << endl;
return 0;

}else if ((ticket == '2') && (age >= '25' || age <= '29'))
result = (car*0.05*1.10*2.25);
cout << "Your Premium is $" << result << endl;
return 0;

case 3: if (ticket == '3') && (age >= '16' || age <= '25')){
result = car*0.05*1.15*0.5;
cout << "Your Premium is $" << result << endl;
return 0;

}else if ((ticket == '3') && (age >= '25' || age <= '29'))
result = (car*0.05*1.10*0.5);
cout << "Your Premium is $" << result << endl;
return 0;

case 4: if ((ticket == '4') && (age >= '16' || age <= '25')){
//result = car*0.05*1.15*1.10;
cout << "COVERAGE DENIED"<< endl;
return 0;

}else if ((ticket == '4') && (age >= '25' || age <= '29'))
// result = (car*0.05*1.10*1.10);
cout << "COVERAGE DENIED" << endl;
return 0; */


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bob Hairgrove
Guest





PostPosted: Wed May 03, 2006 9:06 pm    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote



On 3 May 2006 05:32:59 -0400, "Mauricio" <mundurragaj (AT) gmail (DOT) com>
wrote:

Quote:
I'm having problem with my homework. I have to use if/else if and
switch. If I only use if the programs run, and I get some sort of
result incorrect but something. However if I used switch the program
ask for the input and return 0 without any result ?????
other thing that I am getting is "warning: multi-character character
constant"

/* Premium Insurance Cost*/

#include <iostream
using namespace std;
int main ()

{
int age;
int ticket;
int car;
double result;
//char choice;
cout << " How many ticket do you have?";
cin >> ticket;
cout << " Your current age is:";
cin >> age;
cout << " The value of your car is:";
cin >> car;

//switch (choice)
//{
//case 1:
if ((ticket == '1') && (age >= '16' || age <= '25')){

You need to drop the quotes here and in similar places.

The console I/O objects cin and cout automatically convert the values
to or from whatever type is receiving or sending them. You declared
age, ticket and car as integers, so they already have the integer
values they need. However, you are comparing them to characters; in
some places, you have two digits inside the single quotes, which is
why you get the "multiple constant" error.

Single quotes are used for single char values. Double quotes are used
to denote an array of chars which gets an automatic delimiting '\0' by
the compiler. If you write '25', however, only the '2' is seen by your
code (or is it the '5'? At any rate, one of them is dropped).

--
Bob Hairgrove
NoSpamPlease (AT) Home (DOT) com


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Valentin Kreh
Guest





PostPosted: Wed May 03, 2006 9:06 pm    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote



Mauricio schrieb:
Quote:
I'm having problem with my homework. I have to use if/else if and
switch. If I only use if the programs run, and I get some sort of
result incorrect but something. However if I used switch the program
ask for the input and return 0 without any result ?????
other thing that I am getting is "warning: multi-character character
constant"
One of your problems is, that you use single quotes when you shouldn't

(numbers) and you don't use them when you should (character constant).
The other problem is that choice is not initialized (undefined) and the
switch statement does not have a default path. So what should the switch
do? - Nothing!!!

Quote:

/* Premium Insurance Cost*/

#include <iostream
using namespace std;
int main ()

{
int age;
int ticket;
int car;
double result;
//char choice;
cout << " How many ticket do you have?";
cin >> ticket;
cout << " Your current age is:";
cin >> age;
cout << " The value of your car is:";
cin >> car;

//switch (choice)
choice is undefined!!!
//{
//case 1:
should be "case '1':" since choice is of type char and you probably want

to compare it to a character constant.
Quote:
if ((ticket == '1') && (age >= '16' || age <= '25')){
This should be "if ((ticket == 1) && (age >= 16 || age <= 25)){"

since ticket and age are of type int and you rather want to compare them
to a number instead of comparing them to a (multi-character)
character constant.

Valentin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Wed May 03, 2006 9:06 pm    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote

Mauricio ha scritto:
Quote:
int age;
int ticket;
int car;
double result;

snip

if ((ticket == '1') && (age >= '16' || age <= '25')){

C++ is not a scripting language where you can freely compare integers
with strings... Wink As ticket and age are integers, you had to write:

if ((ticket == 1) && (age >= 16 || age <= 25)){

'1' is a character constant whose value is 49 (the ASCII code of the
character '1', if your computer uses ASCII, of course). '16' is in fact
a "multi-character" constant, whose interpretation is implementation
dependent, therefore the warning (if you are curious, on my platform it
has value 12598).

About the switch, it's clear that it can't work, for two reasons:

1) choice is uninitialized (there's no statement that reads choice from
cin)

2) as choice is a char (and NOT an int) then you should replace

case 2:

with

case '2':

for the opposite reason described above.

HTH,

Ganesh

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Carlos Moreno
Guest





PostPosted: Wed May 03, 2006 9:06 pm    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote

Mauricio wrote:
Quote:
I'm having problem with my homework. I have to use if/else if and
switch. If I only use if the programs run, and I get some sort of
result incorrect but something. However if I used switch the program
ask for the input and return 0 without any result ?????
other thing that I am getting is "warning: multi-character character
constant"

The compiler is telling you exactly what the problem is: look up
"character constants" or "character literals" in your book; you'll
notice that the single-quote character is used to denote a char for
which you're giving it a value (as in, written right there in the
code, as opposed to obtained when the program is running).

An example of use is: if (answer == 'y')

But then, notice that since it is a literal *character*, you're not
allowed to put a *sequence* of characters. For instance, the following
would not be valid: if (answer == 'yes')

Now, look at your code, in the line where the compiler is telling you
there is the error -- see something strange?


But your problem goes a bit deeper than this: you're misusing the
character constants. The variables age and tickets are *integer*
numbers, and not characters. One thing is a value of 1, and another
thing is *the character '1'*. Your variables contain numberic values.
Therefore, you should be comparing against numeric values, and not
against character constants.

Quote:
if ((ticket == '1')

ticket could contain, if anything, a value of 1, and not '1'


As an additional suggestion, instead of trying all the combinations
of all possibilities, why not considering the effect of each factor
individually? That is, consider what happens depending on the age;
then consider what happens depending on the number of tickets....
Then, at the end you compute the premium, based on what you obtained
before. That way, you need only three ifs instead of 8 (or two ifs
instead of four, if only two factors)

HTH,

Carlos
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed May 03, 2006 9:06 pm    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote

Mauricio wrote:
Quote:
I'm having problem with my homework. I have to use if/else if and
switch. If I only use if the programs run, and I get some sort of
result incorrect but something. However if I used switch the program
ask for the input and return 0 without any result ?????

This is indeed a result of the following:

Quote:
other thing that I am getting is "warning: multi-character character
constant"

General rule: never ignore a warning you don't understand.

There are a few problems with your code which I won't go into, but one thing
for the future: when posting code, always try to remove everything that is
not necessary to demonstrate the problem. In your example, you should e.g.
have removed the user-input operations, settin your variables to fixed
values. If that had changed anything, you would know that the problem is
probably there.

Now, let's look at the code:

Quote:
int age;
int ticket;

[...]

Quote:
if ((ticket == '1') && (age >= '16' || age <= '25')){
[...]


This is the very line (I can guess that but you should have said that!)
where the warning occurs. The point is that '1' is a character while just 1
is a number. Now, a character is also an integral type, therefore the first
comparison is okay for the compiler. The second and third use this
multi-character character constant, who's value (IIRC) is
implementation-defined, i.e. something that is compiler dependant and not
universally useful and hence the warning by the compiler.

BTW:
// try this
int i = '1';
char c = '1';
std::cout << "char:'" << c << "' int:'" << i << "'\n" << std::flush;

And you will understand why your code behaved weirdly.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Allan W
Guest





PostPosted: Thu May 04, 2006 10:21 am    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote

Mauricio wrote:
Quote:
I'm having problem with my homework. I have to use if/else if and
switch. If I only use if the programs run, and I get some sort of
result incorrect but something. However if I used switch the program
ask for the input and return 0 without any result ?????
other thing that I am getting is "warning: multi-character character
constant"

In the future, try to strip your program down to the smallest program
that shows the error. And when you're posting a warning or error, tell
us what line(s) the error is on.

But I can see the problem without even putting your program into a
compiler. In fact, the warning was the biggest clue.

The short answer is: quit using apostrophes (the ' character) around
your numbers! For instance, change
if ((ticket == '1') && (age >= '16' || age <= '25')) {
to
if ((ticket == 1) && (age >= 16 || age <= 25)) {


Here's a more detailed explanation:

There are several different types of "literals" in C++.
3 -- is an "integer-literal" with the value 3. You
can use it in expressions such as
age = 3;
3.0 -- is a "floating-literal" with the value 3. You
can use it in expressions such as
result = 3.0;
"Three" -- is a "string-literal" with five characters plus
the terminating null character. You can use it
in expressions such as
std::cout << "Three" << std::endl;
(or, sinc you've use "using namespace std;" you
can just write)
cout << "Three" << endl;
'X' -- is a "character-literal" which is the letter X.
In some contexts you can use it as if it was an
integer with the same value as the character
code for an X (this is 88 in ASCII, other values
on non-ASCII systems).

On some computers, the data type that holds single characters is
actually able to hold more than one character at the same time,
but this is not portable. On those systems, you could write
'AB'
and your character-literal would contain both the letters A and B
(in that order). But this is still different than a string.

Now look at this statement from your program (this is just one
example -- you made this mistake in quite a few places):

if ((ticket == '1') && (age >= '16' || age <= '25')) {

The first part of this expression compares the value of integer
variable ticket to the character code for the digit 1. (This is
49, on ASCII systems -- your system might be different). If you
wanted to compare it to the INTEGER value 1, you should have
written
(ticket==1)

The rest of the expression has even bigger problems. We're comparing
the value of integer variable age to two different numbers -- but
what number is it comparing to? On the system I use, the value of
'16' is 12598 -- that works out to 49*256+54 (49 is the code for
'1' and 54 is the code for '6') and the value '25' is 12853 for
similar reasons.

Change this line to
if ((ticket == 1) && (age >= 16 || age <= 25)) {
and then it will work a LOT better... and the warning will be gone.

Your program still has a few other problems... but I'm sure you'll be
able to work those out on your own, once you have fixed the problem
with the apostrophes.

Good luck.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Allan W
Guest





PostPosted: Thu May 04, 2006 10:21 am    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote

Bob Hairgrove wrote:
Quote:
Single quotes are used for single char values. Double quotes are used
to denote an array of chars which gets an automatic delimiting '\0' by
the compiler. If you write '25', however, only the '2' is seen by your
code (or is it the '5'? At any rate, one of them is dropped).

It depends on the platform. That's why multiple-character char values
are
legal -- and why most (but not all!) platforms warn when they see them.

int x = 'ABCD';
std::cout << x;

On Microsoft Visual Studio .Net 2003, this writes 1094861636 --
which is 0x41424344 -- and it's no coincidence that 'A'==0x41,
'B'==0x42, 'C'==0x43, and 'D'==0x44, and sizeof(int)==4.

Furthermore, even when compiled at maximum warning level,
there is no warning message. (On the other hand,
int x='ABCDE';
gets error C2015: too many characters in constant).


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
James Dennett
Guest





PostPosted: Thu May 04, 2006 10:21 am    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote

Bob Hairgrove wrote:

[snip]

Quote:
Single quotes are used for single char values. Double quotes are used
to denote an array of chars which gets an automatic delimiting '\0' by
the compiler. If you write '25', however, only the '2' is seen by your
code (or is it the '5'? At any rate, one of them is dropped).

'25' is of type int, and while not portable, is likely to
be equal to either 2*256+5 (517) or 2+5*256 (1282) depending
on your implementation/platform.

-- James

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
James Dennett
Guest





PostPosted: Thu May 04, 2006 10:21 am    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote

Bob Hairgrove wrote:

[snip]

Quote:

Single quotes are used for single char values. Double quotes are used
to denote an array of chars which gets an automatic delimiting '\0' by
the compiler. If you write '25', however, only the '2' is seen by your
code (or is it the '5'? At any rate, one of them is dropped).

My previous response to this was not quite right
(which is another way of saying that I was wrong).

The value of '25' is typically '2'*256+'5' or '2'+256*'5',
not 2*256+5 or 2+256*5. (Using these multicharacter
constants is a portability problem waiting to happen.)

-- James

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Thu May 04, 2006 9:22 pm    Post subject: Re: Can I get some help please "warning: multi-character c Reply with quote

Bob Hairgrove wrote:

[...]
Quote:
Single quotes are used for single char values. Double quotes
are used to denote an array of chars which gets an automatic
delimiting '\0' by the compiler. If you write '25', however,
only the '2' is seen by your code (or is it the '5'? At any
rate, one of them is dropped).

It's worse than that. First, if there is more than one
character, the type is int, not char. Secondly, the value of
the constant is implementation defined; it will generally be
either ('2'<<Cool|'5', or ('5'<<Cool|'2', but I think other values
are also acceptable.

To tell the truth, I've never understood why C allowed
multi-byte character constants in the first place.

--
James Kanze GABI Software
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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Maeder
Guest





PostPosted: Fri May 05, 2006 10:21 am    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote

"Mauricio" <mundurragaj (AT) gmail (DOT) com> writes:



Quote:
I'm having problem with my homework. I have to use if/else if and
switch. If I only use if the programs run, and I get some sort of
result incorrect but something. However if I used switch the program
ask for the input and return 0 without any result ?????

The code you posted is way too complex to get right IMHO. So don't be
surprised if you can't get it right, either.

I also think the indentation is far too wide. Try two or three
characters. As it is now, I can't even read closely coupled chunks
without looking around.

Please write a simpler program and try to get it working. For example,
if I understand the above text right, your problem is not related to
whether the values for ticket, age and car are read from the keyboard
or hard-coded in the program.

If I am right, try to first get the switch working with hard-coded
values. And with the minimal number of cases (probably 2).

Quote:
other thing that I am getting is "warning: multi-character character
constant"

This one is easy: Your programs contains '16' and '25'. Character
literals are typically written as <single-quote> <character>
<single-quote>. E.g. '1', 'a'. The two things mentioned above have two
characters between the single quotes, not just one; they are therefore
called multi-character character constants.

I think that you actually want to compare age with the number 16. For
this, simply remove the quotes around 16; e.g.:

if (age >= 16)

Quote:

/* Premium Insurance Cost*/

#include <iostream
using namespace std;
int main ()

{
int age;
int ticket;
int car;
double result;
//char choice;
cout << " How many ticket do you have?";
cin >> ticket;

Another remark: this input operation can fail. Always first check the
success of the input operation (or any other operation that can fail,
for that matter) and only use the value (apparently) read when you
know the operation succeeded. E.g.:

if (cin >> ticket)
{
// use ticket
}
else
{
// deal with input failure
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Sat May 06, 2006 2:21 pm    Post subject: Re: Can I get some help please "warning: multi-character cha Reply with quote

Allan W wrote:

[Just a couple of nits. Your explination is actually quite
good.]
Quote:
Here's a more detailed explanation:

There are several different types of "literals" in C++.
3 -- is an "integer-literal" with the value 3. You
can use it in expressions such as
age = 3;
3.0 -- is a "floating-literal" with the value 3. You
can use it in expressions such as
result = 3.0;
"Three" -- is a "string-literal" with five characters plus
the terminating null character. You can use it
in expressions such as
std::cout << "Three" << std::endl;
(or, sinc you've use "using namespace std;" you
can just write)
cout << "Three" << endl;
'X' -- is a "character-literal" which is the letter X.
In some contexts you can use it as if it was an
integer with the same value as the character
code for an X (this is 88 in ASCII, other values
on non-ASCII systems).

The type is also a key difference. The literals 3.0 and 3 have
the same "value" (for the usual, everyday meaning of value), but
have different types.

It's important to realize that different types use different
interpretations of the underlying bits to represent the value.
Arguably, 3, 3.0 and '3' represent the same value; their size
and bit patterns are, however, different. In the case of C++,
the issue is further clouded by the fact that C++ has no real
character type: '3' is still an integral type, but not the
same integral type as 3, and also not the same value -- as you
say, it's value is the value of the character code (which is
still a number, and not a character).

Of course, how different bit patterns are interpreted is a
question of convention. Sometimes, the convention is practially
imposed: the C++ standard requires integral values to be
represented in a base 2 notation. Othertimes, hardware offers
direct suppport -- most modern platforms have hardware floating
point support, for example. In the case of characters, the
issue is a bit more complex: the conventions are established by
the software in the windowing drivers or in the printer
hardware; on at least some systems, the conventions can vary
according to the user environment, and it isn't rare for the
system to tell the program that one convention is in effect, but
to use a different one for display in the windowing driver, and
yet a third in the printer. (For the original poster: don't
worry about this yet! You can get a lot of work done,
especially in an English speaking environment, without it ever
being a problem. On the other hand, in a multilingual,
networked environment, it can drive you nuts, because you have
no control over so many of the factors.)

Quote:
On some computers, the data type that holds single characters is
actually able to hold more than one character at the same time,
but this is not portable. On those systems, you could write
'AB'
and your character-literal would contain both the letters A and B
(in that order). But this is still different than a string.

It's worse than that. Historically, C promoted everything in an
expression to an int. And character literals had type int,
which typically could hold more than one character. C++ broke
with C here, because you really do want character literals to
overload differently than int's: "cout << ' '" should output
" ", and not "32". But it only did a minimal break:
multi-character literals were still supported, with exactly the
same semantics as in C (which is to say: implementation defined
semantics). Thus, '3' has a type char, and an integral value of
51 (0x33), but '32' has a type int, and an integral value of
13106 (0x3332) on my machines -- more importantly, overload
resolution prefers char for '3', but int for '32', so that "cout
<< '3'" outputs "3", but "cout << '32'" outputs 13106. For the
orginal poster: this brings us back to the conventions
concerning the representation, above. The convention for <<
char is to treat the set of bits (the integral value) as a
character code, and output the corresponding character; the
convention for << int is to treat the set of bits as a signed
integer, and output the value of that integer. (This convention
is defined by the C++ standard in the case of a << operator
where the left hand operand is an ostream.)

Of course, if the execution character set includes multibyte
characters, the issue becomes even more clouded. Supposing
UTF-8 as the execution (and source) character set, something
like 'é' is a multibyte character constant: type int, and << 'é'
would output something like "50089". (I say would, because none
of my compilers support UTF-8 as a character set.) On the other
hand, if the source character set is UTF-8, and the execution
character set is ISO 8859-1, then '\xC3\xA9' is a single byte
character constant, and << '\xC3\xA9' should output "é".

--
James Kanze GABI Software
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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.