The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Problem in program is little but i am confuse.http://forums.devshed.com/newthread
Discuss Problem in program is little but i am confuse.http://forums.devshed.com/newthread in the C Programming forum on Dev Shed. Problem in program is little but i am confuse.http://forums.devshed.com/newthread C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 7th, 2012, 08:23 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 3 h 53 m 55 sec
Reputation Power: 0
|
|
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.
|

December 7th, 2012, 08:40 AM
|
 |
Contributed User
|
|
|
|
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;
}
|

December 7th, 2012, 10:05 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
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)
|

December 7th, 2012, 10:51 AM
|
 |
Contributed User
|
|
|
|
|
No, you do your own homework.
Or as much of it as you can manage, then post your effort here for assistance.
|

December 7th, 2012, 12:12 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
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.
|

December 7th, 2012, 01:13 PM
|
 |
Contributed User
|
|
|
|
|
Say
height = getInt();
Then apply your
if (height < 0 || height > 23 )
|

December 9th, 2012, 12:35 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
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.
|

December 9th, 2012, 12:36 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
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. 
|

December 9th, 2012, 01:02 PM
|
 |
Contributed User
|
|
|
|
|
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.
|

December 10th, 2012, 06:34 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
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;
}
|

December 13th, 2012, 10:29 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
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.
|

December 13th, 2012, 10:38 AM
|
 |
Contributed User
|
|
|
|
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 );
|

December 13th, 2012, 12:08 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
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.
|

December 17th, 2012, 10:34 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 3 h 53 m 55 sec
Reputation Power: 0
|
|
|
I am waiting for answer 4 days a ago.
|

December 17th, 2012, 11:48 AM
|
 |
Contributed User
|
|
|
|
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
##
###
####
#####
######
#######
########
#########
##########
###########
############
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|