#define-c++ novice question

This is a discussion on #define-c++ novice question within the C++ Programming forums, part of the General Programming Boards category; Hey fellas i am new to all this so please bare with me...im not trying to get you guys to ...

  1. #1
    Registered User renurv's Avatar
    Join Date
    May 2005
    Posts
    8

    Smile #define-c++ novice question

    Hey fellas i am new to all this so please bare with me...im not trying to get you guys to write my program or do my homework ....


    This is what I have so far and it isn't working.
    Code:
    #include <iostream>
    using namespace std;
    #define phone 18005551234
    
    int main ( )
    
    {
        cout << phone;
        system ("pause");
        return 0;
    }
    I am trying to use the define option to cout phone 18005551234. I am not looking for an alternative to display the number. I want to learn how to use this #define option. Thanks in advance.

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >This is what I have so far and it isn't working.
    How isn't it working? Aside from the number probably being well out of range for an integer on your compiler, everything is fine. I'll go out on a limb and guess that that's the problem, and if you do this then it'll work perfectly:
    Code:
    #include <iostream>
    using namespace std;
    #define phone "18005551234"
    
    int main ( )
    
    {
        cout << phone;
        system ("pause");
        return 0;
    }
    p.s. The system function is declared in <cstdlib>. Not including it is technically an error even if your compiler allows it.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Or you could make that number a floating point one. Or, in C, a long long.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    the 1800... is too large for a long.. using it like this fixes the problem..

    #define phone "18005551234"

    just a note also that when you make somting a constant you put it in CAPS.. so it can easily be distinguished from other vars..

    hth

  5. #5
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    whoa.. looks like everyone zeroed in on this post at the same time..

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    That's what always happens.

    just a note also that when you make somting a constant you put it in CAPS.. so it can easily be distinguished from other vars..
    You don't have to, but that's the convention.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    >> You don't have to, but that's the convention.

    yeh.. good point..
    i was thinkin to say more along the lines of .. its common to put it in caps.. but the fingers apparently didnt listen..

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yeah, it prevents you from doing things like
    Code:
    #define x 100
    
    x = 3;
    One other thing: if you use a string, you can change "18005551234" to "1-800-555-1234". Or you can write your own class to store the phone number . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User renurv's Avatar
    Join Date
    May 2005
    Posts
    8
    thanks fellas...

  10. #10
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    Quote Originally Posted by Victorian
    You have to change the phone number to something other than:
    #define phone 1800EAT........
    Or esle the compiler will not compile the number.
    what the ehll is EAT ?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I think the point is that this is valid:
    Code:
    #define phone "1800EAT"
    but this is not:
    Code:
    #define phone 1800EAT
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 02:53 PM
  2. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 07:14 AM
  3. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 11:05 AM
  4. Error check problem again
    By rtransfi in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 04:55 PM
  5. macro (#define) question
    By Darrok in forum C++ Programming
    Replies: 30
    Last Post: 12-20-2001, 05:01 PM

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