1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

[PING] Darkhorse or any other C++ experts

Discussion in 'BBS Hangout' started by Rockets2K, Nov 10, 2004.

  1. Rockets2K

    Rockets2K Clutch Crew

    Joined:
    Mar 22, 2000
    Messages:
    18,050
    Likes Received:
    1,271
    Ive been banging my head on a problem all week, an could use a pointer in the right direction...Im not asking for someone to do it for me, just a little help figuring out why my code doesnt work.

    Im generating random numbers(10 of them) and then trying to use the character classification functions to determine what ASCII character the numbers translate to..

    What Im doing is taking the variable that holds the random number and using it as a parameter in the classification functions..(ex...int isprint(character)......I have setup a nested if block to check thru the classifications from isprint thru isupper and see which one the random number translates to....but it isnt working...no matter what number it uses, it returns true...even when the number is obviously a control character.(in the case of isprint)

    Is this even the right way to go about this?

    here is the code from the function to do this...

    note: this wont display right, but take it as given that the ifs and elses are properly aligned and indented(not that it really matters)

     
  2. Behad

    Behad Member

    Joined:
    Feb 20, 1999
    Messages:
    12,358
    Likes Received:
    193
    Just when I thought things couldn't get any geekier on this board...


    :p :p
     
  3. Rockets2K

    Rockets2K Clutch Crew

    Joined:
    Mar 22, 2000
    Messages:
    18,050
    Likes Received:
    1,271
    aaaah...byte me old man...:p

    you are absolutely no help.:(
     
  4. Behad

    Behad Member

    Joined:
    Feb 20, 1999
    Messages:
    12,358
    Likes Received:
    193
    You are damn near my age.

    "Byte" me...cute.:p


    I'll stop interrupting and let you get back to your regularly scheduled geekfest.:)
     
  5. boomboom

    boomboom I GOT '99 PROBLEMS

    Joined:
    Sep 29, 1999
    Messages:
    12,821
    Likes Received:
    9,515
    Just based on what we see in your example...

    if (int isprint(character))

    is this line correct? Wouldn't including the int cause a declaration of an unnamed variable?
     
  6. No Worries

    No Worries Wensleydale Only Fan
    Supporting Member

    Joined:
    Jun 30, 1999
    Messages:
    34,147
    Likes Received:
    22,479
    remove the int as mentioned above and all should be right with the world.

    ie

    Code:
      if ( isprint(character) )
        if ( isgraph(character) )
          if ( isalnum(character) )
            if ( isalpha(character) )
              if ( isupper(character) )
    
     
  7. Rockets2K

    Rockets2K Clutch Crew

    Joined:
    Mar 22, 2000
    Messages:
    18,050
    Likes Received:
    1,271

    heh.....details details...:p

    boomer.....I thought so also...but the text and all the examples of the character clssification functions I found had the int in them....so thats the way I used them...it compiles, so the use of the integer type is a logic error and not a compile error

    but No Worries's post works....kinda....now I dont get 10 lines of output...
    it varies...I ran it 4 times, and got 5, 9, 7 and 6 lines of output...Im assuming that the missing ones are unprintables(control characters) and it isnt printing them out...not sure...I havent debugged it yet..

    but...that is a minor inconvienance...yall fixed my major problem...

    :D
     
  8. No Worries

    No Worries Wensleydale Only Fan
    Supporting Member

    Joined:
    Jun 30, 1999
    Messages:
    34,147
    Likes Received:
    22,479
    You might want to add {} to your if-then-else clauses.
     
  9. DarkHorse

    DarkHorse Member

    Joined:
    Oct 9, 1999
    Messages:
    6,763
    Likes Received:
    1,317
    Are you required to use all these functions to figure out what character is supposed to be returned?

    char c = (char) myInteger;

    Maybe I'm just oversimplifying.

    Just so you know, whenever you see a function definition, they will always list the "return type" for the function. In the case of these functions, they will return a 1 or a 0, depending on the results. 1 means true, and 0 means false. That's why whenever you're seeing the functions written down they're preceeded by an "int".

    And No Worries is right, a little {} organization goes a long way. It's too hard to keep strait what clause applies to what otherwise. If you don't use brackets, only the next sequential line applies. By using brackets, you can apply a conditional statement to a whole block of instructions.
     
  10. Rockets2K

    Rockets2K Clutch Crew

    Joined:
    Mar 22, 2000
    Messages:
    18,050
    Likes Received:
    1,271
    well....the problem statement calls for generating 10 random numbers in the range of 0-127, then the program shoudl determine the ASCII type fo the integer...if the integer represents a printable ASCII character, print the character with one of the messages(noted inthe code above)

    If the ASCII character is a nonprintable character, display its ASCII code in decimal format withthe message(last cout << message above)

    thats what Im tryin to accomplish....

    I initally used the brackets to neaten up the if else structures...but determined I didnt think it necessary since there arent any compound statements inthe if else structure....do yall think doing so would fix the problem of the program skipping the last else that prints out the nonprintable message?

    A big thanks to you guys....Im still real new to this ...;)


    EDIT: answered that one myself...I went ahead and added the braces to the if else structure...and it worked....it printed out the unprintabloe statement....again...many thanks..
     
    #10 Rockets2K, Nov 10, 2004
    Last edited: Nov 10, 2004
  11. boomboom

    boomboom I GOT '99 PROBLEMS

    Joined:
    Sep 29, 1999
    Messages:
    12,821
    Likes Received:
    9,515
    Here's a nice dumbed-down version...sorta...you could easily replace the GT and LT arguments in the if statements with the ifXXX functions.

    enum { lowercase, uppercase, digit, punctuation, space, nonprintable};
    int ischar;

    if (character <= 90 && character >= 65) ischar = uppercase;
    if (character <= 122 && character >= 97) ischar = lowercase;
    if (character <= 57 && character >= 48) ischar = digit;
    if ((character <= 47 && character >= 33)) || (character <= 64 && character >= 58) || (character <= 96 && character >= 91)) ischar = punctuation;
    if (character == 32) ischar = space;
    if (character < 32) ischar = nonprintable;

    switch (ischar)
    {
    case uppercase : cout << "The random number is : " << character << "\tThe ASCII character is a uppercase letter.\n";
    break;
    case lowercase : cout << "The random number is : " << character << "\tThe ASCII character is a lowercase letter.\n";
    break;
    case digit : cout << "The random number is : " << character << "\tThe ASCII character is a digit.\n";
    break;
    case punctuation: cout << "The random number is : " << character << "\tThe ASCII character is a punctuation mark.\n";
    break;
    case space : cout << "The random number is : " << character << "\tThe ASCII character is a space.\n";
    break;
    case nonprintable :
    default : cout << "The random number is : " << character << "\tThe ASCII character is a nonprintable character\n";
    break;
    }
     
  12. Harrisment

    Harrisment Member

    Joined:
    Jun 20, 2001
    Messages:
    15,392
    Likes Received:
    2,158
    R2K - Are you just doing this on your own, or are you taking some development classes? I work with our web developers daily and occasionaly have to sift through some code, but I'm far from comfortable with it. I've always been more of a server guy but would love to pickup more of the dev side. I'm interested in getting more familiar with C#, so if you (or anyone else) could reccomend a good place to start I would be much obliged. :)
     
  13. DarkHorse

    DarkHorse Member

    Joined:
    Oct 9, 1999
    Messages:
    6,763
    Likes Received:
    1,317
    I'm trying my best not to post the solution code to this, cause I believe in the power of struggling through these types of problems. (I was a Computer Science TA for over 3 years)

    The main difficulty for me in terms of knowing how to help is understanding the exact specifications of what you're trying to do - particularly if you're doing this for a class. Generally, with an assignment, the point isn't necessarily to accomplish what they're asking you to do, but to illustrate a particular concept. I or anyone else here with programming experience could supply many different solutions to the problem you've suggested, but depending on what you're intended to glean from the experience, they may not be helpful.

    For instance, I might ask you to generate a random number for me. My intent may be to have you struggle with the mathematical impossibility of "generating" a random number, and have you struggle with various solutions. For instance, you'd probably start with some kind of system clock time, and modify that number in some way to get a "random" number. That's a very basic solution. It's a great exercise to try and work out.

    But someone with a little programming experience might just come in and say. "Hey, use the rand() function in C. Problem solved." That wasn't my intent at all, as I was trying to help you understand how random numbers are generated by computers.

    That was probably too patronizing and long-winded. Sorry about that. But I'm just saying, it would be helpful if we had a link or something that would let us know exactly your problem.

    If you're just doing this on your own for practice, just to try things out and learn, then my approach is a lot different.

    Make sense?

    :)

    Just trying to be helpful,
    DH
     
  14. No Worries

    No Worries Wensleydale Only Fan
    Supporting Member

    Joined:
    Jun 30, 1999
    Messages:
    34,147
    Likes Received:
    22,479
    I noticed that the last else bound to the wrong if. This is actually a fairly common mistake for programmers, epsecially if the programmer puts the else in the right column matching it to the wrong if. A very good programming practice is to always use braces in C/C++. Hint. hint, hint: following this practice will give experienced programmers more confidence in your programming abilities in the workplace.

    (BTW, those programming languages that use a endif do not have the problem.)
     
  15. Rockets2K

    Rockets2K Clutch Crew

    Joined:
    Mar 22, 2000
    Messages:
    18,050
    Likes Received:
    1,271
    ok.....firstly...

    boomer....

    interesting...I debated doing it with a switch statement...but since the assignment was handed out right after we went over the chapter with loops and the character functions...I assumed that was the thing he wanted us to use....the switch statement chapter was covered the week after the assignment was handed out.....but thankf for showing me how that would have worked...I wasnt sure how the logic would go without the classification functions..


    Harrisment....
    this is for a class, Im a network admin major, and one of the requirements is C+....I dont like it since Im not a programmer, Im a networking guy...but hey...at least I hope learning C will help me in Linux scripting......one of the other guys here may be able to answer that question better than I...if nothing else...you could always go and take a beginning C class to get the basics..and go from there if you feel so inclined...


    DH....

    I agree, the best way to learn is to struggle to fix problems...and thats why I just wanted yall to look over what I did and see where the problem was...I wouldnt have asked except I have looked at it for days and couldnt figure out what I was doing wrong...
    as I noted above...it is for a class...and considering when it was assigned, I feel it is definitely a exercise in using loops and the functions to solve the problem...

    I had done exactly as you mentioned...seeded the random with the system time [srand(time (NULL));], and then restricted the range of the numbers
    [int character = (rand () % 128);] ....basic to be sure...but it works for this...that was actually the easy part of this...;)

    Wish I could have posted a link to the assignment...but my posting server is down...so Im stuck describing it. :( No matter...you guys managed to figure it out in no time...wish I would have asked before, save me a ton of headache..

    No worries....
    not sure which one you are referring to...withthe exception of the orphaned 'if' near the bottom...I thought all my ifs and elses were properly matched....that was one of the first things I double checked.....yalls points about using the braces to keep things neat is well taken...I wont make that mistake again since adding the braces fixed the last problem...
    Hopefully, gaining the respect of other programmers in the workplace will never come up since I hope to be working with network administration once I find a damn IT job...;)


    anyway....everything working acharm now....I dont know how to thank you guys for your help...
    If ya ever make it to Live when Im there for a viewing party...I owe ya a drink at least...
     
  16. boomboom

    boomboom I GOT '99 PROBLEMS

    Joined:
    Sep 29, 1999
    Messages:
    12,821
    Likes Received:
    9,515
    For Linux scripting...you'll just need basic programming skills, which you will definitely develop while using C++. Other than that, the syntax for scripting isn't too hard. It's just like all other languages...takes a bit of time to learn the basics and everything else thereafter builds on top of them.

    Good luck!!!
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now