954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I need a program that prompts the user to input a real number and promptly rounds it

I need a program that repeatedly asks for a real number, and then rounds it and output the rounded number as an integer. and it stops asking for real numbers when input 3.14

needhelpe
Newbie Poster
22 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

That sounds like a good beginner's exercise. You should totally write a program like that. :)

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 
That sounds like a good beginner's exercise. You should totally write a program like that. :)


hey I need help. I made one code but doesn't work!!!

needhelpe
Newbie Poster
22 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 
I made one code but doesn't work!!!


If you do not post the code and ask a specific question about it, everyone is going to assume you want someone else to do your homework for you.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 
hey I need help. I made one code but doesn't work!!!


Can we see what you have done so far?

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
If you do not post the code and ask a specific question about it, everyone is going to assume you want someone else to do your homework for you.

this is my code:
#include
Int main()
{
Double x;
Int b;

Printf(“Real: “);
Scanf(“%lf”, &x);

If (x>0)
{
Int b;
b= x+ .5;
printf( “%d”, %b);
}

else
{
Int b;
B=x-.5;
Printf(“%d”, &b);
}

If( x==3.14)
{
printf(“Pye Pye!\n”);
}

Return 0;
}

needhelpe
Newbie Poster
22 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

Most things in programming shouldn't start with an uppercase letter (at least in C anyway)
data types are lowercase and so is printf
Also you have redeclaration of b. And you use b and B interchangeable which you can't do.
This whole code is really a mess. You need to review the basics of declraing variables and issues relating to syntax.
Your not going to be able to write any programs if you can't understand the basics of syntax.

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 
Most things in programming shouldn't start with an uppercase letter (at least in C anyway) data types are lowercase and so is printf

yes I know that. but how can I make it work?

this program should input real numbers and output the number rounded. and also, it should stop input and output numbers when the input is 3.14.

for example, the output should be like:

Real: 2.3
2
Real: 3.7
4
Real: 2.5
3
Real: 3.14
Pye Pye!


code:
#include
Int main()
{
double x;
int b;

printf(“Real: “);
scanf(“%lf”, &x);

if (x>0)
{
int b;
b= x+ .5;
printf( “%d”, &b);
}

else
{
int b;
b=x-.5;
printf(“%d”, &b);
}

if( x==3.14)
{
printf(“Pye Pye!\n”);
}

return 0;
}

needhelpe
Newbie Poster
22 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 
yes I know that. but how can I make it work?


Hmm. I'm not so sure you do because you are still writing int main() with a capital 'I'. Try running the code and post the errors being reported by the compiler. Also, use code tags when posting code in the future.

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 

You don't need to use the & in printf statements. It is only needed when scanning.
Also, when you try to print the variable b, you should use the format specifier for double values. The %d is used for ints. %f can be used for numbers with decimal points.

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 

needhelpe> yes I know that. but how can I make it work?
A loop is needed to continuously repeat the question until the stop signal (3.14) is reached.

If I would want to ask for a digital until the number five is entered I could write it like this:

#include <stdio.h>

int main(void)
{
	int result = 0;

	/*
	 * continuously asks for input until 5 is entered
	 */
	while (result != 5) {
		int c;

		/* repeat the question each time */
		fputs("Enter a number: ", stdout);
		fflush(stdout);

		/* minimum error checking for entered input */
		if (scanf("%d", &result) == 1) {
			/* if correct, input is displayed */
			printf("You entered: %d\n", result);
		} else {
			/* input was wrong, clear standard input */
			while((c = getchar()) != '\n');
		}

	}
	return 0;
}
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
You don't need to use the & in printf statements. It is only needed when scanning. Also, when you try to print the variable b, you should use the format specifier for double values. The %d is used for ints. %f can be used for numbers with decimal points.

the program works. now I just need something that makes the output ask me for a real number until I input 3.14.

example of output:
(~)$ round
Real: 3.2
3
Real: 4.6
5
Real: 2.8
3
Real: -3.4
-3
Real: 3.14
Pye Pye! (here it stops!)

needhelpe
Newbie Poster
22 posts since Oct 2009
Reputation Points: 5
Solved Threads: 0
 

Use a while loop. The stopping condition should be retail value = 3.14

while(retail_value != 3.14)
{
//put your code that executes the program here
}

Now when the user enters 3.14 as the retail value, the loop will stop and output will terminate.

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You