Thread: Help Me!

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Unhappy Help Me!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define SIZE 4
    
    int main()
    {
    char a[SIZE];
    char *ptrA=a;
    int c[SIZE];
    int count;
    int sum;
    int first,second,third,fourth;
    int varify=1;
    
    while(varify==1)
    {
    //get nums
    	printf("Enter Number for Array\n");
    	
    	for(count=0;count<SIZE;count++)
    	{
    		scanf("%d",&a[count]);
    	}
    //convert to ints
    	for(count=0;count<SIZE;count++)
    	{
    		c[count]=atoi(ptrA);
    	}
    //checking
    	for(count=0;count<SIZE;count++)
    	{
    		if(isdigit(c[count])==1)
    		{
    			printf("Must A Numeric Value");
    			varify=1;
    		}
    		else
    		{
    			varify=0;
    		}
    	}
    }
    
    
    //printing
    	printf("Input Reads:\n");
    	
    	for(count=0;count<SIZE;count++)
    	{
    		
    		printf("%d\n",a[count]);
    	}
    
    //adding
    	printf("Summing Numbers\n");
    	
    	first=a[0];
    	second=a[1];
    	third=a[2];
    	fourth=a[3];
    		
    //printing
    	sum=first+second+third+fourth;
    	printf("Sum is: %d\n",sum);
    
    }
    This runs but then errors once it is finished! I dont know why.
    Thanx!
    Dave

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop double posting. Yes, the code is slightly different between your other post and this, but since the bulk of the application is the same, you shouldn't be spamming the board with posts.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What errors?

    Could be this, since you try to print a char as an int.
    printf("%d\n",a[count]);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    It runs, but errors!

    this thing does run and the output is right, i jus dont c why the atoi isnt working right. Hope you can shed some light on this one. I have 2 get it running without it causeing an error. The compiler has 0 errors and 0 warnings. Jus when it runs it errors!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define SIZE 4
    
    int main()
    {
    char a[SIZE];
    int c[SIZE];
    int count;
    int sum;
    int first,second,third,fourth;
    int varify=1;
    
    while(varify==1)
    {
    //get nums
    	printf("Enter Number for Array\n");
    	
    	for(count=0;count<SIZE;count++)
    	{
    		scanf("%d",&a[count]);
    	}
    //convert to ints
    	for(count=0;count<SIZE;count++)
    	{
    		c[count]=atoi(a);
    	}
    //checking
    	for(count=0;count<SIZE;count++)
    	{
    		if(isdigit(a[count])==1)
    		{
    			printf("Must A Numeric Value");
    			varify=1;
    		}
    		else
    		{
    			varify=0;
    		}
    	}
    }
    
    
    //printing
    	printf("Input Reads:\n");
    	
    	for(count=0;count<SIZE;count++)
    	{
    		
    		printf("%d\n",a[count]);
    	}
    
    //adding
    	printf("Summing Numbers\n");
    	
    	first=a[0];
    	second=a[1];
    	third=a[2];
    	fourth=a[3];
    		
    //printing
    	sum=first+second+third+fourth;
    	printf("Sum is: %d\n",sum);
    
    }
    thanks for all your help!
    aol screen name: dt02400

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I already told you why atoi doesn't work right. BECAUSE YOU ARE USING SCANF WRONG! You are not scanning strings of digits, you are scanning an integer. Period.

    [edit]
    char buf[BUFSIZ];
    int x;
    scanf("%s", buf );
    x = atoi( buf );

    That is how you read a string of numbers and convert it with atoi. What you're doing is scanning INTEGERS into a character array, and then you're trying to treat the array of characers as a string.

    Example: I input: 10 20 30 40

    You try running atoi() on that "string", and get: <character whose desimal value is 10, character whose decimal value is 20, characer whose decimal value is 30, character whose decimal value is 40> and you're expecting that to turn out to be "real" integers. Sorry, but that's not how it works.
    [/edit]

    Quzah.
    Last edited by quzah; 11-09-2002 at 01:33 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed