CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2007
    Posts
    12

    Input not accepted

    Code:
    #include <iostream>
    using namespace std;
    
    #define height 3
    #define width 5
    
    int arr[height][width];
    int n,m;
    
    int main ()
    {
        do {
            cout << "Enter n: ";
            cin >> n;
            cout << "Now m: ";
            cin >> m;
        for (n=0; n<height; n++)
         for (m=0; m<width; m++)
         {
             arr[n][m]=(n+1)*(m+1);
         }
         cout << arr[n][m] << "\n";
         } while (n != 2);
         return 0;
    }
    what's wrong??

  2. #2
    Join Date
    Aug 2005
    Posts
    132

    Re: Input not accepted

    The problem you have is in the loops. In the loop:

    Code:
    for (n=0; n<height; n++)
    the n=0 sets n equal to 0 therefore ignoring the input entirely. The same goes for the for loop with m as the subject.

    Try removing the for loops entirely and see what response you get from the code, I.E.

    Code:
    int main ()
    {
        do {
            cout << "Enter n: ";
            cin >> n;
            cout << "Now m: ";
            cin >> m;
            arr[n][m]=(n+1)*(m+1);
            cout << arr[n][m] << "\n";
         } while (n != 2);
         
         return 0;
    }

  3. #3
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Input not accepted

    Quote Originally Posted by Arez
    what's wrong??
    Look at:
    Code:
         cout << arr[n][m] << "\n";
    Because this line will be executed after n-loop block, then it should be
    arr[height][width] => arr[3][5]....this causes memory fault.

    In this line:
    Code:
         } while (n != 2);
    at this point n will always be 3, and then the while loop will never end.

  4. #4
    Join Date
    Oct 2005
    Location
    Valka, Riga
    Posts
    36

    Re: Input not accepted

    and the first for loop has no brackets which would define its block.

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Input not accepted

    Quote Originally Posted by edijs.vee
    and the first for loop has no brackets which would define its block.
    The scope of the outer for-loop is the next statement, i.e. the inner for-loop. No problem there except one could call this bad style.

  6. #6
    Join Date
    Feb 2007
    Posts
    141

    Re: Input not accepted

    code with correction

    Code:
    #include <iostream>
    using namespace std;
    
    const  int HEIGHT = 3;  // use const instead of #define
    const  int WIDTH = 5; // always use capital letter for constant
    
    //int arr[height][width];  why you take them global variable
    //int n,m;                        why global
    
    int main ()
    {
        int arr[HEIGHT][WIDTH];
        int n,m;
        do
        {
            cout << "Enter n: ";
            cin >> n;
            cout << "Now m: ";
            cin >> m;
         //for(n=0; n<height; n++) why you are changing value of n and m
         //for (m=0; m<width; m++)
         //Always use braces to define scope
         for(int i=0;i<HEIGHT;i++)
         {
             for(int j=0;j<WIDTH;j++)
                arr[i][j]=(i+1)*(j+1);
             cout << arr[i][i] << "\n";
         }//For end
         } while (n != 2);
         return (1);
    }
    Regards

  7. #7
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Input not accepted

    Quote Originally Posted by nitin1979
    code with correction
    Hard to correct code, if the OP does not tell us what m and n should be used for Your program creates the same array again and again until the user enters 2 for n. Is there a deep sense behind this?

    Some comments:
    Code:
    #include <iostream>
    using namespace std;
    
    const  int HEIGHT = 3;  // use const instead of #define
    const  int WIDTH = 5; // always use capital letter for constant
    
    //int arr[height][width];  why you take them global variable
    //int n,m;                        why global
    
    int main ()
    {
        int arr[HEIGHT][WIDTH];
        int n,m;
        do
        {
            cout << "Enter n: ";
            cin >> n;
            cout << "Now m: ";
            cin >> m; // m is never used below
         //for(n=0; n<height; n++) why you are changing value of n and m
         //for (m=0; m<width; m++)
         //Always use braces to define scope // so why don't you below?
         for(int i=0;i<HEIGHT;i++)
         {
             for(int j=0;j<WIDTH;j++)
                arr[i][j]=(i+1)*(j+1); // where are n and m ?
             cout << arr[i][i] << "\n"; // arr[3][3] is not a valid array member!
         }//For end
         } while (n != 2); // should be while (n != 42)
         return (1); // why? Did the program fail ?
    }

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