switch statements

This is a discussion on switch statements within the C Programming forums, part of the General Programming Boards category; Hello, I am new to programming and new to this forum. I am in the process of writing a calculator ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    switch statements

    Hello, I am new to programming and new to this forum. I am in the process of writing a calculator program and I know i have to use a switch statement to jump to either a multiplication, division, addition or subtraction function if the characters "*/+-" are used. How do i set up a switch statement to pick up any of these characters out of a string such as *8 (multiply 8) and take that 8 to a multiplication function where it will be solved and return a value


    Thank You

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Search the web for things like reverse polish notation and shunting yard algorithm.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    This is a recurring assignment, take a look at the Shunting-yard algorithm. It will show you everything you need.

    EDIT: Too slow.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to stop and think about how you would do it on paper. How are you writing your lines of input?

    <operator> <N1> <N2>
    <N1> <operator> <N2>

    Then, make a program that can read the pieces you need from a line of text. There is a FAQ on getting input, but something simple like scanf should get you started.

    Give it a shot, post your code when you are stuck, along with the errors/problems you are having.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch statements.
    By elixon in forum C Programming
    Replies: 2
    Last Post: 11-30-2006, 04:57 PM
  2. Switch statements ...
    By twomers in forum C Programming
    Replies: 2
    Last Post: 01-06-2006, 06:14 AM
  3. switch statements
    By joshua in forum C Programming
    Replies: 3
    Last Post: 11-21-2005, 02:26 AM
  4. Switch Statements.
    By RealityFusion in forum C++ Programming
    Replies: 16
    Last Post: 08-19-2003, 11:55 PM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 04:44 PM

Tags for this Thread


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