Q's

This is a discussion on Q's within the C++ Programming forums, part of the General Programming Boards category; this is a compilation of three questions I have first up I was declaring a string intended to chekc input ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    86

    Q's

    this is a compilation of three questions I have

    first up

    I was declaring a string intended to chekc input and I called it (quotes for clarity) "in". My IDE has syntax coloring and in was colored as keyword. can someone tell me what "in" is?

    second:

    in this same program I'm declaring float *<variable>

    I dont know how long the array is gonna end up. in java, there is an array object called length, so array.length gives the length of the array. does something of tis sort exist in c++?

    last one:

    within a class, can I call methods from the class from methods within the same class? if so, do I just type the method name, as opposed to, for example, name.method()?

    thanx

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    I was declaring a string intended to chekc input and I called it (quotes for clarity) "in". My IDE has syntax coloring and in was colored as keyword. can someone tell me what "in" is?
    in is not a C++ keyword. What IDE are you using?
    in this same program I'm declaring float *<variable>

    I dont know how long the array is gonna end up. in java, there is an array object called length, so array.length gives the length of the array. does something of tis sort exist in c++?
    Use std::vector in the <vector> header. It is much easier than dynamic arrays.
    within a class, can I call methods from the class from methods within the same class? if so, do I just type the method name, as opposed to, for example, name.method()?
    Yes, you can. Just call the method without a qualifier.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Quote:
    within a class, can I call methods from the class from methods within the same class? if so, do I just type the method name, as opposed to, for example, name.method()?



    Yes, you can. Just call the method without a qualifier.
    If there is ambiguity, you can use *this. It can be used like any other pointer, as a pointer to the current object.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    Use std::vector in the <vector> header. It is much easier than dynamic arrays.
    thats all well and good, but the point remains that i'm gonna need the array length at some point

    does array.length work or is there some other way?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    use the vector::size() member function. perhaps you should download some STL documentation for reference? here's a good start.
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    for arrays you can determine capacity (that is maximum number of elements in the array) by:

    int capacity = sizeof(arrayName)/sizeof(arrayElementType);

    To determine how many elements are actually in the array (that is, the size of the array), however, you need to keep a separate variable to keep track as you add objects to and remove objects from the array. However, if you have a null terminated char array (that is, a C-style string) then you can determine the actual length of the string (as opposed to the maximum possible length of the string) by doing something like this:

    int length = strlen(arrayName);

    and if the array is an STL string then you can use the length() method of STL string class to get the actual length of the string

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    I dont understand this thing about the size member function

    here's the test code I used

    Code:
    //sizeof.cpp
    
    #include <iostream>
    
    using namespace std;
    
    int main(int argc,char *argv[]){
    
    	int *test;
    	int i;
    	
    	test[0]=8;
    	
    	test[1]=9;
    	
    	i=test.size();
    	
    	cout<<i<<"\n";
    	
    }//end main
    help?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    >> help?

    whoa, you really need to buy a book on C++ bud! raw pointers/arrays are POD's (plain old datatypes). they don't have member functions. besides that, you accessed an invalid pointer - you really need to learn the language before jumping into the STL don't you think?
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    was that helpful?

    I'm just out of practice, I rarely use arrays or vectors for my stuff.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    look, can someone PLEASE just give me some way of finding out the length of, say, an array of floats?

    or failing that, a COMPLETE statement of what object or data type I should be using?

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by pktcperlc++java
    I dont understand this thing about the size member function

    here's the test code I used
    The size member function is meant to be used on the vector container as mentioned by Sebastiani, i.e.:

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        // Declare an vector named "test" that stores integer objects
        vector<int> test;
    
        // Add some stuff to the vector.
        test.push_back(8);  // Now test[0] equals 8
        test.push_back(9);  // Now test[1] equals 9
    
        // Output the "size" of the vector, i.e. the number of elements it contains
        cout << "The vector contains " << test.size() << " elements.\n";
    	
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    >> was that helpful?

    I was merely suggesting that you study the material before writing code, what's wrong with that? logic errors are unavoidable but syntax errors are totally preventable - if you read an entire book on c++ you'll eliminate 99% of your syntax errors. proven fact.
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    look, can someone PLEASE just give me some way of finding out the length of, say, an array of floats?
    Code:
    // Static array
    float a[10];
    // Dynamic array
    float *b = new float[10];
    
    // Get size for static array
    int size1 = sizeof a / sizeof a[0];
    // Get size for dynamic array
    int size2 = 10;
    or failing that, a COMPLETE statement of what object or data type I should be using?
    The solution for a static array only works if the array was declared in scope. If not then the array is actually a pointer and the sizeof trick will not work. There is no portable way to get the size of a dynamic array. You have to save the size when memory is allocated. Because of these problems, you should be using a vector.
    Code:
    #include <vector>
    
    std::vector<float> c;
    
    // Get size for vector
    std::vector::size_type size3 = c.size();

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    you really need to learn the language before jumping into the STL don't you think?
    Actually, I disagree. I think the Standard Library is part of the language and should be learned together with the syntax: std::string when it comes to strings, std::vector when it comes to arrays, and only after that pointers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I agree with CornedBee, using STL right away is not wrong, but I agree wholeheartedly with Sebastiani, et. al., that having, and using, a reference of some sort is invaluable and to be strongly encouraged.

    The word size when applied to variables can be used in several different contexts. This can lead to confusion/misunderstandings. The way [Ren] uses size to describe arrays is one way. In essence the sizeof operator returns the amount of memory, in bytes, of a given variable. Certainly this can be one way to measure size. When using the size of an array divided by the size of each element in an array, you get the maximal number of elements that may be stored in the array. To me, that is the capacity of the array. The actual number of elements stored in the array is what I call size. This is the way size is defined when used as a member of the STL containers.

    int x = sizeof float; //x is number of bytes in a float

    float array[10];
    int x = sizeof array; //x is the number of bytes used by the array of floats called array.

    int x = sizeof array/sizeof float; //x is the capacity, size, legth, whatever you want to call it, it terms of the number of elements that array can possibly hold;

    int x = 0;
    array[0] = 0.1;
    ++x;
    array[1] = 5.3;
    ++x;
    //x is the number of elements placed into array at any given point in time (what is mean when I say size), not the maximal number of elements that could be in the array, and not the memory in bytes of a given element, or the array in total.

    std::vector<float> v;
    int x = v.capacity(); //x is how many floats v could hold;
    int y = v.size(); //y is how many floats v does hold

    char str[] = "and";
    int x = strlen(str); //x is length, size, capacity, whatever, of a C style string called str.

    char str[4];
    str[0] = 'a';
    str[1] = 'n';
    str[2] = 'd';
    int x = strlen(str); //ERROR, str is not null terminated and therefore not a string, but a plain old char array. Therefore, strlen() cannot be used.

    std::string str = "and";
    int x = str.length(); //x is size, length, capacity, whatever, of the STL string called str.

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

Similar Threads

  1. Several Q's
    By Elyubarov in forum Windows Programming
    Replies: 9
    Last Post: 12-26-2004, 09:36 PM
  2. Qs about RAID0 & expandability
    By Markallen85 in forum Tech Board
    Replies: 2
    Last Post: 01-18-2004, 01:52 PM
  3. 2 q's: SPI_SETFLATMENU & platform SDK
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2003, 09:44 AM
  4. I have 2 Q's for you all
    By robid1 in forum C Programming
    Replies: 5
    Last Post: 06-24-2003, 05:52 AM
  5. C and UNIX Q's?
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 06-13-2002, 05:39 AM

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