Thread: trouble with #define statement...

  1. #1
    Registered User
    Join Date
    Jun 2011

    trouble with #define statement...

    This code prompts the user to enter a number and returns the number with a statement if it is prime or not. It also includes "," to separate every 3 digits. I modified the code so that the user could have a choice of three separators(comma, space, period).

    I thought there might be a way to use a control structure to use three different #define commands...something like...

    if(choice == 1)
        #define GROUP_SEP ','
    else if (choice == 2)
        #define GROUP_SEP ' '
    else if (choice == 3)
        #define GROUP_SEP '.'
    The code below works, but I couldn't use the above approach. It would crash my program. Any ideas why?

    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    string output_formatted_string(long long num);
    string determine_separator(int choice);
    int main() {
    long long int  n;                 // Number to test for prime-ness, long long data type
    int  i ;                                    // Loop counter
    int  is_prime = true;   // Bool ean fl ag. . .
                                         // Assume true for now.
            // Get a number from the keyboard.
    cout << "Enter a number and press ENTER: " << endl;
    cin >> n;
    // Test for pri me by checki ng for di vi si bi l i ty
    //  by al l whol e numbers from 2 to sqrt(n) .
    i = 2;
    while (i <= sqrt(n) ) {   // Whi l e i i s <= sqrt(n) ,
    if (n % i == 0)        //  If i di vi des n,
    is_prime = false;  //    n i s not pri me.
    i++;                   //  Add 1 to i .
    // Pri nt resul ts
    string s = output_formatted_string(n);
    if (is_prime)
    cout << s <<" is prime. " << endl ;
    cout << s <<" is not prime." << endl;
    return 0;
    //#define GROUP_SEP       ','
    #define GROUP_SIZE        3
    string output_formatted_string(long long num) {
        int choice;
        string GROUP_SEP;
        cout << "Choose digit separator (1) for comma, (2) for space, (3) for period: " << endl;
            cin >> choice;
            GROUP_SEP = determine_separator(choice);
        //Read data into string s.
        stringstream temp, out;
        temp << num;
        string s = temp.str();
        //Write first characters, in front of
        // first separator (GROUP_SEP).
        int n = s.size() % GROUP_SIZE;
        int i =0;
        if (n > 0 && s.size() > GROUP_SIZE) {
            out << s.substr(i, n) << GROUP_SEP;
            i += n;
        // Handle all the remaining groups.
        n = s.size() / GROUP_SIZE -1;
        while (n-- > 0) {
            out << s.substr(i, GROUP_SIZE) << GROUP_SEP;
            i += GROUP_SIZE;
        out << s.substr(i); //Write the rest of digits.
        return out.str();    //Convert stream -> string.
    string determine_separator(int choice){
        case 1:
            return ",";
        case 2:
            return " ";
        case 3:
            return ".";

  2. #2
    Join Date
    Mar 2008
    # statements are preprocessor directives; they are evaluated before your code is even compiled, let alone run.

    you aren't looking for a #defined variable, you are looking for a char.

    generally speaking, don't use preprocessor directives, they don't work that well, and generally make your work harder to debug.

  3. #3
    Registered User
    Join Date
    May 2011
    Around 8.3 light-minutes from the Sun
    May I suggest you read about the C Preprocessor
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    C++ has a boolean type called bool, use it.

    Com men ts li ke " // Bool ean fl ag. . ." d on' t ad d an y val ue to th e pr ogr am.

    This is line of code makes your program at least 20 times slower than it should be:
    while (i <= sqrt(n) ) {   // Whi l e i i s <= sqrt(n) ,
    Do the square root before the loop and then store the value in a variable. Again that comment is utterly useless. Don't describe what every single lines does. Describe what a block of code does, along with the most important thing of "why" it does that. The only commenting I have ever seen that is worse that what you have here is:
    ++i; /* increment i*/
    determine_separator needs to return a value no matter what. You can't just return nothing if the user types 4 say. Turn your compiler warning levels up and your compiler will tell you that it must return something.

    Don't use all uppercase for variable names.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Join Date
    Oct 2007
    Inside my computer
    Your indentation is lacking. You should learn to improve it.
    Your #define GROUP_SIZE should be replaced with a const int variable.
    And GROUP_SEP should really be a char variable, not a #define.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 04-14-2010, 10:32 PM
  2. Trouble with if statement
    By Tegan in forum C Programming
    Replies: 12
    Last Post: 11-30-2007, 09:13 AM
  3. user define function trouble!
    By johnnypiere in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2002, 01:31 AM
  4. If statement trouble??!
    By riley03 in forum C++ Programming
    Replies: 3
    Last Post: 10-05-2002, 10:52 AM
  5. Having trouble with an if statement!
    By coloughl in forum C Programming
    Replies: 6
    Last Post: 12-14-2001, 06:18 AM
Website Security Test