I NEED #define HELP!!!!

This is a discussion on I NEED #define HELP!!!! within the C++ Programming forums, part of the General Programming Boards category; Ok first let me just let you know I'm just starting to learn C++ so if this seems like a ...

  1. #1
    Twista Game_Addict27's Avatar
    Join Date
    Aug 2004
    Posts
    7

    Question I NEED #define HELP!!!!

    Ok first let me just let you know I'm just starting to learn C++ so if this seems like a stupid question I'm sorry. But I guess we can't all be masters our first day. Ok so here is the prob....
    The book I'm learning from (C++ Programming 101 by Greg Perry) wants me to:
    Write a program that defines the 10 digits, 0 through 9, as literals ZERO through NINE. Add these 10 defined digits and store the sum in a variable named 'total' and print to screen the results.

    I did another program sort of like this and it ran perfect it's first time compiled. But I have no clue what to do here. Here is the source code I've typed so far so you can see if I'm even on the right track. Oh and this code is just to get the literals to print I havn't done the part to add them yet cause I can't even get this to work. Well any suggestions or solutions would be great thanks.
    ---Source Code---
    Code:
    //Review Exercises Pg 97 #3
    #include <iostream.h>
    #define 0 "ZERO"
    #define 1 "ONE"
    #define 2 "TWO"
    #define 3 "THREE"
    #define 4 "FOUR"
    #define 5 "FIVE"
    #define 6 "SIX"
    #define 7 "SEVEN"
    #define 8 "EIGHT"
    #define 9 "NINE"
    int main()
    {
       cout << 0"\n";
       cout << 1"\n";
       cout << 2"\n";
       cout << 3"\n";
       cout << 4"\n";
       cout << 5"\n";
       cout << 6"\n";
       cout << 7"\n";
       cout << 8"\n";
       cout << 9"\n"; 
    	
       system ("Pause");
       return 0;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    From the description I would say this is a more likely answer
    Code:
    #include <iostream>
    #define ONE 1
    #define TWO 2
    #define THREE 3
    
    int main()
    {
      int sum = ONE + TWO + THREE;
      cout << ONE << endl;
      cout << TWO << endl;
      cout << THREE << endl;
      cout << "Sum: " << sum << endl;
    }
    Of course you'll need to take it all the way

  3. #3
    Twista Game_Addict27's Avatar
    Join Date
    Aug 2004
    Posts
    7
    Hey thanks Thantos. I re-wrote it and it compiled and added fine. I figured thats how it should of been too when I started it but I guess the books wording threw me off. Thanks alot.
    -T

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Why not just use const instead, as i have heard defines are evil .
    Code:
    #include <iostream>
    using std::cout;
    const int ONE = 1;
    const int TWO = 2;
    
    int main()
    {
      int sum = ONE + TW0;
      cout<<sum;
      return 0;
    }
    Woop?

  5. #5
    Twista Game_Addict27's Avatar
    Join Date
    Aug 2004
    Posts
    7
    Well thats what my book says to that not many programmers use it because 'const' is more managable but it said that #define does have it's advantages and well I'm just starting so I'm just tryin to learn everything I can. But it's cool cause the program compiled right but thanks for reply. I appreciate it.
    -T

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Not neccessarily evil. Like most of C/C++, preprocessor directives like #define are powerful. It can be a very useful tool. It can also show it's various pitfalls when you need them least
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    I ran this code:
    #include <iostream>
    using std::cout;
    const int ONE = 1;
    const int TWO = 2;

    int main()
    {
    int sum = ONE + TW0;
    cout<<sum;
    return 0;
    }
    But this makes makes me this error, I can't figure out.
    numeros.cpp
    c:\numeros.cpp(8) : error C2065: 'TW0' : undeclared identifier
    Error executing cl.exe.

    numeros.exe - 1 error(s), 0 warning(s)

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    A classic zero-O replacement on the TW0 in this line:
    int sum = ONE + TW0;

    I hate bugs like that.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    
    const int TWO = 2;
    
    int main()
    {
    int sum = ONE + TW0;
    
    Now do you see the problem? Or should I add color?
    Code:
    
    const int TWO = 2;
    
    int main()
    {
    int sum = ONE + TW0;
    
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>int sum = ONE + TW0;
    The letter O can be easily confused for the number 0. The define is TWO yet you used TW0. Feel free to change your viewing font if the two still look identical.

  11. #11
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    abyss:
    i do believe u used a zero in two when declaring sum.
    Code:
    int sum = ONE + TW0;
    //should be
    int sum = ONE + TWO;
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And if you're still not sure, I'm sure four more people will point it out also in the next three minutes...

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    Thnx ha ha ha Ok!!! I got it ha ha ha ha

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by prog-bman
    Why not just use const instead, as i have heard defines are evil .
    Code:
    #include <iostream>
    using std::cout;
    const int ONE = 1;
    const int TWO = 2;
    
    int main()
    {
      int sum = ONE + TW0;
      cout<<sum;
      return 0;
    }
    while you're staying away from deines, you should also stay away from globally-declared variables...

    by the way:
    Code:
    const int TWO = 2;
    
    int main()
    {
      int sum = ONE + TW0;
    </sarcasm>
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by major_small
    by the way:
    Code:
    const int TWO = 2;
    
    int main()
    {
      int sum = ONE + TW0;
    </sarcasm>
    So you're trying to say that one of them is the LETTER O, and the other is the number ZERO?

    Quzah.
    Hope is the first step on the road to disappointment.

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

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 11:52 AM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  5. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 08:44 AM

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