Thread: Command-line Calculator Error:

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Command-line Calculator Error:

    I'm having an error, when it reaches the answer, it always gives me 0!!! I attatched the image of the error, as well as the C file where it resides.

    Code:
    #include <stdio.h>
    
    
    int main ()
    {
    	int r = 0;
    	int l = 0;
    	int b = 0;
    	int i = 0;
    	int n = 0;
    	int a = 0;
    	printf(" MARIOTUTORIALS CALCULATOR\n");
    	printf("\n");
    	printf("\n");
    	printf("\n"); 
    	printf("[1]  Add\n");
    	printf("[2]  Subtract\n");
    	printf("[3]  Multiply\n");
    	printf("[4]  Divide\n");
    	printf("\n");
    	printf("\n");
    	printf("Choose a number: $~ ");
    	scanf("%d", &n); 
    	getchar();	
    	if(n == 1)
    	{
    		printf("\n\nHow many numbers do you wish to add? ");
    		scanf("%d", &a);
    	}
    	for(i=0; i < a; i++)
    		{
    			printf("Your Number(s):  ");
    			scanf("%d", &l);
    			int b = l + b;
    		}
    		printf("\n\nANSWER: %d", b);
    		getchar();
    return(0);
    }
    Attached Images Attached Images Command-line Calculator Error:-screen-shot-2011-10-31-10-10-28-am-png 
    Attached Files Attached Files
    • File Type: c main.c (644 Bytes, 211 views)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    2b or not 2b, that is the question.

    Code:
    $ gcc -Wshadow -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:33: warning: declaration of ‘b’ shadows a previous local
    bar.c:7: warning: shadowed declaration is here
    bar.c:5: warning: unused variable ‘r’
    You have one b too many.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    	for(i=0; i < a; i++)
    		{
    			printf("Your Number(s):  ");
    			scanf("%d", &l);
    			int b = l + b;
    		}
    Remove the int keyword from your b = l + b; line (5)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems building a "command-line" calculator
    By mmario10 in forum C Programming
    Replies: 6
    Last Post: 10-31-2011, 04:27 AM
  2. Command line how to handle optional command.
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2010, 11:41 PM
  3. cl : Command line error D8004 : '/W' requires an argument
    By kakayoma in forum Windows Programming
    Replies: 1
    Last Post: 08-22-2009, 06:09 AM
  4. Ideas for error checking from command line
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-16-2009, 06:55 AM
  5. command line agr
    By Skeptic in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 04:17 PM

Tags for this Thread