Beginner, cant get my program to run :-(

This is a discussion on Beginner, cant get my program to run :-( within the C Programming forums, part of the General Programming Boards category; question im asked to do : write a program that acts as a simple printing calculator i should accept in ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    12

    Beginner, cant get my program to run :-(

    question im asked to do :
    write a program that acts as a simple printing calculator i should accept in the form of number then operator.

    The following operators should be reconised '+', '-', '/', '*' 'S' 's' 'E' 'e'
    The S operator tells the program to set the accumulator to the typed in number.
    The E operator tell the program to excute to the end
    Make certain the program checks for division by zero.
    My effort so far;

    Code:
    #include <stdio.h>
    
    int main (void)
    
    {
    	double value1
    	float accumulator = 0.0f;
    	char operator;
    
    	printf ("\t\tCalulator\n"); 
    	printf ("\t\t---------\n");
    
    	printf("Type in your test\n");
    	scanf("%d, %c", value1, operator);
    
    
    
    	if (value1 == 0)
    		printf("Divsion by zero is not allowed");
    
    	switch (operator)
    	{
    	     case 'S':
    	     accumulator = value1;
    		printf("%2.f\n", value1);
    	     break;
    
    	     case 's';
    	     accumulator = value1:
    		printf("%2.f\n", value1);
    	     break;
    
    	     case '+':
    	     accumulator+= value1;
    		printf("%2.f\n", value1;
    	     break;
    
    	     case '-':
    	     accumulator= accumulator - value1;
    		printf("%2.f\n", value1);
    	     break;
    
    	     case '/':
    	     accumulator= accumulator / value1;
    		printf("%2.f\n", value1);
    	     break;
    
    	     case '*':
    	     accumulator= accumulator - value1;
    		printf("%2.f\n", value1);
    		
    	     break;
    
    	     case 'E':
    	  	printf("End of Tests");
    	     break;
    
    	     case 'e':
    		printf("End of Tests");
    	     break;
    	 
    	     default:
    		printf("UNKNOWN OPERATOR\n");
    	     break;
    
    }
    
    	return 0;
    
    }
    any help or tips would be great

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What problems are you having?


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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    Please be specific in the future. What's the actual problem? Does it compile? Does it crash when you try to run it? Does it give incorrect output? What output does it give and what do you expect. Cut and paste the compiler errors you don't understand.

    I couldn't get it to compile as you posted it. You're missing a semicolon in your variable declarations and a closing parenthesis in the printf in your case '+'. Also, your case 's' has some mixups between colon : and semicolon ;. The colon is for the case label, the semicolon terminates a statement. You need to read up on scanf. It takes the address of the variable you want to store in there, so you need:
    Code:
        scanf("%f, %c", &value1, &operator);
    %d is for integers, %lf is for floats.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anduril462 View Post
    %lf is for floats.
    f is for float, lf is for double.


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

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    My guess is that it doesn't even compile:
    Code:
    printf("%2.f\n", value1;
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    Quote Originally Posted by quzah View Post
    f is for float, lf is for double.


    Quzah.
    Yes, thanks for catching that. OP wants %lf since value1 is a double.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. triggering another program to run using serial port
    By infineonintern in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2009, 05:16 AM
  2. Help on making program run more efficiently
    By peeweearies in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 02:01 AM
  3. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. Replies: 3
    Last Post: 07-11-2005, 03:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21