Like Tree1Likes
  • 1 Post By anduril462

Loop a calculator program.

This is a discussion on Loop a calculator program. within the C Programming forums, part of the General Programming Boards category; So I found a program on the internet, I made a few changes to it here and there but now ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    5

    Loop a calculator program.

    So I found a program on the internet, I made a few changes to it here and there but now I want to loop it....


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void main()
    {
        int a,b,res,ch;
        printf("\t   *********************");
        printf("\n\tMENU\n");
        printf("\t********************");
        printf("\n\t(1)ADDITION");
        printf("\n\t(2)SUBTRACTION");
        printf("\n\t(3)MULTIPLICATION");
        printf("\n\t(4)DIVISION");
        printf("\n\t(5)REMAINDER");
        printf("\n\t(0)EXIT");
        printf("\n\t********************");
        printf("\n\n\tEnter your choice:");
        scanf("%d",&ch);
        if(ch<=5 & ch>0)
        {
            printf("Enter two numbers:\n");
            scanf("%d%d",&a,&b);
        }
        switch(ch)
        {
            case 1:
            res=a+b;
            printf("\n Addition:%d",res);
            break;
            case 2:
            res=a-b;
            printf("\n Subtraction:%d",res);
            break;
            case 3:
            res=a*b;
            printf("\n Multiplication:%d",res);
            break;
            case 4:
            res=a/b;
            printf("\n Division:%d",res);
            break;
            case 5:
            res=a%b;
            printf("\n Remainder:%d",res);
            break;
            case 0:
            printf("\n Choice Terminated");
            exit(1);
            break;
            default:
            printf("\n Invalid Choice");
        }
    }



    Any one has any idea how it can be done?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,559
    Find a program on the internet that does it . Just find it from a better source, one that at least knows the correct form of main (FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com). Seriously though, find some tutorials (we have one on our site for starters) and start reading them. Work through the examples. Learn about loops and try it yourself. Post your effort here, and we'll help, but we sure wont just write it for you. Especially since this sounds like a homework assignment (maybe it isn't in this case, but you did pick perhaps the most common programming assignment of all time).

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    5
    Quote Originally Posted by anduril462 View Post
    Find a program on the internet that does it . Just find it from a better source, one that at least knows the correct form of main (FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com). Seriously though, find some tutorials (we have one on our site for starters) and start reading them. Work through the examples. Learn about loops and try it yourself. Post your effort here, and we'll help, but we sure wont just write it for you. Especially since this sounds like a homework assignment (maybe it isn't in this case, but you did pick perhaps the most common programming assignment of all time).


    But I want to know which loop I should be using....
    If I know that then I will do the rest of the stuff

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    1
    use do while loop

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,559
    A whole 5 minutes between my reply and yours. You didn't even bother looking for a tutorial on looping in C to go with code you didn't even bother writing yourself. It sounds like you want to put in as little effort as possible. We are here to help you learn, not hand out answers. Read the loop tutorial on this site, it's a few short paragraphs, and some useful examples. Try all 3 loop types, see if each is possible and which one(s) work better. Each one only involves a few simple lines of code. If you can't figure it out on your own, then show us you tried and we will help. Note, figure it out means you study and try it yourself, and ask us for help, not wholesale answers.
    Salem likes this.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    5
    Thanks for the help kevinpaladin.







    Quote Originally Posted by anduril462 View Post
    A whole 5 minutes between my reply and yours. You didn't even bother looking for a tutorial on looping in C to go with code you didn't even bother writing yourself. It sounds like you want to put in as little effort as possible. We are here to help you learn, not hand out answers. Read the loop tutorial on this site, it's a few short paragraphs, and some useful examples. Try all 3 loop types, see if each is possible and which one(s) work better. Each one only involves a few simple lines of code. If you can't figure it out on your own, then show us you tried and we will help. Note, figure it out means you study and try it yourself, and ask us for help, not wholesale answers.



    [edited by mod]
    If you have nothing nice to say, say nothing...
    Last edited by lulzintosh; 06-05-2012 at 12:52 AM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Spoonfeeding the answer like that leaves the poster having learnt next to nothing about why do..while is best here and when to use other loop types.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,677
    Learn to use functions.
    Put at least the menu display into a function.
    Reason: It makes the code easier to read (and maintain) and it is good to learn to use/write functions in C at an early point.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator loop problem
    By Laika1986 in forum C Programming
    Replies: 5
    Last Post: 10-22-2011, 12:46 PM
  2. X^Y Calculator Using a For Loop
    By b0rn2fl2 in forum C Programming
    Replies: 23
    Last Post: 10-18-2011, 11:42 AM
  3. while loop (sinple calculator)
    By aweida in forum C Programming
    Replies: 3
    Last Post: 09-27-2010, 12:40 PM
  4. Calculator program
    By treenef in forum C++ Programming
    Replies: 5
    Last Post: 11-23-2005, 02:10 AM
  5. Help with calculator program.
    By banjordan in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2003, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21