Newbie question (Please be kind)

This is a discussion on Newbie question (Please be kind) within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm taking a C++ class, and my teacher isn't really clear. Our assignment deals with answering Yes or No ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    27

    Question Newbie question (Please be kind)

    Hi, I'm taking a C++ class, and my teacher isn't really clear. Our assignment deals with answering Yes or No questions to figure out a bill for work done. Also involved we have to test the input to make sure that it's there.

    Here's what I would like to do:


    Variables:
    char xray;
    char cavity;
    char cleaning;
    float xbill;
    float cavbill;
    float cbill;
    float totalbill;
    bool dataOk;

    cout << " Was cleaning performed? (Y or y = yes, N or n = no): ";
    cin >> cleaning;

    if ( cleaning = ("Y"|| "y") || "N"||"n")
    dataOk = true;
    else
    cout << "Invalid Data";

    Then:

    if ( cleaning = "Y" || "y")
    cbill = 50;
    else
    cbill = 0;


    Though my compilier doesn't encounter any problems, there is not testing performed for data, and the identifier cbill always equals 50.

    Any help would be greatly appreciated!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Use CODE tags.

    Your if statements have alot of problems.

    Code:
    if ( cleaning = ("Y"|| "y") || "N"||"n") // This is an assignment operator =
                                             // You want the comparison operator ==
                                             // Even with that, this statement is always true. 
                                             // You must compare in each statement. Like this:
    
    if (cleaning == 'Y' || cleaning == 'y' || cleaning == 'N' || cleaning ==  'n')
    Secondly, you can clean up your code if you declare variables of similar type on one line. Especially if you aren't initializing them.

    Code:
    char a,b,c;
    int x,y,z;
    Last edited by SlyMaelstrom; 02-15-2006 at 06:43 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    Quote Originally Posted by SlyMaelstrom
    Use CODE tags.

    Your if statements have alot of problems.

    Code:
    if ( cleaning = ("Y"|| "y") || "N"||"n") // This is an assignment operator =
                                             // You want the comparison operator ==
                                             // Even with that, this statement is always true. 
                                             // You must compare in each statement. Like this:
    
    if (cleaning == "Y" || cleaning == "y" || cleaning == "N" || cleaning ==  "n")
    Secondly, you can clean up your code if you declare variables of similar type on one line. Especially if you aren't initializing them.

    Code:
    char a,b,c;
    int x,y,z;
    When i put the above code in, i get this response: "ISO C++ forbids comparison between pointer and integer"

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    A character literal uses single quotes: 'y'. A string literal uses double quotes: "y". You want to compare to the character literal.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    What code? The last statement in the first code tag? That's because I let something slip up. I didn't think about the quotes.

    Characters are defined under single quotes. 'c' and 'g' and '+' and so on are characters. If I were to say "c" or "g" or "+", now we're talking about strings. So when your comparing cleaning to characters, they must be under single quotes.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    char xray;
    char cavity;
    char cleaning;
    float xbill;
    float cavbill;
    float cbill;
    float totalbill;
    bool dataOk;

    cout << " Was cleaning performed? (Y or y = yes, N or n = no): ";
    cin >> cleaning;

    if (cleaning == 'Y' || cleaning == 'y' || cleaning == 'N' || cleaning == 'n')
    dataOk = true;
    else
    dataOk = false;

    if ( cleaning = 'Y' || 'y')
    cbill = 50;
    else
    cbill = 0;


    With the above code i'm hoping to input an answer, from that answer decide if it's true or false, then assign a value to cbill depending on the answer.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    You didn't fix your second if statement. Not to mention the fact that your second if statement doesn't check your dataOk variable. Basically making your first if statement pointless.
    Sent from my iPadŽ

  8. #8
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    you can also add:

    Code:
    var=tolower(var);
    After you take their input so it turns it lower case. I think it makes it simpler because then you dont have to put:

    Code:
    if (cleaning == 'Y' || cleaning == 'y' || cleaning == 'N' || cleaning == 'n')
    Just my 2 cents.
    -Kyle

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Low level programming classes usually don't like you using convenient functions that weren't taught in the class. Not that tolower() isn't good to know, I just wouldn't suggest he uses it since it's being graded.
    Sent from my iPadŽ

  10. #10
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    alright, I can see what you mean. Im in school too, but I dont get graded for my coding (As I am in 8th Grade). Actually, most of the kids im my school dont even know HTML.
    -Kyle

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Most of them never will. It's not a very essential life skill.
    Sent from my iPadŽ

  12. #12
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    No, they will. As a class in school they are forced to take it unless they have a learning lab or something. I take web design too, but I do alot more advanced stuff. Where they are learning the code, I am learning actual design elements and how to make my pages look and operate better. I self taught myself HTML and C++. I learned HTML when I was 9 and C++ Just a few months ago. I just started getting into OpenGL. I picked up a book "Begining OpenGL Game Programming" and Im learning some good things from it.
    -Kyle

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Everyone in your school is required to learn HTML? Where the hell did priority go in schools these days?
    Sent from my iPadŽ

  14. #14
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    Yeah, but is only a quarter of the year. I go to a good school on Long Island. They keep there prioritys straight.

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    HTML is way more important than spelling.
    Sent from my iPadŽ

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  4. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  5. very newbie question: how to copy files
    By webwesen in forum C Programming
    Replies: 26
    Last Post: 04-25-2002, 03:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21