Thread: int value of char

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    28

    int value of char

    I have written a function which receives a char variable and checks it's int value for certain restrictions, but when I try to compare it with -1 it doesnt work and I don't know why.

    can anyone help me please?




    void INIT(int poly[]){
    int i=-1, n;
    const MAX=10;
    char temp, power;

    for (n=1;n<=MAX;n++){ /* stops after 9 good entries */
    printf("Enter power and coefficient\n");
    printf("power of %d stops receiving the polynomial\n",-1);

    scanf("%s ",&power);

    int rank=(power-'0');
    if(rank== -1) /* stops "for" loop if power= -1 */
    break;

    if(rank==i){ /* cheks the power hasn't been entered twice */
    printf("current power already entered\n");
    printf("begin again\n");
    return;
    }

    scanf("%s",&temp);
    int num=(temp-'0'); /* checks that the entry is legal
    if it's not it stops the function*/
    if(rank>=0 && (rank<=9))
    if(num>=0 &&(num<=999))
    poly[rank]=num;

    if(rank<0 && (rank>9)){
    printf("wrong entry please start again\n");
    return;
    }

    rank=i;
    } /* end of receiving polynomial */

  2. #2
    Unregistered
    Guest
    I have written a function which receives a char variable and checks it's int value for certain restrictions, but when I try to compare it with -1 it doesnt work and I don't know why.
    you cant be sure of the signedness of a char so if you need to check it for a negative value then declare it as signed

    signed char temp, power;

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    28
    Originally posted by Unregistered

    you cant be sure of the signedness of a char so if you need to check it for a negative value then declare it as signed

    signed char temp, power;
    I tried it but it still doesn't work.

    Actually none of the restrictions work.
    (It's really sad.)
    Last edited by dana_h; 05-12-2002 at 11:59 AM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why have you defined temp and power as char's, when you really need int's (or so it seems to me).

    I'd suggest you make temp and power int's, then change scanf accordingly. Make sure you check the return code from scanf to ensure you read a valid number though, before you start using it.

    Also, this
    >int num = (temp - '0');
    is c++ style code. You should define you variables at the top of a function, not in the middle.

    >(It's really sad.)
    Don't be sad, it's all fun learning C
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    28
    I have to receive anything frm the user and when I get garbage the function should stop.
    that's why I've used "scanf %s".

    It does ignore the garbage but it doesn;t stop untill it receives 9 valid entries.


    Originally posted by Hammer
    Don't be sad, it's all fun learning C
    Usualy I'd agree with you.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's a sample program showing how to validate user input. It asks for a number between 0 and 10, or -1 to exit. Anything else is considered to be an error.

    Hopefully, this may help your understanding.
    Code:
    #include <stdio.h>
    #define MAX 10
    
    int main(void)
    {
    	int num1;
    	
    	for (;;)
    	{
    		printf ("Please enter an int between 0 and %d, or -1 to exit\n", MAX);
    		if (scanf("%d", &num1) == 1)
    		{
    			if (num1 == -1)
    				break;
    			if (num1 >= 0 && num1 <= MAX)
    				printf("You entered %d\n", num1);
    			else
    				printf("%d is out of range\n", num1);
    		}
    		else
    		{
    			printf("That was not a number\n");
    			while (getchar() != '\n'); /* Eat duff character from buffer */
    		}
    	}
    	return (0);
    }
    Last edited by Hammer; 05-12-2002 at 04:34 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    28
    Thaks.

    I'm going to try something like that.
    Atleast now I know how to do it.

    I hope it will work

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    #include <stdio.h>
    #define MAX 10
    
    int main(void)
    {
    	int num1;
    	
    	for (;;)
    	{
    		printf ("Please enter an int between 0 and %d, or -1 to exit\n", MAX);
    		if (scanf("%d", &num1) == 1)
    		{
    			if (num1 == -1)
    				break;
    			if (num1 >= 0 && num1 <= MAX)
    				printf("You entered %d\n", num1);
    			else
    				printf("%d is out of range\n", num1);
    		}
    		else
    		{
    			printf("That was not a number\n");
    			while (getchar() != '\n'); /* Eat duff character from buffer */
    		}
    	}
    	return (0);
    }--------------------------------------------------------------------------------
    will not work. getchar is a member of conio and it is not included.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Lynux-Penguin

    will not work. getchar is a member of conio and it is not included.
    Wrong matey, getchar() is ANSI and is in stdio.h. Maybe you're thinking of getch(), getche() etc.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    28
    Originally posted by Hammer
    Here's a sample program showing how to validate user input. It asks for a number between 0 and 10, or -1 to exit. Anything else is considered to be an error.

    Hopefully, this may help your understanding.
    Code:
    #include <stdio.h>
    #define MAX 10
    
    int main(void)
    {
    	int num1;
    	
    	for (;;)
    	{
    		printf ("Please enter an int between 0 and %d, or -1 to exit\n", MAX);
    		if (scanf("%d", &num1) == 1)
    		{
    			if (num1 == -1)
    				break;
    			if (num1 >= 0 && num1 <= MAX)
    				printf("You entered %d\n", num1);
    			else
    				printf("%d is out of range\n", num1);
    		}
    		else
    		{
    			printf("That was not a number\n");
    			while (getchar() != '\n'); /* Eat duff character from buffer */
    		}
    	}
    	return (0);
    }


    I changed my function and it works great


    Thanks !!! (I'm so happy)

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    9

    some observations

    /*
    as you wrote "but when I try to compare it with -1 it
    doesnt work and I don't know why."
    going through your programm there are certain things which
    would be best to bring to your notice
    1.if(rank<0 && (rank>9))
    now this condition would never be satisfactory
    why
    because rank can never be <0 and at the same time >9
    both are going opposite directions <0 is going
    towards -1,-2 and so on whereas >9 is becoming
    10,11 and so on; therefore || should be used like
    if(rank<0 || (rank>9))//now this is ALLRIGHT
    because either rank could be <0 or >9;
    in one other of your statements
    if(rank>=0 && (rank<=9))//using && is ok
    because yes rank can be >=0 and at the same time
    <=9 why because 9 is >=0 and 0 is <=9 both would at
    some stage meet eachother >=0 is being 1,2 and so
    on whereas <=9 is being 8,7 so it is evident why your
    using && in one statement is correct
    and wrong in other;
    2. why do you use %s in scanf when you declared power
    as
    char power?
    althou it does not generate errors,
    but scrutinize it and you would find in power the value
    stored would be of one byte only wheather you use
    %s or %c;allright u would say no it wont be like that;
    then see you wrote scanf("%s ",&power);
    the above statement when executed would actually
    make the scanf to work twice that is if you hit A and
    then hit ENTER still you will have to hit some other key
    and then ENTER again;all this because you kept space
    between"%s " and not "%s"; but even then the
    character which is hit first would only be
    recorded and not the second one in this
    case recorded charater would be A;
    scanf("%s",&power);//now the scanf will work
    normall as it should
    that is only once when you will hit ENTER it would
    teminate but still even if you hit ABC and then hit enter
    only A is the recorded character BC went unrecognised;
    so my suggestion is use only %c when it is just char
    power;if you want to store multiple values in power
    then make it an array;then using %s would
    make sense;
    3. you have already in the begining of the function made
    int i=-1;
    but still you write these two if statements together;
    if(rank== -1) // stops "for" loop if power= -1
    {
    printf(" i am breaking");
    break;
    }
    if(rank==i)
    { // cheks the power hasn't been entered
    twice
    printf("current power already
    entered\n");
    printf("begin again\n");
    return;
    }
    //////////
    have a look above once more and try to comprehend
    what is the diffrence
    between -1 and i ?when you made i=-1 in the
    beginging!
    so if the condition is satisfied for rank==-1
    the control will go out of loop allready and will never
    reach
    the immediate if statement which is if(rank==i).so this if
    statement is only occupying the space of your
    programm
    and nothing else. only first if statement
    that is if(rank==-1) would do the job;
    4.from above function it is evedinet that rank would
    only be -1 when
    someone hits '/' or ther maybe some other way
    too.but with what exactly do you want to compare -1
    with? is it with rank or num or temp or with
    something else? then maybe your solution might
    appear
    please tell this much.
    5.i am just a student and you seem to be some
    professional
    so please dont mind if i am wrong somewhere please
    correct me
    i my self need guidence of your esteemed self;
    6.this is how i tried to understand your function

    #include<includes.c>
    void INIT(int poly[])
    {
    int i=-1, n;
    const MAX=10;
    char temp,power
    int rank;int num;
    for (n=1;n<=MAX;n++)
    { // stops after 9 good entries
    printf("Enter power and coefficient\n");
    printf("power of %d stops receiving the
    polynomial\n",-1);
    fflush(stdin);
    scanf("%s",&power);
    fflush(stdin);
    printf("\n%c=power\n",power);//just a check
    rank=(power-'0');
    printf("rank: %d\n",rank);//just acheck
    if(rank== -1) // stops "for" loop if power= -1
    {
    printf(" i am breaking");//wanted to make sure if control reaches here
    break;
    }
    if(rank==i)
    {// cheks the power hasn't been entered twice
    printf("current power already entered\n");
    printf("begin again\n");
    return;
    }

    fflush(stdin);
    printf("value for temp ");//just a check
    scanf("%s",&temp);
    fflush(stdin);
    printf("\ntemp=%c",temp);//just a check
    num=(temp-'0'); // checks that the entry is legal
    //if it's not it stops the function
    printf("\nnum:-%d\n",num);//just a check
    if(rank>=0 && (rank<=9))
    {
    if(num>=0 &&(num<=999))
    {
    poly[rank]=num;
    printf("poly[%d]=%d\n",rank,poly[rank]);//just a check
    }
    }
    if(rank<0 && (rank>9))//instead of && it should be ||
    {
    printf("wrong entry please start again\n");
    return;
    }

    rank=i;
    printf("rank=%d n=%d",rank,n);//just a check;
    } // end of receiving polynomial

    }

    void main()//to make your fuction work i made this
    //main from where i called it;
    {
    char *one;
    one=(char *) malloc(sizeof("hello"));
    one="hello";
    clrscr();
    printf("%s\n",one);//checking if hello has been inserted in one or not;
    INIT((int *)one);//and passed this one;
    getch();

    }
    */

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    28
    Wow,
    I'm impresed.
    I'm a student too and at the very begining of Cprog.
    That's why I ask so many Q.

    I did make some stupid mistakes but now after much help
    I got it right, it's pretty long but here goes:

    One more thing:
    I only used i= -1 so when first I check (power==i)
    it wont stop running (I can use power 0).
    After that at the end of the loop I assign "i=power;"
    so next time if power was already entered it will stop running.

    ************************************************** **

    void INIT(int poly[]){
    int i=-1,n;
    int temp,num,power;
    const MAX=9;

    /* checks that the entry is legal if it's not it stops function*/
    for (n=MAX;n>0;n--) /* stops after 9 good entries */
    {
    printf("Enter power\n");
    printf ("Please enter an int between 0 and %d, or -1 to exit\n", MAX);

    if (scanf("%d", &power) == 1)
    {
    if (power == -1)
    break;
    if (power >= 0 && power <= MAX)
    printf("You entered %d\n", power);
    else
    {
    printf("%d is out of range\n", power);
    return;
    }
    }
    else
    {
    printf("That was not a number\n");
    return;
    while (getchar() != '\n');
    }

    if(power==i||(poly[power])) /* cheks that the power hasn't been entered twice */
    {
    printf("current power already entered\n");
    return;
    }
    printf("Enter coefficient\n");
    printf("Please enter an int between %d and %d\n",-999,999);

    if (scanf("%d", &temp) == 1)
    {
    if (temp >= -999 && temp <= 999)
    printf("You entered %d\n", temp);
    else
    {
    printf("%d is out of range\n", temp);
    return;
    }
    }
    else
    {
    printf("That was not a number\n");
    return;
    while (getchar() != '\n');
    }

    poly[power]=temp;
    i=power;

    } /* end of receiving polynomial */

    /* next part prints the polynomial as received */

    i=9;
    num=poly[i];
    printf("%dx%c%d",num,'^',i);

    for(i=8;i>=0;i--)
    {
    num=poly[i];
    if(num>=0)
    printf("%c%dx%c%d",'+',num,'^',i);
    else
    printf("%dx%c%d",num,'^',i);
    }
    } /* end of function */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM