C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 7th, 2012, 08:23 AM
affan502 affan502 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 affan502 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 53 m 55 sec
Reputation 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.

Reply With Quote
  #2  
Old December 7th, 2012, 08:40 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 21 h 26 m 11 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old December 7th, 2012, 10:05 AM
affan502 affan502 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 affan502 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 53 m 55 sec
Reputation Power: 0
Quote:
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)

Reply With Quote
  #4  
Old December 7th, 2012, 10:51 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 21 h 26 m 11 sec
Reputation Power: 1774
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!

Reply With Quote
  #5  
Old December 7th, 2012, 12:12 PM
affan502 affan502 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 affan502 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 53 m 55 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #6  
Old December 7th, 2012, 01:13 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 21 h 26 m 11 sec
Reputation Power: 1774
Say
height = getInt();

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

Reply With Quote
  #7  
Old December 9th, 2012, 12:35 PM
affan502 affan502 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 affan502 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 53 m 55 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #8  
Old December 9th, 2012, 12:36 PM
affan502 affan502 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 affan502 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 53 m 55 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #9  
Old December 9th, 2012, 01:02 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 21 h 26 m 11 sec
Reputation Power: 1774
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.

Reply With Quote
  #10  
Old December 10th, 2012, 06:34 AM
affan502 affan502 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 affan502 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 53 m 55 sec
Reputation Power: 0
Quote:
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;

}

Reply With Quote
  #11  
Old December 13th, 2012, 10:29 AM
affan502 affan502 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 affan502 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 53 m 55 sec
Reputation 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.

Reply With Quote
  #12  
Old December 13th, 2012, 10:38 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 21 h 26 m 11 sec
Reputation Power: 1774
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 );

Reply With Quote
  #13  
Old December 13th, 2012, 12:08 PM
affan502 affan502 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 affan502 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 53 m 55 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #14  
Old December 17th, 2012, 10:34 AM
affan502 affan502 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 affan502 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 53 m 55 sec
Reputation Power: 0
I am waiting for answer 4 days a ago.

Reply With Quote
  #15  
Old December 17th, 2012, 11:48 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 21 h 26 m 11 sec
Reputation Power: 1774
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
          ##
         ###
        ####
       #####
      ######
     #######
    ########
   #########
  ##########
 ###########
############

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Problem in program is little but i am confuse.http://forums.devshed.com/newthread

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap