[update:post: 10] displaying 0s, leading zeros?

EDIT: I realized that if I type in 1000 the result would be 1,0 because of the modulus divsion.

So do I convert the numbers into strings in order to solve this problem? but I havn't learned about string manipulations yet so...is there an alternative method?



My code got the error of having too few arguments to function. what does that mean exactly? I tried google but they had different situations and I don't think it applys to mine.

I also realize my code is not as concise or "smart" as it should, it just that I can't think of an alternative method to write this code.

#include <cstdlib>
#include <iostream>

using namespace std;

//----------------------------------------

void DisplayWithCommas(long number)
/*  displaying any long integers with proper commas */

{
    int number1, number2, number3, number4, number5;
    
    
    number1 = number % 1000;
    number = number / 1000;
    number2 = number % 1000;
    number = number / 1000;
    number3 = number % 1000;
    number = number / 1000;
    number4 = number % 1000;
    number = number / 1000;
    number5 = number % 1000;
    
    if (number5 != 0)
    cout << number5 << "," << number4 << "," << number3 << "," << number2 << "," << number1 << endl;
    else if (number4 != 0)
    cout <<  number4 << "," << number3 << "," << number2 << "," << number1 << endl;
    else if (number3 != 0)
    cout << number3 << "," << number2 << "," << number1 << endl;
    else if (number2 != 0)
    cout << number2 << "," << number1 << endl;
    else
    cout << number1;
}

//--------------------------------------------------
        


int main(int argc, char *argv[])
{
    DisplayWithCommas();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Last edited on
It means just what is says. You're giving too few arguments when calling a function.

DisplayWithCommas takes a number as a parameter. You're not giving it a number when you call it.
so I set it as follows:

long number;
cin >> number;
DisplayWithCommas(number);


but now I realized that if I type in 1000 the result would be 1,0 because of the modulus divsion.

So do I convert the numbers into strings in order to solve this problem? but I havn't learned about string manipulations yet so...is there an alternative method?
anyone?
in ur main function..u're calling function DisplayWithCommas..
this is what u call..

DisplayWithCommas();

the function that u're declare is

void DisplayWithCommas(long number)..

in the () u have a long number as function parameter..

so when u're calling the function..u should put a long argument there..

for example..

DisplayWithCommas(111111111);
Yea, I solved that part of the problem - few argument. thx anyway

however now I realized that if I type in 1000 the result would be 1,0 because of the modulus divsion.

So do I convert the numbers into strings in order to solve this problem? but I havn't learned about string manipulations yet so...is there an alternative method?
Last edited on
may i know..
what u want to do actually..
he probably want to
 
/*  displaying any long integers with proper commas */


;-).

Something like that, only human readable :-D

1
2
3
4
5
6
7
void DisplayWithCommas(long number)
{
	string s;
	for (long i = number%10, c = 1; number; number /= 10, i = number%10, c = (c+1)%3)
		s = (c==0 && number>9 ?",":"") + ((char)(i+48) + s);
	cout << s;
}


Paulliwali: If you want to understand my code above, go ahead and ask which part I should explain. Maybe it's a good excercise. Else, just ignore it. I wouldn't want someone who writes cryptic stuff like this all the day in my team. :-D

(And for the real exercise, make it work with negative numbers :-D )

Ciao, Imi.
Last edited on
but now I realized that if I type in 1000 the result would be 1,0 because of the modulus divsion.

It's because "cout << numberX" doesn't fill up with leading zeroes. IIRC, "cout << setw(3) << setfill('0') << numberX" should do the trick.

Ciao, Imi.
Last edited on
whoa thx imi for those codes, I will try to make sense of it but I doubt my teacher would be pleased if I used that =P

but using "cout << setw(3) << setfill('0') << numberX"



if (number5 != 0)
    cout << setw(3) << setfill('0') << number5 << "," << number4 << "," << number3 << "," << number2 << "," << number1 << endl;
    else if (number4 != 0)
    cout << setw(3) << setfill('0')  <<  number4 << "," << number3 << "," << number2 << "," << number1 << endl;
    else if (number3 != 0)
    cout << setw(3) << setfill('0')  << number3 << "," << number2 << "," << number1 << endl;
    else if (number2 != 0)
    cout << setw(3) << setfill('0')  << number2 << "," << number1 << endl;
    else
    cout << setw(3) << setfill('0')  << number1;



sometimes works like 1235400 would.
but for numbers like 10000 won't it would display as 10,0



[and that negative number sure is a pain, but don't tell me the answer i wan't to figure that part out myself]
setw only last until the next "normal" output. (I am not sure for setfill - try it out, maybe you have to set it only once). So you have to put setw(3) before every number. But you should start only after the first comma with the setw-things or else you would get leading zeroes (1200 will become "001,200")


Ciao, Imi.

PS: Code tags are [code] - you get nice syntax higlighting with them. It's the "<>" button on the left. ;)
Last edited on
thx imi for the help, finally got it to work


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

//----------------------------------------

void DisplayWithCommas(long number)
/*  displaying any long integers with proper commas */

{
    int number1, number2, number3, number4, number5;
    
    
    if (number > 0)
    {
               number1 = number % 1000;
               number = number / 1000;
               number2 = number % 1000;
               number = number / 1000;
               number3 = number % 1000;
               number = number / 1000;
               number4 = number % 1000;
               number = number / 1000;
               number5 = number % 1000;
    
               setfill ('0');
               if (number5 != 0)
               cout << number5 << "," << setw(3) << number4 << "," << setw(3) << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
               else if (number4 != 0)
               cout << number4 << "," << setw(3) << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
               else if (number3 != 0)
               cout << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
               else if (number2 != 0)
               cout << number2 << "," << setw(3) << number1 << endl;
               else
               cout << number1 << endl;          
    }
    else
    {
               number1 = -number % 1000;
               number = number / 1000;
               number2 = -number % 1000;
               number = number / 1000;
               number3 = -number % 1000;
               number = number / 1000;
               number4 = -number % 1000;
               number = number / 1000;
               number5 = -number % 1000;
    
               setfill ('0');
               if (number5 != 0)
               cout << "-" << number5 << "," << setw(3) << number4 << "," << setw(3) << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
               else if (number4 != 0)
               cout << "-" << number4 << "," << setw(3) << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
               else if (number3 != 0)
               cout << "-" << number3 << "," << setw(3) << number2 << "," << setw(3) << number1 << endl;
               else if (number2 != 0)
               cout << "-" << number2 << "," << setw(3) << number1 << endl;
               else
               cout << "-" << number1 << endl;  
    }
        
    
}

//--------------------------------------------------
        


int main(int argc, char *argv[])
{
    long number;
    cout << "Please enter a number: ";
    cin >> number;
    DisplayWithCommas(number);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



Topic archived. No new replies allowed.