Thread: Help with Calculator Program using Scanf, Printf, Float variables, and char vari.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    Help with Calculator Program using Scanf, Printf, Float variables, and char vari.

    hello i am trying to write a simple program to calculate addition, subtraction, division, and multiplication; however when i execute my program it only gets to entering a Y or N for continueing before it crashes however i don't get any errors from my compiler, i was wondering if it was one specific thing i was doing with my code or if i should just restart and try some other way todo it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main (void)
    { float num1,num2,sum; char operation, ques;//defines float variables to hold user input and for calculations and defines character types for user input
    printf("would you like todo a math problem? type y/n\n");//asks user for operators
    scanf("%c",ques);//scans for user input and sets to character type variable QUES
    while(ques=='y')//states that as long as user inputs a Y for answer to last input that it will continue to the math
    {
    printf("please enter a number you wish to to use: \n");//asks  user for input
    scanf("%f",&num1);//stores user input into num1 as float
    printf("please enter an operator to which you would like to use with your number: \n");// asks user for input
    scanf("%c",&operation);//stores user input to operation variable as character type variable
    printf("please enter the next number you would like to use: \n");//asks user for input
    scanf("%c",&num2);//stores user input to operation variable as character type variable
    if (operation=='+')//states that if the character type variable operation is the addition sign it will print the sum of float num1 and float num2
    printf("the sum is: %f\n",num1+num2 );//calculates number and prints result
    if (operation=='-')
    printf("the sum is: %f\n",num1-num2 );//same as above^ for subtraction
    if (operation=='*')
    printf("the sum is: %f\n",num1*num2 );//same as above^for multiplication
    if (operation=='-')
    printf("the sum is: %f\n",num1/num2 );//same as above^ for division
    printf("would you like todo another problem?");//prompts user if he would like to restart and use more numbers
    scanf("%c",&ques);//takes user answer
    }
    printf("Guess you don't need me anymore!");//end of program statement if user doesn't enter a 'Y' into ques variable
    return 0;//"ends program by returning a 0 to to the function main()"//
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Please indent your code properly.
    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
    Feb 2011
    Posts
    52
    Line 7: '&' missing. You perhaps overlooked it. Right one is, scanf("%c",&ques);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-02-2011, 09:17 PM
  2. scanf/printf char troubles
    By XantuVolo in forum C Programming
    Replies: 2
    Last Post: 04-10-2011, 01:51 PM
  3. cannot Scanf float? terminates program
    By hirano in forum C Programming
    Replies: 16
    Last Post: 03-08-2010, 11:53 AM
  4. Reading in char for float with scanf
    By ramparts in forum C Programming
    Replies: 3
    Last Post: 11-05-2006, 01:05 AM
  5. printf: zero padding float
    By Laserve in forum C Programming
    Replies: 22
    Last Post: 06-16-2004, 06:08 PM

Tags for this Thread