Thread: A simple "printing" calculator... (Yeah, right!!)

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    A simple "printing" calculator... (Yeah, right!!)

    Hi,

    I am a student taking an Intro C programming course and I need help! My assignment is below. I have written the code to best of my ability and it even seems to compile fine, but when I enter numbers into the calculator the whole thing crashes. I have no idea why or even how to go about figuring it out. My "crash" error is at the bottom of the page... Please help me!

    My Assignment:
    http://calpyso-poet.com/assignment.bmp

    My Code:
    Code:
    // Homework 4.cpp : Defines the entry point for the console application.
    //
     
    #include "stdafx.h"
     
     
    int main (void)
    {
        float accumulator, value;
        char op;
     
        printf ("Enter number.\n");
     
        do {
            scanf_s ("%f %c", &value, &op);
     
           switch (op)
          {
     
        case 's':
                    accumulator = value; break;
     
        case '+':
                    accumulator = accumulator + value; break;
     
        case '-':
                    accumulator = accumulator - value; break;
     
        case '*':
                    accumulator = accumulator * value; break;
     
        case '/':
                    if ( value == 0 )
                    printf ("Divison by zero does not compute.\n");
                 else
                    accumulator = accumulator / value; break;
            }
     
                } while (op != 'e' );
        return 0;
    }
    My Error:
    http://calpyso-poet.com/error.bmp

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    accumulator isn't initialized (it contains garbage) and then you use it in your calculations.

    Consider:
    * Initializing your variables (to 0 perhaps)
    * Indenting properly
    * Shortening your variable names.
    * including stdio.h

    ie:
    Code:
    float   accum = 0.0f,
            val = 0.0f;
    /* etc... */
    btw your assignment says 'E' ends the program and 'S' sets the accumulator. C is case-sensitive, you *could* lose marks. 's' isn't the same as 'S'.

    I would, read the string from the user using fgets() and use sscanf(), just so there's no possibility they can break it by entering say,
    Code:
    55 S 0 E
    That or, flush stdin (don't use fflush(stdin) see the FAQ).
    Last edited by zacs7; 11-05-2007 at 10:11 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > * Shortening your variable names.
    No, they're fine as they are.
    Too many noobs show up with single letter identifiers, or identifiers with all the vowels ripped out.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM