Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sentinel

Reply
Thread Tools

sentinel

 
 
d.j.
Guest
Posts: n/a
 
      04-22-2004
This program will not let me use the charater as the sentinel please
advise... The error I get is can not convert char to char*


# include <iostream.h>

int main()
{
char stname [12]; //student name
int code; // veteran or not 1 or 2
int units; //number of units student register for
int dep; // number of dependents
float fin; //finacial assistance
int vets =0; //verterans
int nonvet =0; //non verterans
int fulvet=0;// full time vet
int pvet=0;// part time vet
char end;
while (stname !=end)
{
cout << "\nEnter Students name\n";
cin >> stname;
if (stname == end)
break;
cout << "\Enter code\n";
cin >> code;
cout << "\nEnter number of units\n";
cin >> units;
cout << "\nEnter number of dependents\n";
cin >> dep;

if (code == 2)
vets++;
else nonvet++;

if (code ==2 && units>=15)
fulvet++;

if (code ==2 && units <15)
pvet++;



if (code == 2 && units <15 && dep < 2)
fin += units * 20.00;

if (code == 2 && units <15 && dep >= 2)
fin += units * 23.00;

if (code == 2 && units >=15 && dep >= 2)
fin += units * 30.00;

if (code == 2 && units >=15 && dep < 2)
fin += units * 27.00;
}
cout << "Total full time veterans students " << fulvet << " "<<'\n';
cout << "Total part time veterans students " << pvet << " "<<'\n';
cout << "Total amount " << fin << " " << '\n';




return 0;
}


Thanks in advance

Dario

 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      04-22-2004
On Wed, 21 Apr 2004 20:03:10 -0400, d.j. <(E-Mail Removed)> wrote:
> This program will not let me use the charater as the sentinel please
> advise... The error I get is can not convert char to char*
>

[snip]
> char stname [12]; //student name

[snip]
> char end;
> while (stname !=end)


Those are different types you can't just compare them.

You could do,

stname[0] == end

or

char *end;
strcmp(stname,end)==0

But it'll crash anyway since you haven't initialised stname or end.

Since it's C++ you should really do:

std::string stname;

anyway.

> if (stname == end)


same here.

[snip rest of code]

There is not a global shortage of white space. Indent your code so
that it is readable.

--
Sam Holden
 
Reply With Quote
 
 
 
 
d.j.
Guest
Posts: n/a
 
      04-22-2004
Mr. Holden could you please elaborate a lil more I don't understand.
When you say they are different type what do you mean I assumed they
were the same char. Why would I

[snip] stname[0] == end

when I am entering the students name, and what does the "*" and strcmp in

[snip] char *end;
strcmp(stname,end)==0

mean.

we just started c++ in class and I want to learn this to my full
ability. So your help is greatly appreciated..

Thanks in advance

Dario

Sam Holden wrote:

> On Wed, 21 Apr 2004 20:03:10 -0400, d.j. <(E-Mail Removed)> wrote:
>
>>This program will not let me use the charater as the sentinel please
>>advise... The error I get is can not convert char to char*
>>

>
> [snip]
>
>>char stname [12]; //student name

>
> [snip]
>
>>char end;
>>while (stname !=end)

>
>
> Those are different types you can't just compare them.
>
> You could do,
>
> stname[0] == end
>
> or
>
> char *end;
> strcmp(stname,end)==0
>
> But it'll crash anyway since you haven't initialised stname or end.
>
> Since it's C++ you should really do:
>
> std::string stname;
>
> anyway.
>
>
>>if (stname == end)

>
>
> same here.
>
> [snip rest of code]
>
> There is not a global shortage of white space. Indent your code so
> that it is readable.
>


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      04-22-2004

"d.j." <(E-Mail Removed)> wrote in message
newsrEhc.317$(E-Mail Removed)...
> Mr. Holden could you please elaborate a lil more I don't understand.
> When you say they are different type what do you mean I assumed they
> were the same char. Why would I
>
> [snip] stname[0] == end
>
> when I am entering the students name, and what does the "*" and strcmp in
>
> [snip] char *end;
> strcmp(stname,end)==0
>
> mean.
>
> we just started c++ in class and I want to learn this to my full
> ability. So your help is greatly appreciated..
>
> Thanks in advance
>
> Dario
>


Dario, you need to read a book on C++. You don't understand what a type is,
or what a pointer is, or the difference between an element and an array of
elements, or simple string handling functions, these are big gaps in your
C++ knowledge.

Now I think that all Sam suggestions were wrong because he misunderstood
what you are trying to do. I suspect that what you want is for the user to
type in the string "end" and for your loop to break at that point. If that
is true then you don't understand the difference between a variable name and
a literal value either.

Here's a few brief answers to your questions, but really you need to get a
book and study. The gaps in your knowledge are a bit too big to answer in a
newsgroup post.

Nevertheless here's a very, very brief answer to some of the questions
you've raised.

1)

char end;
char stname[12];

end is a char variable, stname is an array of twelve chars, This means they
are of different type. char and array of char are not the same thing. The
type of an object of variable determines what you can do with that object of
variable. You can compare to chars for equality

char x;
char y;
....
if (x == y)
cout << "x and y are equal\n";

but you cannot compare an array for equality with anything. That's just the
rules of C++.

2)

char* end;

The * makes end a pointer to char (just like the [12] make stname an array
of char). I'm not going to try an explain pointers to you. Read a book if
you're interested, its one of the most difficult things that newbies have to
learn about.

3) strcmp is that way of comparing C style strings. C style strings are
represented as arrays of char. Remember that I said you cannot compare
arrays for equality. Well strcmp is what you do instead

char s1[12];
char s2[12];
....
if (strcmp(s1, s2) == 0)
cout << "s1 and s2 are equal\n";

You must include <string.h> to use strcmp.

4)

char end;

The above is a variable called end

"end"

The above is a value, the value is a C string consisting of the characters
'e', 'n', and 'd'.

char e;

The above is a variable called e.

'e'

The above is a character value (the letter e).

int two;

The above is a variable called two.

2

The above is the integer value two.

Values and variables are not the same thing. Just because you called a
variable end, does not mean it has the value end. A variable can have any
value, the value that you assign to it.

Now as I said, I strongly suspect that what you want to do is quit your loop
when the user types in the name "end". So you need to compare your the value
of your stname variable with the value "end". Like this

if (strcmp(stname, "end") == 0)
break;

You do not need a variable called end, you need a value.

HTH
john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sentinel values for special cases Ben Finney Python 7 10-28-2006 02:00 AM
iter(callable, sentinel) Will McGugan Python 3 07-27-2006 06:07 PM
DVD Verdict reviews: THE SENTINEL: THE COMPLETE FIRST SEASON and more! DVD Verdict DVD Video 0 04-26-2006 08:25 AM
ConfigParser - add a stop sentinel? rzed Python 2 01-12-2005 07:17 PM
win xp serious error - sentinel DLL? hugh jass Computer Support 4 11-04-2003 04:38 PM



Advertisments