Thread: Multiplying variables but all I get is <null>

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    8

    Multiplying variables but all I get is <null>

    I'm trying to write a program to calculate meat and the prices and stuff but the program comes up with <null> embarrassingly enough I have spent over 8 hours trying to do this

    Code:
    #include <stdio.h>#include <stdlib.h>
    #define beef     1 /* multiplication factor of beef*/
    #define chicken  1.5 /*factor of chicken*/
    #define pork     2 /* factor of pork*/
    #define s1   0.5 /* state1 is close to expiry*/
    #define s2   1 /*state2 is frozen storage*/
    #define s3   1.5 /*state3 is fresh*/
    
    
    int main(int argc, char *argv[])
    {
      float amt = 0; /*amount of meat*/  
      char st = 0; /*state of meat*/
      char meat = 0; /*type of meat*/
      float price = 0; /*total price*/
      
      printf ("This program calculates price of meat. \n");
      printf ("\n Please enter type of meat ( beef, chicken, pork):");
      fflush (stdin);
      scanf ("%s",&meat);
      if ((meat != beef) || (meat != chicken) || (meat != pork))
      price, "NA";
      printf ("\n Please enter state of meat(s1, s2, s3):");
      fflush (stdin);
      scanf ("%s",&st);
      if ((st != s1) || (st != s2) || (st != s3))
      price, "NA";
      printf ("\n Please enter quantity of grams of meat (0 - 2000):");
      fflush (stdin);
      scanf ("%d",&amt);
      if ((amt < 0 ) || (amt > 2000))
      price, "NA";
      else if (amt >= 1500)
      price = (meat*st)*8;
      else if (amt >= 1000)
      price = (meat*st)*6;
      else if (amt >= 500)
      price = (meat*st)*4;
      else
      price = (meat*st)*2;
      
      printf( "\n the price is: %s", price);
    
      if (price [0] == 'N', price [1] == 'A')
      printf ("Variable is not recognized, please use provided variables");
      
      system("PAUSE");    
      return 0;
    }
    Last edited by Josh Nako; 06-04-2013 at 03:27 AM. Reason: I couldn't get rid of the error for the printf for when the price is NA

  2. #2
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    In your scanf()s you are reading your variables as if they were of different types from the ones you have defined them.

    For example, you define meat to be a char and then you read it as if it was a cstring. Or you read amt as an int, but you have defined it to be a float.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    So how do I change it? Sorry I am really new to this.

  4. #4
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by Josh Nako View Post
    So how do I change it? Sorry I am really new to this.
    I'd suggest you have a look at the documentation of the function scanf(). Here's a random source I just got from google.

    For example, you should read meat like this:

    Code:
    scanf( " %c", &meat );
    Moreover, you should make sure that the comparisons you do in your conditional statements are between variables of the same type (or at least between compatible types).

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    I tried changing it and it doesn't do anything different

  6. #6
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Well, I'm not supposed to solve your exercise for you. I think I've hinted you enough to get you started fixing your code. If you have more specific questions about things you tried and didn't work, I'll be glad to help.

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    oh okay, my bad. I'm just having heaps of trouble. Thanks for the help

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    is the way I did the multiplication and everything correct though

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Unfortunately for you there's next to nothing correct in that program. You need to go back to your book and notes and read them more carefully and give it another try.

  10. #10
    Registered User
    Join Date
    Jun 2013
    Posts
    8
    :O I tried reading my notes but I have trouble understanding and the teacher doesn't help much either all together now I have sat in this class for 16 hours trying to do this, thanks though, I'll give it another try

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, let's start with the basics:
    1. Indent/format your code properly (link). If your code is hard to read, it's easy to make mistakes, and hard to find and fix them.

    2. Use sensible names. Don't abbreviate and be lazy. Also, it's conventional to use ALL_UPPER_CASE for constants. s1 should be STATE_EXPIRING, s2 STATE_FROZEN, s3 STATE_FRESH. Then you don't need silly comments by their name to describe it. Also, beef should be more descriptive, like BEEF_COST_FACTOR, and similarly CHICKEN_COST_FACTOR and PORK_COST_FACTOR. That helps makes your code easy to read, and has the same benefits as good indentation/formatting.

    3. Compile at the maximum warning level. How to do this varies based on what compiler you're using, but the result is the same. It helps ensure you are writing clean, proper code. Below is what happens when I compile your code with maximum warnings:
    Code:
    $ make meat
    gcc -Wall -Werror -std=c99 -o meat meat.c
    meat.c: In function ‘main’:
    meat.c:23:8: error: left-hand operand of comma expression has no effect [-Werror=unused-value]
    meat.c:23:3: error: statement with no effect [-Werror=unused-value]
    meat.c:28:8: error: left-hand operand of comma expression has no effect [-Werror=unused-value]
    meat.c:28:3: error: statement with no effect [-Werror=unused-value]
    meat.c:31:3: error: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘float *’ [-Werror=format]
    meat.c:33:8: error: left-hand operand of comma expression has no effect [-Werror=unused-value]
    meat.c:33:3: error: statement with no effect [-Werror=unused-value]
    meat.c:43:3: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘double’ [-Werror=format]
    meat.c:45:13: error: subscripted value is neither array nor pointer nor vector
    meat.c:45:31: error: subscripted value is neither array nor pointer nor vector
    meat.c:45:23: error: left-hand operand of comma expression has no effect [-Werror=unused-value]
    cc1: all warnings being treated as errors
    make: *** [meat] Error 1
    • Those errors are because price, "NA"; does not do anything. It does not assign "NA" to price or anything of the sort. It's like saying x; Nothing happens to x, it's just a wasted statement.
    • That error is because scanf is trying to read an int (%d), but you are telling it to store that in a float. Read the scanf docs (see migf1's post #4), use the right format specifier and variable type to store it.
    • That error is because scanf is looking for a string (%s), but you are telling it to store the result in a double. Again, read the scanf docs.
    • Those errors are because price is not an array, thus you can't use the array index operator [ ]. It's not even of char type, or any kind of integer type that would support comparing to 'N' or 'A'. It's a float.
    • That error is because of the if condition. You do not use comma to combine conditionals. You need to use && or || for compound conditionals.


    More things I noticed at quick glance:
    Code:
    fflush(stdin);
    That is undefined behavior. Read this link and this link.
    Code:
    scanf ("%s",&meat);
    That is wrong. scanf with %s will read a string until a white space or EOF occurs, then it will append the null character. That means that you will always need at least one extra char for the null terminator '\0', meaning 2+ chars in your string. meat is declared as a single char. You need to declare it as a string (i.e. char array) if you wish to read a string into it. You will need one more character than the longest string you plan to read. So if you want to store "chicken" (7 chars), you need 8 chars (7 for the word + 1 for the null).
    Code:
    if ((meat != beef) || (meat != chicken) || (meat != pork))
    Look at your #defines. beef is 1, chicken is 1.5 and pork is 2. You intend meat to store a string (based on the scanf above it), yet you compare it to an int/double value. You can't do that in C, you need to compare things of equivalent types. You can't even compare two strings with the == sign (or != or <, <=, > or >=). String comparison uses the strcmp function. Google for examples of how to use it.

    You have a similar problem with how you deal with the meat state.

    That is a lot of stuff for you to work on and fix. It should keep you busy for a while. If your teacher alone is not enough, find some books and online tutorials. We have some here (link), and Google will turn up lots more.
    Last edited by anduril462; 06-04-2013 at 11:27 AM. Reason: formatting

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-04-2013, 03:02 AM
  2. assign null value to a struct and check if it is null
    By ymc1g11 in forum C Programming
    Replies: 10
    Last Post: 11-01-2012, 03:58 PM
  3. Replies: 9
    Last Post: 10-20-2007, 01:05 AM
  4. accept(ListenSocket, NULL, NULL); cause program to hang?
    By draggy in forum Networking/Device Communication
    Replies: 11
    Last Post: 06-16-2006, 03:40 PM
  5. Multiplying! PLEASE HELP!!!!!!!!!!!!!
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2001, 09:12 AM