Thread: Help with logical operators and data types

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Help with logical operators and data types

    Hi Everyone,

    First off, I'm new to the board, so hello. Second, I'm new to programming in C, so this might seem like a basic question.

    As I'm reading/learning the language, I'm trying to create a basic program that functions very well. I this case I'm trying to make a tip calculator.

    I've gotten the main bits to work, but I'm wondering how I can compare a variable to its own data type.

    For instance, everything I'm reading about logical operators uses values as an example:

    int main ()

    #include <stdio.h>
    #include <stdlib.h>

    int main() {

    int newInt

    newInt = 3

    if (newInt == 3) {
    printf ( "etc etc etc...");
    }
    I want to compare the variable to evaluate whether or not it is a the correct data type.

    For instance, I'd like to code things so that if the bill amount the user enters is not a number, they get an error.

    Is there an equivelent to if (newInt != int);

    Please help! I remember being able to do this in Pascal, which was the language I learned many, many years ago. How do I do it in C?

    Thanks to everyone for taking a look at my problem!

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Simple. There's no way. Actually you don't need such feature for your purpose(just to validate input!).
    Read a line with fgets() and parse with strtol.
    man page fgets section 3
    man page strtol section 3
    You could also search the forum.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    newInt will always be an int, because that's what you declared it to be. The words you are looking for are "input validation".

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by CJT212 View Post
    Hi Everyone,

    First off, I'm new to the board, so hello. Second, I'm new to programming in C, so this might seem like a basic question.

    As I'm reading/learning the language, I'm trying to create a basic program that functions very well. I this case I'm trying to make a tip calculator.

    I've gotten the main bits to work, but I'm wondering how I can compare a variable to its own data type.

    For instance, everything I'm reading about logical operators uses values as an example:

    int main ()

    #include <stdio.h>
    #include <stdlib.h>

    int main() {

    int newInt

    newInt = 3

    if (newInt == 3) {
    printf ( "etc etc etc...");
    }
    I want to compare the variable to evaluate whether or not it is a the correct data type.

    For instance, I'd like to code things so that if the bill amount the user enters is not a number, they get an error.

    Is there an equivelent to if (newInt != int);

    Please help! I remember being able to do this in Pascal, which was the language I learned many, many years ago. How do I do it in C?

    Thanks to everyone for taking a look at my problem!
    You're in a slightly different world here. I too started with Pascal many moons ago and two things gave me fits when I moved over to C (because Borland so successfully screwed up Pascal nobody in their right mind would use it anymore)... First, the lack of a string type, using char arrays and null terminators instead and second, the lack of type validation and checking. It takes a bit, but you'll get the hang of it... Just remember not to compare the two languages, learn C as a separate thing.

    The input validation you are looking for is to get the int using whatever console input function you select for the task... then check it aftwards to be sure it's valid for your code. Usually this is just a matter of checking it against the lowest and highest numerical value you can tolerate. Essentially you need range checking... but you have to code it yourself.

    In pseudo code...
    Code:
     input loop
      {  get keyboard data
          test against allowable values
       exit if ok.   }
    Also a couple of minor issues with your code...

    First, please learn to use the code /code tags when posting. People here are real picky about that.

    Second, main should always be declared as int main (void) and it should always return a value (usually 0) as an exit code for the program.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One weird thing that I observed is this extra line at the top:
    Code:
    int main ()
    Then there's the missing closing brace, but perhaps these are just carelessness in providing an example. Next time, if you do want to provide a code example, do it properly, with indentation and in [code][/code] bbcode tags. This way it is more likely that people would concentrate on what is actually wrong with your code rather than pointing out things that don't matter.

    Quote Originally Posted by CommonTater
    Second, main should always be declared as int main (void) and it should always return a value (usually 0) as an exit code for the program.
    Note that the void part is optional, and if you are compiling with respect to C99, returning 0 from main is also optional.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Awesome

    Hey everyone,

    Thank you for taking the time to help out. I'm going to try this tonight. I'll let you know how it comes out.

    As per my faux pas in posting, apologies, won't happen again. Whoever said it was carelessness on the part of my just providing an example was right. I'll present it better next time.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Note that the void part is optional, and if you are compiling with respect to C99, returning 0 from main is also optional.
    And when you are not compiling "with respect to c99" ??

    It is harmless to do these things in C-99 ... it is however harmful to not do these things in earlier compilers.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CommonTater View Post
    And when you are not compiling "with respect to c99" ??
    Is this a legitimate question? There are compilers out there that are still C89-only with no C99 mode or official support. Microsoft's Visual Studio, for instance. (I guess I shouldn't say _no_ C99 support, since it does have the bits of C99 that came from, or match, or however you want to phrase it, C++.)

    But your point that it's harmless is true.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Homework

    Hey Again.

    Just for the hell of it, I'm posting the program I'm working on as a C practice project. Everything I'm doing right now is console so that I get the hang of real coding and syntax before moving on to more object oriented endeavors.

    Tater, in this example, I've implemented your range solution. However, the program still freaks out if the user enters letters instead of numbers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    
        int rating, statusQuo, quit;
        float bill, tip, badTip, okTip, goodTip;
    
    
        badTip = 0.1;
        okTip = 0.15;
        goodTip = 0.2;
        statusQuo = 1;
    
        printf ( "\n\*WELCOME TO THE EASY TIP CALCULATOR!\*\n");
    
        while ( statusQuo == 1) {
    
        printf("\n\nPlease enter the amount of your bill:  $");
        scanf ("%f", &bill );
    
        if (bill <= 0 || bill > 1000000) {
        printf ( "\nI'm sorry, but no restaurant bill would ever be that amount.\n\nPlease enter a REAL amount:  $");
        scanf ("%f", &bill);
        }
    
        if ( bill >0 && bill < 1000000) {
        printf ( "\n\nPlease enter:\n\n1\. For bad service\n2\. For Okay service\n3\. For \"I want to date the waitor\"\n\nRating:  ");
        scanf ("%d", &rating);
        }
        if (rating < 1 || rating > 3) {
        printf ( "\nLook, we have restrictions around here.  Please enter a 1, 2 or 3:  ");
        scanf ( "%d", &rating);
        }
    
        if ( rating == 1 ) {
            tip = (bill * badTip);
        }
        else if ( rating == 2) {
            tip = (bill * okTip);
        }
        else if ( rating == 3) {
            tip = (bill * goodTip);
        }
    
        printf( "\nYou should tip:  $%.2f\n ", tip);
        printf ( "\nPlease enter 1 to quit or 2 to continue:  ");
        scanf ( "%d", &quit);
        printf ( "\n");
        if ( quit == 1) {
        (++statusQuo);
        printf ( "Thanks for using my program! Hope you found it helpful.\n" );
        }
        }
        return 0;
    }
    Just posted this in case anyone is nice enough to grade my homework. It's been a while since I've spent a good amount of time coding, so even though the program works for the most part, I'd love some feedback on implementation, coding practices, neatness...etc.

    I also started to do this modularly by turning most of the code into functions, but the local nature of variables is throwing me off a little. I think to really start using functions well, I need to get deeper into pointers, which is where I am in the book I'm reading. Thoughts?

    Also, I know it's not commented. Since it's small and I'm the only one working on it, I didn't bother. But I should go back and do it at some point.

    Finally, in regard to the Main function and the compiler I'm using. Most examples on this site, I'm noticing, use

    Code:
    int main () {
    However, the book I'm learning from uses:

    Code:
    int main (int argc, const char * argv[]) {
    I've actually found this small sticking point somewhat confusing. I've also seen the (void) version. The GCC compiler seems to take any of these versions, so it hasn't been much of a hangup, but if anyone has a definitive answer, I'm all ears.

    Thanks again for the help.
    Last edited by CJT212; 12-10-2010 at 04:50 PM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Is this a legitimate question? There are compilers out there that are still C89-only with no C99 mode or official support. Microsoft's Visual Studio, for instance. (I guess I shouldn't say _no_ C99 support, since it does have the bits of C99 that came from, or match, or however you want to phrase it, C++.)

    But your point that it's harmless is true.
    There are people here still using Turbo-C... 16 bit compiler from the 1980s... yes it's a legitimate question.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CommonTater View Post
    There are people here still using Turbo-C... 16 bit compiler from the 1980s... yes it's a legitimate question.
    Haha... despite reading the post, deciding to reply, quoting your reply, reading my reply, etc. it didn't dawn on me until just now that you asked "when you are not" instead of "when are you not". Oops.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Haha... despite reading the post, deciding to reply, quoting your reply, reading my reply, etc. it didn't dawn on me until just now that you asked "when you are not" instead of "when are you not". Oops.
    No worries. You should see some of the misreads I've pulled over the years.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CJT212 View Post
    Tater, in this example, I've implemented your range solution. However, the program still freaks out if the user enters letters instead of numbers.
    The "f" in "scanf" means "formatted". If the input does not conform, reading stops on the current input, and more importantly from your perspective, it doesn't go away. Which means when you try to read a number again, well, there's that letter still. One of the FAQ's here (and just about everywhere) is "what do I do about this". You can either (1) read in a line and validate it yourself (that is to say, use fgets+sscanf), or (2) once you realize that there's bad input, to suck the input stream dry before trying gain (the while getchar()!='\n' trick).


    Quote Originally Posted by CJT212 View Post
    I also started to do this modularly by turning most of the code into functions, but the local nature of variables is throwing me off a little. I think to really start using functions well, I need to get deeper into pointers, which is where I am in the book I'm reading. Thoughts?
    The local nature of variables is exactly what you want to make functions happen. You can pass variables into a function, and do your scratch work knowing that the scratch work doesn't affect the "real" variables in your main program and the main program only gets the answer.

    Quote Originally Posted by CJT212 View Post
    However, the book I'm learning from uses:

    Code:
    int main (int argc, const char * argv[]) {
    I've actually found this small sticking point somewhat confusing. I've also seen the (void) version. The GCC compiler seems to take any of these versions, so it hasn't been much of a hangup, but if anyone has a definitive answer, I'm all ears.

    Thanks again for the help.
    Those are your two choices, basically: you can take no arguments, or you can take two arguments (the number of command-line arguments and what those command-line arguments are). Obviously if you need the command-line arguments you have to use the second one. If you don't then you use whichever. (If you've got your warnings turned on, you might get "unused variable" warnings if you don't use argc/argv. If so, and you want to get rid of them, you can just switch to the first version.)
    Last edited by tabstop; 12-10-2010 at 05:14 PM. Reason: left some extra bits

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  2. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 05:19 AM
  3. Compile errors with overloading >> operators
    By GMHummerH1 in forum C++ Programming
    Replies: 1
    Last Post: 12-19-2004, 07:13 PM
  4. Binary Tree Revisited: Near Completion
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2003, 08:23 AM
  5. Replies: 5
    Last Post: 11-24-2002, 11:05 PM

Tags for this Thread