Thread: Beginner - calculator

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    18

    Beginner - calculator

    Hello. I am just starting C programmer if I can call myself a programmer right now but let's get straight into the problem. The main point of the program is to calculate equations just like a standard calculator but I wanted to do it myself. I don't understand what the problem is right now but I've managed to create a program that asks for both values but somehow it doesn't want to ask for an operator (*, /, + etc). What's wrong with my code that the terminal skips the scanning part for the operator?

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int value1, value2, answer;
    	char operator;
    	
    	printf("Enter the first value: ");
    	scanf("%i", &value1);
    	
    	printf("Enter the second value: ");
    	scanf("%i", &value2);
    	
    	printf("Enter the operator: ");
    	scanf("%c", &operator);
    	
    	if(operator == "+")
    		answer = value1 + value2;
    	if(operator == "-")
    		answer = value1 - value2;
    	if(operator == "*")
    		answer = value1 * value2;
    	if(operator == "/")
    		answer = value1 / value2;
    	
    	printf("This is your result:\n");
    	printf("%i\n", answer);
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Welcome to the forum. This entry in our FAQ has an explanation for the problem you're seeing: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com
    If you need clarification, just ask.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    Okay, I get what the problem is now. I've changed it but why does the answer always becomes 0? I don't see any problem with the if statement. I've told the program to add value1 and value2 if the operator entered is "+". So, what is the actual problem?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The answer isn't always zero. But zero is definitely a possibility; for instance 1/2 is zero (well, it's zero remainder one, but you didn't ask for the remainder).

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    Wait, I don't get it. So how should it look then?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose the question is what kind of output do you want to get?

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    Well the output of a standard calculator. If the first value is 5, second value is 5 and an operator is + then the answer should be equalled to 10. That's the point I'm trying to make.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MrLazio View Post
    Well the output of a standard calculator. If the first value is 5, second value is 5 and an operator is + then the answer should be equalled to 10. That's the point I'm trying to make.
    And that's what you have. (Assuming you actually did fix the read-the-operator issue. If you didn't fix the read-the-operator issue, well then, you would need to fix that.)

  9. #9
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    Look at the screenshot below. The coding looks OK. It reads the operator now but doesn't give the expected answer. While it should be 10 it still gives 0 in any case. Rather it is 5*5 or 10/2, it is always 0. Have a look.
    http://iv.pl/images/71653172978004629313.png

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For a single character, you should only be using single quotes.

    Code:
    // incorrect
    if(operator == "+")
    
    // correct
    if(operator == '+')

  11. #11
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    Quote Originally Posted by Matticus View Post
    For a single character, you should only be using single quotes.
    You're the beast Matticus! Thank you!

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Matticus View Post
    For a single character, you should only be using single quotes.

    Code:
    // incorrect
    if(operator == "+")
    
    // correct
    if(operator == '+')
    Good catch! (The Nyquil is kicking in and I haven't even taken it yet! Sigh.)

    Your compiler should be screaming at you about that; mine says "warning: comparison between pointer and integer" for each of those lines.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by MrLazio View Post
    You're the beast Matticus! Thank you!
    I'll take that as a compliment
    (Roar)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner calculator program problem
    By Cjof in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2013, 03:17 AM
  2. Beginner's Calculator: Problem with expressions
    By SpudCake in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2011, 05:36 AM
  3. help with simple calculator (beginner)
    By philthemn in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2006, 04:05 AM
  4. Replies: 2
    Last Post: 11-08-2005, 01:16 PM
  5. C++ Beginner Simple Calculator
    By mom in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:51 AM