#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Question Problem in program is little but i am confuse.http://forums.devshed.com/newthread


    It's my home work Please help.
    requirements of program is below here.
    than you run program ask height.height of half right side pyramid.



    Height: -2
    Height: -1
    Height: foo
    Retry: bar
    Retry: 1
    ##

    My program handle wall than i write height in + numbers.
    no - number accept and no alphabets.and height not grater than 23 and not less than 0.


    Here is my program script.

    #include <cs50.h>
    #include <stdio.h>
    int main()

    {
    int height= 0;
    int i=0,j=0,k=0;

    printf("height:");
    scanf("%d", &height);

    int spacing=height -1;
    int hashes=2;


    for (i=0; i<height; i++)
    {
    for (j=spacing; j>0; j--)

    {
    printf(" ");
    }
    for (k=0; k<hashes; k++)
    {
    printf("#");
    }
    spacing--;
    hashes ++;
    printf("\n");

    }


    if (height < 0 || height > 23 )
    {

    printf("height:\n");
    scanf("%d",&height);

    }





    return 0;
    }
    Than i write height above 23 or less than 0. so my program handle wall both conditions.but according to requirements you enter 24 so program noting do any thing and come on directory.same 0 but 0 handle well and come back at directory.
    other thing alphabets if u write alphabets so program say retry .
    So please help what is mistake in my program and what is right script.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    The basic thing you need to do is start by checking the return result of scanf.

    Code:
    #include<stdio.h>
    int main()
    {
      int num, ch, done = 0;
      do {
        if ( scanf("%d",&num) == 1 ) {
          // scanf worked
          done = 1;
        } else {
          // scanf failed, throw away chars up to next newline
          while ( (ch=getchar()) != EOF && ch != '\n' );
          if ( ch == EOF ) done = 1;
        }
      } while ( !done );
      if ( ch != EOF ) {
        printf("You entered %d\n", num );
      }
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by salem
    The basic thing you need to do is start by checking the return result of scanf.

    Code:
    #include<stdio.h>
    int main()
    {
      int num, ch, done = 0;
      do {
        if ( scanf("%d",&num) == 1 ) {
          // scanf worked
          done = 1;
        } else {
          // scanf failed, throw away chars up to next newline
          while ( (ch=getchar()) != EOF && ch != '\n' );
          if ( ch == EOF ) done = 1;
        }
      } while ( !done );
      if ( ch != EOF ) {
        printf("You entered %d\n", num );
      }
      return 0;
    }



    Please can u make this program.
    there is link for requirements.
    https://x.cs50.net/2013/psets/1/pset1.html
    go below on site . title name (IT'S MARIO)
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    No, you do your own homework.

    Or as much of it as you can manage, then post your effort here for assistance.

    Comments on this post

    • affan502 agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by salem
    No, you do your own homework.

    Or as much of it as you can manage, then post your effort here for assistance.

    Thanks . OK i will try again.
    if i want to add GetInt(); after int main so there make one q.
    in GetInt(); function if i write alphabet so result retry so that's i want.
    But how to use this function and which point and please guide me behalf of my script.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Say
    height = getInt();

    Then apply your
    if (height < 0 || height > 23 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by salem
    Say
    height = getInt();

    Then apply your
    if (height < 0 || height > 23 )

    YA i have done this change but still wrong.
    than i start program ask noting because GetInt work and GetInt nature show noting if i write alphabets so result retry and if numeric so ask height.so what i do now on that position.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by salem
    Say
    height = getInt();

    Then apply your
    if (height < 0 || height > 23 )

    YA i have done this change but still wrong.
    than i start program ask noting because GetInt work and GetInt nature show noting if i write alphabets so result retry and if numeric so ask height.so what i do now on that position. :chomp:
  16. #9
  17. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Post your latest code.

    Because
    - we can't see your screen from here,
    - making wild guesses as to what you might have done wrong is a waste of time,
    - and just posting a complete answer for you to copy/paste just isn't going to happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by salem
    Post your latest code.

    Because
    - we can't see your screen from here,
    - making wild guesses as to what you might have done wrong is a waste of time,
    - and just posting a complete answer for you to copy/paste just isn't going to happen.
    #include <cs50.h>
    #include <stdio.h>
    int main()
    {

    int height= GetInt();
    if (height < 0 || height >= 23 )
    {

    printf("height:\n");
    scanf("%d",&height);

    }
    int i=0,j=0,k=0;

    printf("height:");
    scanf("%d", &height);

    int spacing=height -1;
    int hashes=2;

    for (i=0; i<height; i++)
    {
    for (j=spacing; j>0; j--)

    {
    printf(" ");
    }
    for (k=0; k<hashes; k++)
    {
    printf("#");
    }
    spacing--;
    hashes ++;
    printf("\n");

    }


    return 0;

    }
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Salem

    Please help i am vary hope to come there on this forum if i don't have noting know about c so why i came there.
    some time try best but little problem make more complex. that same condition i am facing.please please help.
  22. #12
  23. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Code:
    int height= GetInt();
    if (height < 0 || height >= 23 )
    {
    printf("height:\n");
    scanf("%d",&height);
    }
    Why are there two methods for reading the height here?

    Consider
    Code:
    int height;
    do {
        height= GetInt();
    } while ( height < 0 || height >= 23 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by salem
    Code:
    int height= GetInt();
    if (height < 0 || height >= 23 )
    {
    printf("height:\n");
    scanf("%d",&height);
    }
    Why are there two methods for reading the height here?

    Consider
    Code:
    int height;
    do {
        height= GetInt();
    } while ( height < 0 || height >= 23 );
    Ya that's near to my mission there is update behave of program.
    Code:
    height:afsds
    retry:2
    height:2
    "/there show height and this height is result of do and while statement i don't know why they doing like this behave./"
    ##
    and there script.
    Code:
    #include <cs50.h>
    #include <stdio.h>
    int main()
    {
    	printf("height:");
    	  	int i=0,j=0,k=0;
    	  int height;
    	     do {
    	        height= GetInt();
    	      }
    	         while (height<0 || height > 23); 
               {  
                      
               printf("height:"); 
               scanf("%d",&height);
        
             }  
    		
    	int spacing=height -1;
    	int hashes=2;
      
    	   for (i=0; i<height; i++)
    	   {
    		for (j=spacing; j>0; j--)
    	
    		{
    			printf(" ");
    		}
    		for (k=0; k<hashes; k++)
    		{
    			printf("#");
    		}
    		spacing--;
    		hashes ++;
    		printf("\n");
           
            }
            
             
                    return 0;
             
     }
    Than i write positive numeric thy show again height i have mans-hen on top.
    negative numeric result show noting like GetInt behave.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    I am waiting for answer 4 days a ago.
  28. #15
  29. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    So what's your new question?

    Here, I've re-indented your code.
    Code:
    #include <stdio.h>
    
    //!! I made my own, I don't know what yours is like
    //!! Note that mine has no error checking (like yours).
    int GetInt()
    {
      int a;
      scanf("%d", &a);
      return a;
    }
    
    int main()
    {
      printf("height:");
      int i = 0, j = 0, k = 0;
      int height;
      do {
        height = GetInt();
      }
      while (height < 0 || height > 23);
    
    #if 0
      //!! Why is this still here!?
      {
        printf("height:");
        scanf("%d", &height);
      }
    #endif
    
      int spacing = height - 1;
      int hashes = 2;
    
      for (i = 0; i < height; i++) {
        for (j = spacing; j > 0; j--)
        {
          printf(" ");
        }
        for (k = 0; k < hashes; k++) {
          printf("#");
        }
        spacing--;
        hashes++;
        printf("\n");
      }
    
      return 0;
    }
    Apart from the redundant asking for height (now commented out with #if 0 .. #endif), I get
    Code:
    $ gcc foo.c
    $ ./a.out 
    height:11
              ##
             ###
            ####
           #####
          ######
         #######
        ########
       #########
      ##########
     ###########
    ############
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo