CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Guest

    Why doest this code work?????

    I am trying to get this program to print to screen ut it doesnt???
    Could anyone tell me why.
    Or is there a better way of doing it.
    All I want to do is see the result on screen.
    I am using ms visiul c++ 6.
    I am new arrays so I might be missing something but i dont know what.
    Please help me
    Thank You in advance.

    ps: Is there a way of putting a loop to also get the results out without
    typing each array one by one.????

    #include <iostream.h>
    #include <stdio.h>
    #include <conio.h>
    main()
    {

    int num[7];

    num[0]=47;
    num[1]=( num[6]+1 );
    num[2]=0;
    num[3]=( num[0]+num[1]+num[5]+num[6] );
    num[4]=0;
    num[5]=( num[0]-13 );
    num[6]=65;

    cout<<num[0]<<num[1]<<num[2]<<num[3]<<num[4]<<num[5]<<num[6];

    getch();
    return 0;
    }



  2. #2
    Join Date
    May 1999
    Location
    Texas, USA
    Posts
    568

    Re: Why doest this code work?????

    What do you mean it doesn't work? I compiled it as is and it works. But change it to the following to see better results.
    #include <iostream.h>
    #include <stdio.h>
    #include <conio.h>
    main()
    {

    int num[7];

    num[0]=47;
    num[1]=( num[6]+1 );
    num[2]=0;
    num[3]=( num[0]+num[1]+num[5]+num[6] );
    num[4]=0;
    num[5]=( num[0]-13 );
    num[6]=65;

    cout << num[0] << endl;
    cout << num[1] << endl;
    cout << num[2] << endl;
    cout << num[3] << endl;
    cout << num[4] << endl;
    cout << num[5] << endl;
    cout << num[6] << endl;

    getch();
    return 0;
    }




    There is a huge problem, though. What exactly are you tring to do? If it is just to learn how to do arrays, then ok. But in the assignment statement you assign num[1] to num[6] + 1. num[6] has not been initialized, it could be anything at this moment.

    Hope that helps,

    Wayne


  3. #3
    Guest

    Re: Why doest this code work?????

    Thanks Wayne. I dont know why ti didnt work on my compiler. I am just trying to learn arrays here.
    I thought the num[1]=num[6]+1 would add one to the num[6] number and put it in num[1] location. Did
    I do wrong there. I was wanting to to put numbers into the arrays and put them out on the screen once done.
    I am new at this.
    Thanks you very much for respending.
    Eric


  4. #4
    Join Date
    May 1999
    Location
    Seattle, WA USA
    Posts
    423

    Re: Why doest this code work?????

    Yes, num[1] = num[6] + 1 would add 1 to whatever value is stored in num[6] and then store it in num[1]. The problem is that in your code, num[6] has never been given any value before you do this assignment, so the value of num[6], and hence num[1], is undefined.

    --michael


  5. #5
    Join Date
    Apr 1999
    Posts
    383

    Re: Why doest this code work?????

    > ps: Is there a way of putting a loop to also get the results out without
    > typing each array one by one.????

    If you want to use a loop, it goes something like this:



    const int ItemCount = 7;
    // initialise array items here
    ...
    ...
    // Output array items
    for (int item = 0; item < ItemCount; ++item)
    {
    cout << num[item] << endl;
    }




    Dave



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured