I want to enter a number, but the number cannot contain 8 and 9. How can I do it?
THX.
I want to enter a number, but the number cannot contain 8 and 9. How can I do it?
THX.
>I want to enter a number, but the number cannot contain 8 and 9. How can I do it?
I find myself repeating this over and over. Read the input as a string and validate it. Then if you need to use it as a number, convert the string.
-Prelude
My best code is written with the delete key.
Can u tell me how to convert number to string and then validate it? I just a beginner in C.
THX.
Here's a simple example:
-PreludeCode:#include <stdio.h> #include <stdlib.h> static int validate ( char *input ) { for ( ; *input != '\0'; input++ ) if ( *input == '8' || *input == '9' ) return EXIT_FAILURE; return EXIT_SUCCESS; } int main ( void ) { char buffer[BUFSIZ]; if ( fgets ( buffer, BUFSIZ, stdin ) != NULL ) { if ( validate ( buffer ) == EXIT_SUCCESS ) (void)puts ( "It's good, work with it" ); else (void)puts ( "Not good, contains an 8 or 9" ); } return EXIT_SUCCESS; }
My best code is written with the delete key.
What are you using tp get input? If it's something such as getc or fgets, then it's already a string. If it's scanf, then you just give it %s as a string type argument.
As far as conversion goes, look up the itoa() function.
starX
www.axisoftime.com
---------------
starX
www.axisoftime.com
Can you briefly describe how the following code
work. I can not understand how it work.
Thank you very much..
#include <stdio.h>
#include <stdlib.h>
static int validate ( char *input )
{
for ( ; *input != '\0'; input++ )
if ( *input == '8' || *input == '9' )
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
int main ( void )
{
char buffer[BUFSIZ];
if ( fgets ( buffer, BUFSIZ, stdin ) != NULL )
{
if ( validate ( buffer ) == EXIT_SUCCESS )
(void)puts ( "It's good, work with it" );
else
(void)puts ( "Not good, contains an 8 or 9" );
}
return EXIT_SUCCESS;
}
Perhaps you can be more specific. What part is difficult to understand?
First she calls fgets() and immediately checks the return value. Then she passed the string into the validation function which returns the macro EXIT_SUCCESS if 8 & 9 are not present, EXIT_FAILURE if they are. ( EXIT_SUCCESS might represent '1' (true), and EXIT_FAILURE '0' (false). )
The point is, in programming, you must be inquisitive. Stop saying "can you show me how?" and start saying "how can I figure this out?"...
No 8's or 9's sounds like you want to work in octal(base 8). If this is the case take in any numbers from the user and use:
// Code
printf("%o", number);
to display it.
Here is the cheap way of doing it for beginners.
Code:#include<stdio.h> int main() { int inum; printf("Enter a number: "); while ( ( scanf("%d",&inum) != 1) || inum == 8 || inum == 9 ) { //clear input in standard input buffer fflush(stdin); printf("The number was %d, Try again: ",inum); } return 0; }
>fflush(stdin);
Please don't teach people this one, it's behaviour is undefined and therefore should not be used. There are better ways to clear the input buffer.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I know about this issue however fflush() complies with ANSI C.
If you knew about the issues, then why write code this way? Yes, fflush() is ANSI, BUT it is designed for OUTPUT streams, not INPUT.Originally posted by TK
I know about this issue however fflush() complies with ANSI C.
K&R2 p242
int fflush(FILE *stream)
On an output stream, fflush causes any buffered but unwritten data to be written; on an input stream effect is undefined.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Code:#include<stdio.h> int main() { int inum; printf("Enter a number: "); while ( ( scanf("%d%*",&inum) != 1) || inum == 8 || inum == 9 ) { printf("The number was %d, Try again: ",inum); } return 0; }
Yes, I want to use stack to convert Base-8 number to decimal.
Below is my code but contain some errors. I tried thousand times already but still can not solve the problem. I hope someone can help me to find out the problem.
THX.
#include<stdio.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include"StackInterface.h"
#define BASE 10
void main()
{
int Qoutient, Number, sum=0, count=0;
char Remainder;
Stack_t S;
InitializeStack(&S);
printf("BASE-8 : ");
scanf("%d", &Number);
Qoutient = Number;
do
{
Remainder = Qoutient % BASE * pow(8,count);
Push(Remainder, &S);
Qoutient/=BASE;
count++;
} while(Qoutient!=0);
while(!EmptyStack(S))
{
Pop(&S, &Remainder);
sum+=Remainder;
}
printf("BASE-10: %d\n", sum);
}
Code:int validate_num(int num) { register int rm = 0; while(num) { rm = num%10; if(rm == 9 || rm == 8){ return rm;//invalid .. return the num that invalidate it } num = num/10; } return 0; //valid }