Writing Code but am having problems

This is a discussion on Writing Code but am having problems within the C Programming forums, part of the General Programming Boards category; ........

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    11

    Writing Code but am having problems

    .....
    Last edited by Choppers; 06-25-2009 at 07:47 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you think pWord should be between 1 and 20? What do you think pWord is, exactly?

    (Note also that you don't return anything, let alone the length of the string.)

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    .....
    Last edited by Choppers; 06-25-2009 at 07:47 PM.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> This is main and this is my string length whats wrong with it i don't seem to know why it isn't working:

    1) It doesn't compile
    2) pWord is a pointer, not a length. One way find the length is to traverse the text, and increment a counter until you reach the character value 0.
    3) scanf is vunerable to buffer overflows - use a hard-coded width-specifier, or use something like snprintf to imbed the max length into the format string beforehand.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    we only have learned scanf and fscanf

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    In that case walk along the array array pWord until you reach the terminating null character while incrementing the count.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> we only have learned scanf and fscanf

    OK, so use the hard-coded width specifier then, no?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    itCbitC i don't understand what you are saying. Why use NULL?

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    hard coded wdith specifier?

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> itCbitC i don't understand what you are saying. Why use NULL?

    Not NULL but null. In C, text is *usually* (but this is just a convention) terminated with the character value 0 (ie: null). Naturally, if you count how many characters precede it, you get the length.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> hard coded wdith specifier?

    As in:

    Code:
    scanf("%19s", word);
    That limits the input to MAX-1 characters, thus preventing a buffer overflow.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    so are you saying to do something like
    in stringlen


    *pWord=null

    while
    *pWord != '\0'
    return;

    i am doing this for class and am not allowed to change my main

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Quote Originally Posted by Choppers View Post
    itCbitC i don't understand what you are saying. Why use NULL?
    That is the C convention for character strings ie they are null terminated so that stdlib routines like strlen() etc. can find its end. In the case of stringLen() too start at the beginning and keep incrementing pWord until a null character '\0' is reached. In pseudocode it'll be something like
    Code:
    pWord points to the beginning of the array
    set character count equal to zero
    while (null character isn't found)
        increment pWord
        increment character count
    return character count
    Last edited by itCbitC; 06-25-2009 at 06:46 PM.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Quote Originally Posted by Choppers View Post
    so are you saying to do something like
    in stringlen


    *pWord=null

    while
    *pWord != '\0'
    return;

    i am doing this for class and am not allowed to change my main
    Is that logic supposed to be inside main() or stringlen()??

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> sp are you saying to do something like

    OK, first of all, let's write code that compiles, shall we? Next, why would you set the first character to 0 and then set up a loop that tests that very condition? The null terminator will already be in the buffer. Finally, in the loop you'll need to increment pWord.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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

Similar Threads

  1. Problems with code
    By hollyjaye in forum C Programming
    Replies: 11
    Last Post: 03-29-2007, 06:42 AM
  2. Temperature Conversion code problems
    By eroth88 in forum C Programming
    Replies: 6
    Last Post: 10-22-2006, 02:24 AM
  3. Code writing issues Part 2...
    By jaybo684 in forum C++ Programming
    Replies: 10
    Last Post: 08-01-2005, 09:28 AM
  4. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 02:02 AM
  5. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 01:43 PM

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