Thread: Help with calculator program.

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    1

    Help with calculator program.

    I'm still a beginner at C++. I've been working a little with classes but I heard that you should start with procedural programming before starting on object oriented programming. So I'm working on this procedural program and am currently stuck because the program seems to be skipping over the scanf that scans the char, cPrompt. It then just exits the function and goes back to main().
    Can anyone help me with this?
    Here's the code:
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    void promptTheUser(int firstNumber, int secondNumber);
    
    
    /*** AddTheNumbers ***/
    int addTheNumbers(int numberOne, int numberTwo)
    {
      numberOne = numberOne + numberTwo;
      return numberOne;
    }
    
    
    /*** SubtractTheNumbers ***/
    int subtractTheNumbers(int numberOne, int numberTwo)
    {
      numberOne = numberOne - numberTwo;
      return numberOne;
    }
    
    
    /*** MultiplyTheNumbers ***/
    int multiplyTheNumbers(int numberOne, int numberTwo)
    {
      numberOne = numberOne * numberTwo;
      return numberOne;
    }
    
    
    /*** DivideTheNumbers ***/
    int divideTheNumbers(int numberOne, int numberTwo)
    {
      numberOne = numberOne / numberTwo;
      return numberOne;
    }
    
    
    /*** pomptTheUser - Prompt the user to choose an option. ***/
    void promptTheUser(int firstNumber, int secondNumber)
    {
    
      char cPrompt;
    
      printf("prompt: ");
      scanf("%c", &cPrompt);
      printf("\n\n");
    
      if ((cPrompt == 'a') || (cPrompt == 'A'))
      {
        addTheNumbers(firstNumber, secondNumber);
        printf("The sum of the two numbers is: %i", firstNumber);
      }
    
      if ((cPrompt == 's') || (cPrompt == 'S'))
      {
        subtractTheNumbers(firstNumber, secondNumber);
        printf("The difference of the two number is: %i", firstNumber);
      }
    
    
      if ((cPrompt == 'x') || (cPrompt == 'X'))
      {
        multiplyTheNumbers(firstNumber, secondNumber);
        printf("The product of the two numbers is: %i", firstNumber);
      }
    
      if ((cPrompt == 'd') || (cPrompt == 'D'))
      {
        divideTheNumbers(firstNumber, secondNumber);
        printf("The quotient of the two numbers is: %i", firstNumber);
      }
    
      if ((cPrompt == 'q') || (cPrompt == 'Q'))
      {
        false;
      }
    }
    
    /*** main - Asks user if they want to coninue, asks user to enter two numbers,
                and then calls enterNumber() ***/
    int main(int argc, char *argv[])
    {
      printf("intro\n\n\n");
    
      char cQuit;
      int first;
      int second;
    
      printf("quit? ");
      scanf("%c", &cQuit);
    
      if ((cQuit == 'q') || (cQuit == 'Q'))
      {
        return 0;
      }
    
      printf("first #: ");
      scanf("%i", &first);
      printf("\n\n");
    
      printf("second #: ");
      scanf("%i", &second);
      printf("\n\n");
    
      promptTheUser(first, second);
    
      system("PAUSE");
      return 0;
    }
    I'm sure the rest of the program works ok. Please point out any other mistakes. Thank you.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Read the sacred scrolls young grasshopper, and learn the ways of scanf.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed