Thread: sorry I didn't make my self clear last time

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    9

    sorry I didn't make my self clear last time

    sorry I didn't make my self clear last time so Im
    posting the same question again with another limitation.
    Im allowed to use only <stdio.h>
    I think that the problem is with the %d.
    maybe it will be easier with %s??
    but Im not sure how to do it.


    I need to check if the user is ,for example, typing a letter insted of a number or that he is typing too many pieces ,for example
    if i asked him to Enter an array of size 3 and he by mistake puts 4.
    how do I check this?



    this is part of the main function only:
    Code:
    #include <stdio.h>
    #define max_Size 19
    #define max_pieces 10
    
    void main() {
    	
    	int width[max_pieces], // the array of the pieces' heights
    		height[max_pieces], // the array of the pieces' widths
    		i, // index
    		numH,numW,numP,//heights widths and number of pieces.
    		puzzle[361];//the array of the puzzle.
    	printf("Enter puzzle height( < 20 ) width( < 20 ) number of pieces( <= 10 )\n");
    	scanf("%d%d%d",&numH,&numW,&numP);
    	
    	while(numH>=20||numW>=20||numP>=10||numH<=0||numW<=0||numP<=0){
    		printf("Incorrect input ,try again\n");
    		printf("\nEnter puzzle height( < 20 ) width( < 20 ) number of pieces( <= 10 )\n");
    		scanf("%d%d%d",&numH,&numW,&numP);
    	}//while
    	
    	printf("Enter array of size %d of piece heights:\n",numP);
    	for (i=0; i<numP; i++)
    		scanf("%d",&height[i]);
    	
    	printf("Enter array of size %d of piece widths:\n",numP);
    	for (i=0; i<numP; i++)
    		scanf("%d",&width[i]);

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    check this

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int a,b,c;
    	char str[6];
    	printf("Enter 3 numbers: ");
    
    	if (scanf("%d %d %d",&a,&b,&c) == 3)
    	{
    		printf("a = %d, b = %d, c = %d\n",a,b,c);
    	} else {
    		printf("I said 3 numbers..FOOL !!!\n");
    		return 1;
    	}
    
    	printf("Enter a string with 5 characters: ");
    	
    	scanf("%5s",str);
    	
    	printf("string = %s\n",str);
    	
    	return 0;
    }
    Sample output.

    D:\CProg>check1
    Enter 3 numbers: 1 d v
    I said 3 numbers..FOOL !!!

    D:\CProg>check1
    Enter 3 numbers: 1 2 3
    a = 1, b = 2, c = 3
    Enter a string with 5 characters: abcdefg
    string = abcde
    Thanks,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. Determine the closest departure time
    By Kyeong in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 08:06 PM
  3. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  4. Time between Military Hours
    By StarOrbs in forum C++ Programming
    Replies: 18
    Last Post: 03-01-2005, 06:46 PM
  5. Military Time Functions
    By BB18 in forum C Programming
    Replies: 6
    Last Post: 10-10-2004, 01:57 PM