Hey I'm having trouble using scanf to input two number and it is only letting me input 1. here is my code

#include <stdio.h>

int main()
{

	int i ;
	int j ;
	int tl[10][25] ; 
	int br[40][5] ; 
	int point1[100][100] ; 
	
	
	for(i=0;i<40;i++)
		for(j=0;j<25;j++)
		{
	
		printf("please enter value x,y for top left", i+1) ;
		scanf("%d", &tl[i][j]) ;
			
		printf("please enter value x,y for bottom right", i+1) ;
		scanf("%d", &br[i][j]) ;
		
	
		printf("please enter value x,y for point") ;
		scanf("%d", &point1[i][j]) ; 


if(point1[i][j] <= tl[i][j] && point1[i][j] <= br[i][j])
{
	printf("point %d, is inside rectangle", point1[i][j]) ;
}
else
{
	printf("point %d, is outside rectangle", point1[i][j]);
}
			  
		}
	}

Recommended Answers

All 5 Replies

Your printf statement has too many arguments. I'm not sure what the i+1 is for. What are you trying to do there?

Hey I'm having trouble using scanf to input two number and it is only letting me input 1.

When I run your code, it does ask me for two numbers, and then a point. Perhaps I'm not understanding your question fully. Please explain what your program is supposed to do, and I'll try to help out more.

Works fine for me. This is the output that I got when I ran the code

please enter value x,y for top left10
please enter value x,y for bottom right20
please enter value x,y for point30
point 30, is outside rectangleplease enter value x,y for top left15
please enter value x,y for bottom right23
please enter value x,y for point45
point 45, is outside rectangleplease enter value x,y for top left10
please enter value x,y for bottom right30
please enter value x,y for point15
point 15, is outside rectangleplease enter value x,y for top left

But there is a logical error in the code. point 15 is inside the rectangle not outside it

Works fine for me.
But there is a logical error in the code. point 15 is inside the rectangle not outside it

There are buffer over-runs all over. But what do I know, it could be a feature and not a bug.

Assuming the best scenario where scanf() will not screw you. Here we go: int tl[10][25]; t1 dimensions are 10 rows and 25 columns, right?

for(i=0;i<40;i++) /* <-- 40 rows */
		for(j=0;j<25;j++) /* <-- 25 columns */
                   ...
                   scanf("%d", &tl[i][j]) ;

How many rows could t1 get? 10 not 40! int br[40][5] ; br dimensions are 40 rows and 5 columns, right?

for(i=0;i<40;i++) /* <-- 40 rows */
		for(j=0;j<25;j++) /* <-- 25 columns */
                   ...
                   scanf("%d", &br[i][j]) ;

How many columns could br get? 5 not 25! int point1[100][100] ; did you suspect that something what getting short and over compensated here, with 100 rows and 100 columns?

printf("please enter value x,y for top left", i+1) ;

printf("please enter value x,y for bottom right", i+1) ;

Missing the format operand.

if(point1[i][j] <= tl[i][j] && point1[i][j] <= br[i][j])
{
	printf("point %d, is inside rectangle", point1[i][j]) ;
}
else
{
	printf("point %d, is outside rectangle", point1[i][j]);
}

Poor formatted,and irrelevant if the input is not correct.

The question is meant to find the coordinates of the top left of a rectangle and the coordinates of the bottom right and the nfind if the coordinates of the point are inside the rectangle.

Okay... so you need each scanf to ask for two numbers... I understand now.

In that case, I don't think a 2-dimensional array is the easiest way to go. It might be easier to make arrays for each of the following: tlx[], tly[], brx[], bry[]. I don't know what the end goal of your program is, but if you don't need to save all of that data, you don't need arrays at all.

To use scanf to input two numbers, the code should look like:

scanf("%d %d", &tlx[i], &tly[i]);

You'll enter the two numbers separated by a space (i.e. '5 8' will store 5 into tlx and 8 into tly).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.