Using a for loop to display the contents of an array

Discussion in 'C++' started by Scott, Dec 3, 2004.

  1. Scott

    Scott Guest

    Will someone please tell me why the first element of the array is not
    displayed if the const int amt (see code below) is declared as a
    multiple of 4?
    If I declare it as 3, or 5 the all elements of the the array are
    displayed, if 4 or a multiple is used, it will not cout the first
    element.

    #include <stdlib.h>
    #include <string>
    #include <iostream.h>

    using namespace std;


    void main(){
    const int Amt = 4;
    char r1[Amt];
    char r2[Amt];


    cout << "1: " << endl;
    cin >> r1;
    for (int x = 0; x < Amt; x++)
    cout << r1[x];

    cout << endl;

    cout << "2: " << endl;

    cin >> r2;
    for (int y = 0; y < Amt; y++)
    cout << r1[y];

    cout << r1[0];

    }
    Scott, Dec 3, 2004
    #1
    1. Advertising

  2. Scott wrote:
    > Will someone please tell me why the first element of the array is not
    > displayed if the const int amt (see code below) is declared as a
    > multiple of 4?
    > If I declare it as 3, or 5 the all elements of the the array are
    > displayed, if 4 or a multiple is used, it will not cout the first
    > element.
    >
    > #include <stdlib.h>
    > #include <string>
    > #include <iostream.h>
    >
    > using namespace std;
    >
    >
    > void main(){
    > const int Amt = 4;
    > char r1[Amt];
    > char r2[Amt];
    >
    >
    > cout << "1: " << endl;
    > cin >> r1;
    > for (int x = 0; x < Amt; x++)
    > cout << r1[x];
    >
    > cout << endl;
    >
    > cout << "2: " << endl;
    >
    > cin >> r2;
    > for (int y = 0; y < Amt; y++)
    > cout << r1[y];
    >
    > cout << r1[0];
    >
    > }


    And yes, do use size_t for array indices instead of int.

    --
    Karthik.
    karthik kumar, Dec 3, 2004
    #2
    1. Advertising

  3. > Will someone please tell me why the first element of the array is not
    > displayed if the const int amt (see code below) is declared as a
    > multiple of 4?


    Use a debugger.

    Niels Dybdahl
    Niels Dybdahl, Dec 3, 2004
    #3
  4. Scott wrote:
    > Will someone please tell me why the first element of the array is not
    > displayed if the const int amt (see code below) is declared as a
    > multiple of 4?
    > If I declare it as 3, or 5 the all elements of the the array are
    > displayed, if 4 or a multiple is used, it will not cout the first
    > element.
    >
    > #include <stdlib.h>
    > #include <string>
    > #include <iostream.h>
    >
    > using namespace std;
    >
    >
    > void main(){
    > const int Amt = 4;
    > char r1[Amt];
    > char r2[Amt];
    >
    >
    > cout << "1: " << endl;
    > cin >> r1;
    > for (int x = 0; x < Amt; x++)
    > cout << r1[x];
    >
    > cout << endl;
    >
    > cout << "2: " << endl;
    >
    > cin >> r2;
    > for (int y = 0; y < Amt; y++)
    > cout << r1[y];
    >
    > cout << r1[0];
    >
    > }



    It does for me. However, you never put anything in something other than
    r1[0] and r2[0]. Try initializing all elements that you try to print
    before you print them and see if that helps.

    / martin
    Martin Magnusson, Dec 3, 2004
    #4
  5. Scott

    Gianguz Guest

    (Scott) wrote in message news:<>...
    > Will someone please tell me why the first element of the array is not
    > displayed if the const int amt (see code below) is declared as a
    > multiple of 4?
    > If I declare it as 3, or 5 the all elements of the the array are
    > displayed, if 4 or a multiple is used, it will not cout the first
    > element.
    >
    > #include <stdlib.h>
    > #include <string>
    > #include <iostream.h>
    >
    > using namespace std;
    >
    >
    > void main(){
    > const int Amt = 4;
    > char r1[Amt];
    > char r2[Amt];
    >
    >
    > cout << "1: " << endl;
    > cin >> r1;
    > for (int x = 0; x < Amt; x++)
    > cout << r1[x];
    >
    > cout << endl;
    >
    > cout << "2: " << endl;
    >
    > cin >> r2;
    > for (int y = 0; y < Amt; y++)
    > cout << r1[y];
    >
    > cout << r1[0];
    >
    > }


    I tried your code with "gcc version 3.3.4 20040623 / Gentoo Linux"
    and seems to work even if the compiler warn me about <iostream.h>
    (<iostream> must be used) deprecation and error me on void main()
    (i.e.: C++ standard requires int main() )
    First element is always displayed as the whole array.
    With compiler and platform are you using?

    Gianguglielmo
    Gianguz, Dec 3, 2004
    #5
  6. Scott

    Jerry Coffin Guest

    (Scott) wrote in message news:<>...
    > Will someone please tell me why the first element of the array is not
    > displayed if the const int amt (see code below) is declared as a
    > multiple of 4?
    > If I declare it as 3, or 5 the all elements of the the array are
    > displayed, if 4 or a multiple is used, it will not cout the first
    > element.


    let's start with "why are you using arrays?"

    > #include <stdlib.h>
    > #include <string>
    > #include <iostream.h>
    >
    > using namespace std;
    >
    >
    > void main(){


    main returns an int -- right now, your entire program has undefined
    behavior.

    > const int Amt = 4;
    > char r1[Amt];
    > char r2[Amt];
    >
    >
    > cout << "1: " << endl;
    > cin >> r1;


    This is dangerous -- your array only has room for 3 characters of
    input (plus a NUL terminator) but you're allowing the user to enter an
    arbitrary amount of input.

    If you're going to work with strings, why not USE strings to do it?

    std::string r1, r2;

    std::getline(std::cin, r1);
    std::cout << r1 << std::endl << "2: ";

    std::getline(std::cin, r2);
    std::cout << r2;

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Dec 3, 2004
    #6
  7. Scott

    Ron Natalie Guest

    Jerry Coffin wrote:

    >
    >>#include <stdlib.h>
    >>#include <string>
    >>#include <iostream.h>
    >>


    iostream.h is a non-standard header name. It frequently doesn't
    boogie well with objects defined in standard headers.

    >>
    >>void main(){

    >
    >
    > main returns an int -- right now, your entire program has undefined
    > behavior.
    >


    Actually, it's ill-formed. The compiler should issue a diagnostic.
    There may be an implementation defined extension to allow you to progress.
    Ron Natalie, Dec 3, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loudking
    Replies:
    9
    Views:
    455
    Keith Thompson
    May 1, 2007
  2. addi
    Replies:
    0
    Views:
    268
  3. Paul Roche
    Replies:
    4
    Views:
    108
    Robert Klemme
    Nov 2, 2010
  4. Replies:
    0
    Views:
    189
  5. Isaac Won
    Replies:
    9
    Views:
    369
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page