Thread: what exactly does this mean?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    what exactly does this mean?

    What exactly does this mean? I get the cout and the cin part.

    Code:
    cout << "Do you want to do some banking? ";
    cin >> moreBankingBusiness;
    
    for (int i = 0;
          i < moreBanking Business.length(); i++) {
              moreBankingBusiness[i] = toupper (moreBankingBusiness[i]);
          }
    i dont get anything up to the toupper statement... and for the toupper statement i understand that it takes the input and capitalizes it... but i just dont understand the significance of the "[i]".

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by lastresort
    What exactly does this mean? I get the cout and the cin part.

    Code:
    cout << "Do you want to do some banking? ";
    cin >> moreBankingBusiness;
    
    for (int i = 0;
          i < moreBanking Business.length(); i++) {
              moreBankingBusiness[i] = toupper (moreBankingBusiness[i]);
          }
    i dont get anything up to the toupper statement... and for the toupper statement i understand that it takes the input and capitalizes it... but i just dont understand the significance of the "[i]".
    It seems to take an array of characters and capitalize them. It will cycle through each element of the array, and assign that element to its "toupper" return result...I'm assuming it will capitalize characters.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Krak is correct. toupper( ) takes a character (more specifically, an integer, which is at least as big as a character -- think of it as just a character), and raises it to its uppercase value (if such an operation makes sense -- otherwise, it will do nothing). The subscript operator [] returns the character with that offset from the start of the array. The loop makes sure you go over each character in the string.

    *edit*
    Well, I see that it isn't an array being used, but rather a string class. So, it works by the same principle (returning the character in the position denoted by the index value).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by lastresort
    i dont get anything up to the toupper statement... and for the toupper statement i understand that it takes the input and capitalizes it... but i just dont understand the significance of the "[i]".
    Think of the following: the first part of your for loop initializes a counting variable to be used just in that loop. The second part is the condition that must be met for the loop to run again, and the third is usually just called "the increment statement", where you increment your counting variable.

    Typically, "i" is a very popular name for a counting variable. Here's how this works:

    Code:
    for (int i=0; i<5; i++){
    myArray[i] = i;
    }
    i is 0, and as long as it's less than 5, run the loop again and again, each time increasing i. Now remember that the index number of the first element of an array is ZERO, not one.

    So here's what this actually does: Because "i" is incremented and plugged into the array index number, you assign each element in the array a value:

    Code:
    //Something like this really happens:
    myArray[0] = 0;
    myArray[1] = 1;
    //...And so on

Popular pages Recent additions subscribe to a feed