# No idea how to make loop

Page 1 of 1

## 7 Replies - 873 Views - Last Post: 23 November 2011 - 09:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=256925&amp;s=6b4ea11b751f5c595ea7e5b3fc1d6c4b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Fala

Reputation: 0
• Posts: 2
• Joined: 23-November 11

# 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.
```#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;

printf("\t\t\t\t #FRACTION CALCULATOR#\n\n");
printf("Choose an option: \n\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)
{
}
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;
}

```

Is This A Good Question/Topic? 0

## Replies To: No idea how to make loop

### #2 modi123_1

• Suitor #2

Reputation: 13953
• Posts: 55,694
• Joined: 12-June 08

## 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 Ntwiles

Reputation: 148
• Posts: 831
• Joined: 26-May 10

## Re: No idea how to make loop

Posted 23 November 2011 - 10:46 AM

You want to use a while loop. The syntax is:

```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 Fala

Reputation: 0
• Posts: 2
• Joined: 23-November 11

## Re: No idea how to make loop

Posted 23 November 2011 - 11:09 AM

### #5 jimblumberg

Reputation: 5444
• Posts: 16,968
• Joined: 25-December 09

## 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

### #6 modi123_1

• Suitor #2

Reputation: 13953
• Posts: 55,694
• Joined: 12-June 08

## Re: No idea how to make loop

Posted 23 November 2011 - 11:21 AM

... put the start of your loop right before that!

### #7 J-e-L-L-o

Reputation: 23
• Posts: 204
• Joined: 23-January 11

## 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

### #8 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: No idea how to make loop

Posted 23 November 2011 - 09:18 PM

You may like to see these ...

http://developers-he...index.php/topic,2022.0.html