Sunday, July 12, 2009

Ok still have a problem with this C++ program?

// This program by Dillon Nicholson gets numbers by the user


// then counts the number of digits in the number either being both


// positive or negitive whole numbers





#include %26lt;fstream%26gt;


#include %26lt;iostream%26gt;


using namespace std;





void countingFunction(int);





int main()


{


ofstream out;


out.open("output.txt");





int number, num;





out%26lt;%26lt;"This is the program of : ";


out%26lt;%26lt;"Dillon Nicholson"%26lt;%26lt;"\n";


out%26lt;%26lt;"CMPS 1043-02"%26lt;%26lt;"\n";


out%26lt;%26lt;"Due date:";


out%26lt;%26lt;" Nov 15 2007"%26lt;%26lt;"\n"%26lt;%26lt;"\n";


cin%26gt;%26gt;number;


cin%26gt;%26gt;num;


countingFunction(number);


out%26lt;%26lt;num;


out.close();


return 0;


}





void countingFunction(int num, int count)





{


while (num != 0)


if (num %26lt; 0) num*=-1;


else if (num %26gt;= 10)


count = 1;


count++;


num= num / 10;


}








its got an error, im sorry for reposting but i made alot of changes, but its spose to allow me to imput a number then count the digits in it, please ne advice or correction of code would be great

Ok still have a problem with this C++ program?
remove line 24: cin%26gt;%26gt;num





why would you capture an input to the result?





line 9 should be:





void countingFunction(int, int%26amp;);





line 25 should be:





countingFunction(number, num);





line 31 should be;





void countingFunction(int num, int %26amp;count)





you need to pass by reference or the count will not pass back.
Reply:modify main() to:





int number=0;


cin%26gt;%26gt;number;


int count=1;


if (number %26lt; 0) number *= -1;


while ((number/=10) != 0) count++;


cout%26lt;%26lt;count;


return 0;





Done :).





Update: That should be a cinch:


int countingFunction(int number)


{


int count=1;


if (number %26lt; 0) number *= -1;


while ((number/=10) != 0) count++;


return count;


}


and call countingFunction(number) from your main().


No comments:

Post a Comment