sentinel

Discussion in 'C++' started by d.j., Apr 22, 2004.

  1. d.j.

    d.j. Guest

    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
    d.j., Apr 22, 2004
    #1
    1. Advertising

  2. d.j.

    Sam Holden Guest

    On Wed, 21 Apr 2004 20:03:10 -0400, d.j. <> 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
    Sam Holden, Apr 22, 2004
    #2
    1. Advertising

  3. d.j.

    d.j. Guest

    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. <> 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.
    >
    d.j., Apr 22, 2004
    #3
  4. "d.j." <> wrote in message
    news:xrEhc.317$...
    > 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
    John Harrison, Apr 22, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rzed
    Replies:
    2
    Views:
    287
  2. Will McGugan

    iter(callable, sentinel)

    Will McGugan, Jul 27, 2006, in forum: Python
    Replies:
    3
    Views:
    445
    Simon Forman
    Jul 27, 2006
  3. Ben Finney

    Sentinel values for special cases

    Ben Finney, Oct 27, 2006, in forum: Python
    Replies:
    7
    Views:
    342
    Gabriel Genellina
    Oct 28, 2006
  4. Mike

    Adding a Sentinel

    Mike, Oct 31, 2004, in forum: C Programming
    Replies:
    3
    Views:
    429
    -berlin.de
    Nov 1, 2004
  5. shan

    sentinel control loops

    shan, Oct 20, 2005, in forum: C Programming
    Replies:
    13
    Views:
    909
    Mark McIntyre
    Oct 24, 2005
Loading...

Share This Page