Hi, i'm second year in college and i'm diving into the basics of c++ , basicly i have to read 2 numbers and divide them with each other by repeated substractions using a function, the function is to divide those numbers by repeatedly substracting one from another, and return the result, and the rest, + give an error in case one of the numbers is 0 :D
i tried to play with that a bit but i think i pretty much failed :(
here is what i got for now:

#include "stdafx.h"
#include <conio.h>
#include <iostream>

using namespace std;


int div (int x, int y)
{
int c=0,d=0;
while (x!=0,y!=0,x>=y)
{
c=x-y;
x=c;
d=d+1;
}
return d;
}

int main()
{
int a,b,c=0,d=0,z=0,n;
cout<<"A= ";
cin>>a;
cout<<" B= ";
cin>>b;
z = div (a,b);

if (a==0 || b==0)
{
cout<<" ERROR "<<endl;
}
if (a!=b,a<=b)
{
cout<<" "<<endl;
cout<<"A / B = "<<z<<endl;
n=a%b;
cout<<"Rest= "<<n<<endl;
}
getch();
}

i get 4 errors but it would be pointless to post them since i know its probably something obvious >.<

Posting the errors you get is never pointless; People reading your post aren't psychic, so we have no idea what errors you're seeing; and they might try to compile it under a different compiler to you, which means they may get different errors (Especially if you use non-standard libaries like <conio.h>)

Edited 6 Years Ago by Bench: n/a

I'd check for denominator equals zero and if not, then call the function passing a, b, result and remainder to the funciton with result and remainder being passed by reference. When the function is done, then print out the numerator, denominator, result and remainder.

void div(int temp, const int b, int & result, int & remainder)
{
     //default values of result and remainder---just like you did
     remainder = 0
     result = 0

     //do serial subtract of b from temp---division.
     while temp greater than b
         subtract b from temp and reassign difference back to temp
         increment result

      //when loop over 
      temp is remainder
}

This is wrong :

while (x!=0,y!=0,x>=y)

Use either the logical AND operator( "&&" ) or the logical OR operator( "||")

Same here :

if (a!=b,a<=b)

.

thanks a lot guys, i made it work, it seems the name i gave my function "div" was not very well thought of, seems there already is something with that name and it fucked up my shizzle :D also the operator "&&" yeah i forgot about that one. loads of thanks mates :D

This article has been dead for over six months. Start a new discussion instead.