Help on the C++ tutorial

This is a discussion on Help on the C++ tutorial within the C++ Programming forums, part of the General Programming Boards category; Ok guys im now at lesson 8 (Arrays) So i took the quiz: 4. Which of the following correctly accesses ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    4

    Help on the C++ tutorial

    Ok guys im now at lesson 8 (Arrays)
    So i took the quiz:
    4. Which of the following correctly accesses the seventh element stored in foo, an array with 100 elements?
    A. foo[6];
    B. foo[7];
    C. foo(7);
    D. foo;

    I answered the B,while the correct was A.
    Frmoo what i know the only thing we can access "directly" is always the last element(in this case 100)
    So why cant i get 7?
    Is this a general case?I should remove 1(-1) to get the element i want everytime?

    Example:
    If i want to access the 10th element,should i put foo[9]; ?

    Also:Now reading part 9 (Strings)
    So i saw this example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     char string[256];                               // A nice long string
    
     cout<<"Please enter a long string: ";
     cin.getline ( string, 256, '\n' );              // Input goes into string
     cout<<"Your long string was: "<< string <<endl;
     cin.get();
    }
    Regarding "cin.getline ( string, 256, '\n' );"
    i didnt find any actual reason to include the 256 after the string so i maddified it to:
    cin.getline ( string,'\n' );
    and it worked.
    Whats the difference?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    im a n00b so not sure if im right but i think the 250 means thats the max number of characters u can enter for that string.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Frmoo what i know the only thing we can access "directly" is always the last element(in this case 100)
    There is random access available here, so you can access any of the array's elements directly.

    Is this a general case?I should remove 1(-1) to get the element i want everytime?
    Yes. The first element of the array is foo[0]. It makes sense then that the seventh element is foo[6]. There is no foo[100] if foo has 100 elements, since the last element would then be foo[99].

    Regarding "cin.getline ( string, 256, '\n' );"
    i didnt find any actual reason to include the 256 after the string so i maddified it to:
    cin.getline ( string,'\n' );
    and it worked.
    Whats the difference?
    Read up on getline(). It probably 'worked' because the char '\n' was cast to the int value of 10.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 06:38 PM
  2. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 01:49 PM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 11:32 PM

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