Thread: switch statements

  1. #1
    Registered User
    Join Date
    Jul 2011

    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
    Search the web for things like reverse polish notation and shunting yard algorithm.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Around 8.3 light-minutes from the Sun
    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
    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.

    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, 05:57 PM
  2. Switch statements ...
    By twomers in forum C Programming
    Replies: 2
    Last Post: 01-06-2006, 07:14 AM
  3. switch statements
    By joshua in forum C Programming
    Replies: 3
    Last Post: 11-21-2005, 03: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, 05:44 PM

Tags for this Thread