|
-
August 21st, 2007, 05:55 PM
#1
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??
-
August 21st, 2007, 06:46 PM
#2
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;
}
-
August 21st, 2007, 11:49 PM
#3
Re: Input not accepted
 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:
at this point n will always be 3, and then the while loop will never end.
-
August 22nd, 2007, 06:08 AM
#4
Re: Input not accepted
and the first for loop has no brackets which would define its block.
-
August 22nd, 2007, 08:00 AM
#5
Re: Input not accepted
 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.
-
August 22nd, 2007, 11:12 AM
#6
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
-
August 22nd, 2007, 02:22 PM
#7
Re: Input not accepted
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|