7 Replies - 463 Views - Last Post: 23 November 2011 - 09:18 PM Rate Topic: -----

#1 Fala  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;

//=========== 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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: No idea how to make loop

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8957
  • View blog
  • Posts: 33,581
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 825
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 Fala  Icon User is offline

  • New D.I.C Head

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

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"?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8957
  • View blog
  • Posts: 33,581
  • 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!
Was This Post Helpful? 0
  • +
  • -

#7 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 280
  • View blog
  • Posts: 1,787
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1