Thread: Newbie question

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Newbie question

    Hi all. I am currently taking a beginning class in C programming and am working on an assignment where I need to prompt the user to enter an expression then display the expression and the result. e.g

    /*
    Enter INT Expression: 6 / 2
    Result: 6 / 2 = 3
    */

    I know how to store the INT values using the scanf function, but I'm not sure how I should go about storing the operator. Also once I have the operator stored, how should I go about writing an expression to store the result. I know how if the operator was a constant, e.g. "x + y = result", but how should I approach it when the operator is a variable. Thanks in advance for the help.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The operator can just be a char - use %c in scanf. You represent the possible operators as '*', etc...

    Code:
    if(operator == '/') {
        result = op1 / op2;
    }
    Also - if this is a C problem, it should be in the C board. C# is a different language.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well here is a hint...

    Code:
    scanf("%d %c %d", &num1, &op, &num2);
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by fossage View Post
    Hi all. I am currently taking a beginning class in C programming and am working on an assignment where I need to prompt the user to enter an expression then display the expression and the result. e.g

    /*
    Enter INT Expression: 6 / 2
    Result: 6 / 2 = 3
    */

    I know how to store the INT values using the scanf function, but I'm not sure how I should go about storing the operator. Also once I have the operator stored, how should I go about writing an expression to store the result. I know how if the operator was a constant, e.g. "x + y = result", but how should I approach it when the operator is a variable. Thanks in advance for the help.
    use switch case.switch takes any char or int and checks for it in its cases.so you can input two numbers and the operator and then keep the operator as switch(op),check for the case and do the required operation.an eg
    Code:
    switch(ch) //say ch inputted is '+'
    {
    case '+':result=a+b;
    .........
    ....rest of the program
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM