Thread: Very simple C question. Please help

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    3

    Very simple C question. Please help

    Hi everyone, first post and very, very new to C. I'm taking a class to learn programming and while doing homework I ran into a problem that I can't figure out. I'm trying to get the output to show "mph" which I added below in red, and I've tried adding a string but cannot get it to work. The code is below, If anyone could point me in the right direction on how to get "mph" added I'd greatly appreciate it.

    int a;
    float b,c,d,e,total;


    a = 100;
    b = 3;
    c = 19;
    d = 3;
    e = 4;
    total = e+d+a/c*b;


    printf("The total miles driven per day: %d\n", a);
    printf("The cost per gallon of gasoline: $%0.2f\n", b);
    printf("The average miles per gallon: %.0f\n", c);
    printf("The parking fees per day: $%0.2f\n", d);
    printf("The tolls per day: $%0.2f\n", e);
    printf("Your daily cost of driving to work is: $%0.2f\n", total);
    system("PAUSE");
    }


    /*
    The total miles driven per day: 100
    The cost per gallon of gasoline: $3.00
    The average miles per gallon: 19 mph
    The parking fees per day: $3.00
    The tolls per day: $4.00
    Your daily cost of driving to work is: $22.79
    Press any key to continue . . ./

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, jsalz.

    Always go advanced when you have code to post, highlight your code, and click on the # icon in the top bar of the editor - that will put [code] tags around your program, and keep it looking like code, instead of forum html text.

    Makes it MUCH easier to study.

    The answer to your question is to make a
    Code:
    char *mymph={"mph"};
    and then print it using %s, after printing the mph digits.

    Although you could just print the mph digits and then print out mph, right inside the printf formatting string.

    More than one way to do almost anything.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    Thank you so much Adak!
    We learned how to add a name with char name [10] but never how to just add normal char's! If you don't mind me asking, why is it *mymph=? What does the asterisk do?
    Sorry... like I said, super new at programming C.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think it's because it dereferences the pointer, right? Like, mymph would be the pointer to a char structure and *mymph would be what mymph points at, the char structure itself. So what you're doing is setting the value of the char structure. I think. I'm super new to C as well but I need to know if I know this stuff so if I'm wrong then please, feel free to stop the spread of misinformation.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I'd just put it in the printf rather than having a variable:

    Code:
    printf("The average miles per gallon: %.0f mph\n", c);
    I'm afraid you're wrong, MutantJohn. The * here is indicated that the variable being defined is a pointer.
    Once you're got a pointer, you can use * to derefence it in the way you've said.

    Code:
      char *mymph = "mph";   // mymph is type char*
      
      char test = *mymph;     /// Deference the pointer and get back the char 'm'
    jsalz -- if you're happier with arrays you can also do:
    Code:
      char mymph[] = "mph";   
      // or
      char mymph[4] = "mph";
    What you can't do is:
    Code:
       char mymph = "mph";
    These are all valid ways of defining a string (sequence of chars) in C. They're all slightly different in important ways -- but I wouldn't worry about that for now (unless you really want to know?).

    Usually when you see a char* in code it's referring to a string. Pointer types hold the addresses at which a variable is stored -- in the case of a string, it holds the address of the first character. The string continues until a NULL terminator is found (value 0). However, a char* can also be the address of a single character, or a pointer to a sequence of characters in memory that aren't a string because they don't end with a NULL terminator.

    If you're just starting out then it's probably best to think of char* as "string type*. Be aware that it's a pointer and could point to anything, not necessarily a valid string. But don't worry about it too much -- you won't get very far in C without understanding pointers, so it probably won''t be long before you learn about them.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    This forum is awesome!! Thank you so much everyone! I may have another question soon haha

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Just a minor nit but this:
    Code:
    char *mymph = "mph";
    Should really be this:
    Code:
    const char *mymph = "mph";
    It's safer to do this as the compiler can then make sure you aren't trying to write to the memory pointed to by the pointer somewhere in your code. Without the const you could accidentally try writting to this memory - and the compiler wouldn't know to complain about it - which could quite possibly present itself as a bug.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Really simple question
    By PvtVampire in forum C Programming
    Replies: 1
    Last Post: 12-09-2011, 05:51 AM
  2. simple question im not sure about
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2006, 09:56 AM
  3. simple simple design question
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2005, 11:33 PM
  4. simple SDL question
    By sand_man in forum Game Programming
    Replies: 3
    Last Post: 07-02-2004, 12:04 AM
  5. simple question
    By lwong in forum C Programming
    Replies: 3
    Last Post: 10-21-2003, 08:11 AM