.....
.....
Last edited by Choppers; 06-25-2009 at 06:47 PM.
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.)
.....
Last edited by Choppers; 06-25-2009 at 06:47 PM.
>> 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; }
we only have learned scanf and fscanf
In that case walk along the array array pWord until you reach the terminating null character while incrementing the count.
>> 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; }
itCbitC i don't understand what you are saying. Why use NULL?
hard coded wdith specifier?
>> 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; }
>> hard coded wdith specifier?
As in:
That limits the input to MAX-1 characters, thus preventing a buffer overflow.Code:scanf("%19s", word);
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; }
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
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 05:46 PM.
>> 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; }