Jump to content

[C] Calculator. help?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
dBx

dBx

    Newbie

  • Members
  • Pip
  • 5 posts

//Calculator written in C

//Coded by dBx @ h4ckinab0x

#include <stdio.h>

#include <conio.h>



int main()

{


      printf("On Your Marks. Get Set. Calculate!\n\n");

      printf("Press [1] for Addition\n");

      printf("Press [2] for Subtraction\n");

      printf("Press [3] for Multiplication\n");

      printf("Press [4] for Division\n");

      getch();

      

     

      

      if("choice==1") 

      

      {

                      

       int num1;

       int num2;

       int sum;

       

      printf("Enter Your Number:\n");

    

    scanf("%d", &num1);

    scanf("%d", &num2);

    

    sum = num1 + num2;

    

    printf("The Answer is %d!", sum); 

    

}


     else if("choice==2")

    

    {

     

       int num1;

       int num2;

       int sub;


     printf("Enter Your Number:\n");

    

    scanf("%d", &num1);

    scanf("%d", &num2); 

    

    sub = num1 - num2;

    

    printf("The Answer is %d!", sub); 

    

}

   

      else if("choice==3")

    

    {

      

       int num1;

       int num2;

       int multi;


   printf("Enter Your Number:\n");

    

    scanf("%d", &num1);

    scanf("%d", &num2); 

    

    multi = num1 * num2;

    

    printf("The Answer is %d!", multi); 

    

}

    

  else if("choice==4")

    

    {

    

       int num1;

       int num2;

       int dvs;


   printf("Enter Your Number:\n");

    

    scanf("%d", &num1);

    scanf("%d", &num2); 

    

    dvs = num1 / num2;

    

    printf("The Answer is %d!", dvs); }

    getch();

}


What am i doing wrong...when i compile it,
the exe shows up. but the functions dont work.
i think i have to set them as for example;
sub = subtraction;
etc.
also i think i should use switch/case statements.
or do while loops..
but im having trouble with it ]:

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You can't put your if conditions in quotes.
Try this:
//Calculator written in C
//Coded by dBx @ h4ckinab0x
#include <stdio.h>
#include <conio.h>


int main()
{

      printf("On Your Marks. Get Set. Calculate!\n\n");
      printf("Press [1] for Addition\n");
      printf("Press [2] for Subtraction\n");
      printf("Press [3] for Multiplication\n");
      printf("Press [4] for Division\n");
      getch();
      
     
      
      if(choice==1) 
      
      {
                      
       int num1;
       int num2;
       int sum;
       
      printf("Enter Your Number:\n");
    
    scanf("%d", &num1);
    scanf("%d", &num2);
    
    sum = num1 + num2;
    
    printf("The Answer is %d!", sum); 
    
}

     else if(choice==2)
    
    {
     
       int num1;
       int num2;
       int sub;

     printf("Enter Your Number:\n");
    
    scanf("%d", &num1);
    scanf("%d", &num2); 
    
    sub = num1 - num2;
    
    printf("The Answer is %d!", sub); 
    
}
   
      else if(choice==3)
    
    {
      
       int num1;
       int num2;
       int multi;

   printf("Enter Your Number:\n");
    
    scanf("%d", &num1);
    scanf("%d", &num2); 
    
    multi = num1 * num2;
    
    printf("The Answer is %d!", multi); 
    
}
    
  else if(choice==4)
    
    {
    
       int num1;
       int num2;
       int dvs;

   printf("Enter Your Number:\n");
    
    scanf("%d", &num1);
    scanf("%d", &num2); 
    
    dvs = num1 / num2;
    
    printf("The Answer is %d!", dvs); }
    getch();
}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dBx

dBx

    Newbie

  • Members
  • Pip
  • 5 posts
i have tried that already. I got this error: 19 C:\Dev-Cpp\Calculator.c `choice' undeclared (first use in this function)

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
You never declared "choice"
cin >> choice;


#5
dBx

dBx

    Newbie

  • Members
  • Pip
  • 5 posts
11 C:\Dev-Cpp\Calculator.c `cin' undeclared (first use in this function)
^ lol.

#6
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Im pretty sure for i/o you need to use one of these:
#include <iostream.h>
#include <iomanip.h>


#7
dBx

dBx

    Newbie

  • Members
  • Pip
  • 5 posts
there are no such directories/files. that is for C++ not C i think.

#8
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
So why not use something like:
scanf("%d", &choice);


#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You need to declare choice an int, and scanf("%d",choice);
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
dBx

dBx

    Newbie

  • Members
  • Pip
  • 5 posts
Perfect. I got it working. Thank you for your help guys.
Much appreciated.