sentinel

D

d.j.

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
 
S

Sam Holden

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.
 
D

d.j.

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 said:
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.
 
J

John Harrison

d.j. said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top