Why doesnt this output what I want?

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

double receiptmake(string item1, double priceitem1, string item2, double priceitem2, string item3, double priceitem3, string item4, double priceitem4, string item5, double priceitem5) 
{
	double item1length= item1.length();
	double item2length= item2.length();
	double item3length= item3.length();
	double item4length= item4.length();
	double item5length= item5.length();
	int looptimes;
	
	std::left;
	cout << setw(20) << item1;
	looptimes=20-item1length;
	for(looptimes; looptimes=0; -looptimes){
		cout << ".";
	}
	cout << endl;
	cout << looptimes;
	return 0;
}

int main()
{
	receiptmake("Example",5,"a",5,"a",3,"a",4,"a",5);
}


the bold is where my question is. on this for loop, I want it to make the number of dots needed to get to 20 spaces. aka, if the string is 8 characters, there will be 12 dots. But the output doesnt make any dots. Why not?

output:
             Example
0
closed account (zb0S216C)
Get rid of setw( 20 ) on line 17.
First of all, this makes no sense:
for(looptimes; looptimes=0; -looptimes)
What are you trying to do here?

Also why create variables you don't need? item5length is no more clear than item5.length();
Last edited on
first of all, getting rid of setw(20) would make the not do what i want.

next, the looptimes is an integer, lets say 12. that subtracts one each loop until its 0. so it *should* output 12 dots. But it doesnt. Why?
Because you didn't write it to do that.

 
for(;looptimes>0;--looptimes) //this is what you want. 
closed account (zb0S216C)
would make the not do what i want.

I'm sorry?

that subtracts one each loop

Subtracted by? Your code is: for(looptimes; looptimes=0; -looptimes)

Are you sure it's doing what you want? Is this what you want your output to look like:
.............Example


@hanst99: Actually, more proper would be to combine lines 14,18, and 19 into this:

for (int looptimes = 20-item1.length(); looptimes > 0; --looptimes)
and a for loop with an increasing counter would make more sense.
Last edited on
Topic archived. No new replies allowed.