#include <stdio.h> #include <stdlib.h> #include <string.h> //======================= FUNCTIONS ========================= void add (int a,int b,int c,int d) { int nom; nom=a*d + b*c; int denom; denom=b*d; if (denom==0) printf("WE CAN'T DIVIDE BY ZERO!\n\n"); else printf("The result is = %d/%d\n\n\n",nom,denom); } void sub (int a,int b,int c,int d) { int nom; nom=a*d - c*b; int denom; denom=b*d; if (denom==0) printf("WE CAN'T DIVIDE BY ZERO!\n\n"); else printf("The result is = %d/%d\n\n\n",nom,denom); } void mul (int a,int b,int c,int d) { int nom; nom=a*c; int denom; denom=b*d; if (denom==0) printf("WE CAN'T DIVIDE BY ZERO!\n\n"); else printf("The result is = %d/%d\n\n\n",nom,denom); } void dive (int a,int b,int c,int d) { int nom; nom=a*d; int denom; denom=b*c; if (denom==0) printf("WE CAN'T DIVIDE BY ZERO!\n\n"); else printf("The result is = %d/%d\n\n\n",nom,denom); } //================== MAIN FUNCTION ================== int main(int argc, char** argv) { int a,b,c,d; int nom,denom; int n,o; char N,Y; int i,j; //=========== READ FRACTION ================== printf("\t\t\t\t #FRACTION CALCULATOR#\n\n"); printf("Choose an option: \n\n"); printf("Enter 1 for ADDITION\n"); printf("Enter 2 for SUBSTRACTION\n"); printf("Enter 3 for MULTIPLICATION\n"); printf("Enter 4 for DIVISION\n\n"); scanf("%d" ,&n); //=========== ENTER FRACTION ================== printf("\t\t\tFirst Fraction:\n\n"); printf("Enter the first number: "); scanf("%d",&a); printf("Enter the second number: "); scanf("%d",&B)/>; printf("\n"); printf("\t\t\tSecond Fraction: \n\n"); printf("Enter the first number: "); scanf("%d",&c); printf("Enter the second number: "); scanf("%d",&d); printf("\n\n"); //=========== CALLING FUNCTIONS =============== if (n==1) { add(a,b,c,d); } else if (n==2) { sub(a,b,c,d); } else if (n==3) { mul(a,b,c,d); } else if (n==4) { dive(a,b,c,d); } //============ NO IDEA HOW TO MAKE LOOP TO CONTINUE CALCULATING======== printf("To continue calculating press Y or to end press N\n\n"); printf("\t\t\t\t\t"); scanf("%c",&Y,&N); system("PAUSE"); return 0; }
No idea how to make loop
Page 1 of 17 Replies - 873 Views - Last Post: 23 November 2011 - 09:18 PM
#1
No idea how to make loop
Posted 23 November 2011 - 10:39 AM
Hi,i've made a fraction calculator,but the problem is that i need to make it continuous,so after a first calculating it shouldn't close,but get back to step when i choose an option.No idea how to start a loop.
Replies To: No idea how to make loop
#2
Re: No idea how to make loop
Posted 23 November 2011 - 10:43 AM
I would think wrapping line 47 & down in a while statement where the condition is if the user's input was 'y' or not seems logical.
#3
Re: No idea how to make loop
Posted 23 November 2011 - 10:46 AM
You want to use a while loop. The syntax is:
where x should evaluate to either true or false. As long as x is true, it will continue looping and do y repeatedly. After each calculation, you'll want to ask the user if he wants to do another one. If so, it should continue looping, if not, it should break out of the loop and end the program.
while(x) { y; }
where x should evaluate to either true or false. As long as x is true, it will continue looping and do y repeatedly. After each calculation, you'll want to ask the user if he wants to do another one. If so, it should continue looping, if not, it should break out of the loop and end the program.
This post has been edited by Ntwiles: 23 November 2011 - 10:48 AM
#4
Re: No idea how to make loop
Posted 23 November 2011 - 11:09 AM
Ok i will try.Other question,how do i return to the moment when i "Read Fraction"?
#5
Re: No idea how to make loop
Posted 23 November 2011 - 11:13 AM
You can start by studying this link: C++ Control Structures, and then use a while{}, for{}, or do/while{} loop.
Jim
Jim
#6
Re: No idea how to make loop
Posted 23 November 2011 - 11:21 AM
... put the start of your loop right before that!
#7
Re: No idea how to make loop
Posted 23 November 2011 - 08:44 PM
I would use a do/while loop. Since you want your program to always do something, and exit when you are done, the do/while is best. (it guarantees at least one instance of your loop is always executed).
here is an example of a simple program I wrote:
do/while example of mine
here is an example of a simple program I wrote:
do/while example of mine
#8
Re: No idea how to make loop
Posted 23 November 2011 - 09:18 PM
Page 1 of 1