How Can I Fix My C While Loop Issue in Calculating Factorials?

  • Thread starter Thread starter Vavotaj
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary

Discussion Overview

The discussion revolves around issues related to a C program that calculates factorials using a while loop. Participants explore problems with input validation for a number that should be between 50 and 100, specifically focusing on the use of data types (double vs. float) and the correct format specifier for input.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants question the logic of the condition in the while loop, suggesting that the use of "||" (OR) may not be appropriate for the intended validation of input between 50 and 100.
  • Others argue that the original condition is correct and will prompt the user until a valid number is entered.
  • A participant points out that the program behaves differently when using double instead of int or float, leading to confusion about input handling.
  • There are suggestions to change the format specifier in the scanf function from "%f" to "%lf" for double data types.
  • Some participants express frustration with the documentation and the need for clearer guidance on using scanf with double types.
  • A later reply emphasizes the importance of consulting reference materials for programming issues, suggesting that understanding the documentation is crucial for problem-solving.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to input validation and handling of double types in C. Multiple competing views remain regarding the correct implementation and understanding of the scanf function.

Contextual Notes

There are limitations in the discussion regarding the assumptions about the input types and the handling of the scanf function, as well as the varying interpretations of the while loop's logic.

Who May Find This Useful

This discussion may be useful for programmers dealing with input validation in C, particularly those working with different data types and format specifiers in scanf.

Vavotaj
Messages
14
Reaction score
0
#include <stdio.h>
#include <string.h>

double faktoriel (double y);

void main()
{
double x, fac;
char yes[5] ="yes";
char answer[5];

do
{
printf("Enter number:\n");
scanf("%f", &x);

while (50>x || x>100)
{
printf("Number doesn't match \n"); //50<x<100
printf("Enter number again:\n");
scanf("%f", &x);
}
fac = faktoriel (x);
}while(strcmp (yes, answer)!=0);
return;
}

double faktoriel (double y)
{
double i, factor;


for(i=y; i>1; i--)
{
factor*=i;
}
printf("%f", factor);
return factor;
}
----------------------------------------------------------------------------------------
There are proly many mistakes xD
 
Technology news on Phys.org
Vavotaj said:
while (50>x || x>100)

//50<x<100

No idea what you are trying to do, but this is inconsistent.
 
to ppl enter number lower than 100 higher than 50
 
x>100 doesn't mean lower than 100.
 
but while x is higher than 100 or lower than 50 to to that code
 
or how should it be done ?
 
Code:
x > 100
means precisely what it suggest, x is greater than 100.

And || is OR, I think you were wanting && (AND).
 
The line of code that Borek pointed out is, in fact, correct. It will keep asking the user to enter a value until it's between 50 and 100.

Your post contains no questions, though, so I have no idea what you want from us.

- Warren
 
chroot said:
The line of code that Borek pointed out is, in fact, correct.

Oops, sorry. I was absolutely sure it was x>50 || x>100. Must be senior moment :eek:
 
  • #10
than why it is always repeating it self even if i enter number between 50 and 100 ?
 
  • #11
for exaple :
#include <stdio.h>

main()
{
double x;

printf("Enter number:");
scanf("%f", &x);

while (50>x || x>100)
{
printf("Number doesn't match \n");
printf("Enter number again:\n");
scanf("%f", &x);
}
}
 
  • #12
it works with int ... why it doesn't work with double ?
 
  • #13
It is always a good idea to run a program with a debugger. If you do this you will find that the value of x is nowhere close to the value you typed in. Hint: Your example program will work if you change x from a double to a float.
 
  • #14
but how do i do it in double ?
 
  • #15
You write programs to solve problems. Based on the complexity of your problem, you devise a solution that is most efficient/productive. What is your problem?
 
  • #16
my professor ... he demands double
 
  • #17
ah solution :
#include <stdio.h>

main()
{
float x;


(double) x;
printf("Enter number:");
scanf("%f", &x);
while (50>=x || x>=100)
{
printf("Number doesn't match \n");
printf("Enter number again:\n");
scanf("%f", &x);
}
}
:)
 
  • #18
Vavotaj said:
but how do i do it in double ?
I could tell you right now how to solve this problem, but that will not help you solve related problems in the future. You need to learn how to use reference material. There is no way to keep all the stuff associated with a language and its associated libraries in your head. The ability to quickly look things up in the reference material is key to having any success in programming.


So, I will instead tell you to RTFM: Read The Fine (or if you prefer, some other F word that is a tad more vulgar) Manual. I'll even give a specific thing to read up on this time: scanf. If you are still vexed, ask again, but please only do so after you have read the documentation.
 
  • #19
Vavotaj said:
my professor ... he demands double

Vavotaj said:
ah solution :
float x;


(double) x;
You will (or at least should) get points off if you offer this as your solution. In particular, (double) x; does absolutely nothing useful. In particular, it is not doing what your professor has asked of you.
 
  • #20
All i have bout scanf is int, char and float ... double is not mentioned
 
  • #21
10 seconds googling.

http://www.rt.com/man/scanf.3.html
 
Last edited by a moderator:
  • #22
Solution :

#include <stdio.h>

main()
{
double x;
printf("Enter number:");
scanf("%lf", &x);
while (50>=x || x>=100)
{
printf("Number doesn't match \n");
printf("Enter number again:\n");
scanf("%lf", &x);
}
}
 
  • #23
Solution :

#include <stdio.h>

main()
{
double x;



printf("Enter number:");
scanf("%lf", &x);
while (50>=x || x>=100)
{
printf("Number doesn't match \n");
printf("Enter number again:\n");
scanf("%lf", &x);
}
}
 
  • #24
Excellent. RTFM is a powerful technique. Except when the Fine Manual is not so fine, that is.
 
  • #25
Actualy Lousy Manual didn't have it so i looked at google more precise xD
 
  • #26
Vavotaj said:
Solution :

Code:
#include <stdio.h>

main()
{
	double x;
	

	
	printf("Enter number:");
	scanf("%lf", &x);
	while (50>=x || x>=100)
	{
		printf("Number doesn't match \n");
		printf("Enter number again:\n");
		scanf("%lf", &x);
	}
}
You're either coding to C90, in which case you can drop the "int" in front of main(void), but you'll have to explicitly specify a return value; or C99, in which case you have to have the "int" in front of main(void), but can let the return value be implicit; or C++, where you don't need the void, but otherwise the same as C99.
 
  • #27
return type double :/
I am not sure what your prof wants you to write code about, Instead of just directly pasting code, can you also post what it is you are trying to code. MOre help can be granted to you that way
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K