Do you see something wrong here?

PLease can you tell me what is wrong here?
Thanks

#include <iostream>

using namespace std;

void main()
{
int number, count, total;
total = 0;
count = 0;

cout << "Enter a number less than 10" << endl;
cin >> number;

while(count < number )
{
total = total + number;
count++;
}
//end while

system("pause");
}
Last edited on
1. main() cannot return void

2. it's poor style creating variables before they are needed

3. endl is redundant before cin >>

4. the code after cin >> number and before system("pause") does not affect the program output and will be eliminated by the compiler

5. the system command "pause" does not exist on many platforms.
Last edited on
Could you please tell me why (your 4th statement) will be eliminated by the compiler?
Thanks in advance
Hi Scout,

I don't think it will but I can be wrong.

put a cout << "test" << number << endl;
in your while loop and you will find out

watch while loops they have to be implemented very well or your will end up in a endless loop it is better to use a for loop in those cases.
I think you are making table of entered number
so just print the number in the while loop...
Please drop
 
system("PAUSE");


replace it with
 
return 1;
Agree with gurukrupa, you forgot to print out your result.

What I feel about your program is that:

I was hungry.
I bought my food, but I did not eat it.
I found myself still hungry.

Apply to your code:
You are trying out while loop.
You coded the while loop, but I did not print out the numbers that shows while loop is working.
You thought that while loop was wrong.

=p

Just joking.
closed account (zb0S216C)
scout51 wrote:
"Could you please tell me why (your 4th statement) will be eliminated by the compiler?"

Because total and count do not affect the program (no noticeable side-effects). Therefore, the compiler will see it as a waste of processing time, and will optimise it away. However, if you used total and count in a way that affects the program (such as opening a file, writing to the output buffer, or using it to store input), the compiler cannot ignore that and the code will remain (optimising away the code would break the program).

Wazzak
Last edited on
Too many lines of code that to calculate number * number. And moreover even this simple operation the program does incorrectly because if the number will be enetered as negative the sum will be equal to zero.:)
The loop has no function. It does not output the numbers or the result of the calculation.

Secondly, what is the program supposed to do?

total = total + number will give you a multiple of whatever number is. If number is 5 total will be (counter:total) 0:5 1:10 2:15 3:20 4:25. The value of number does not change in the loop.

If you are trying to total up the values untill you get to number for instance 1+2+3+4....number then it should be total = total + counter.

to see the effect put cout >> total in the loop after the calculation.
@Wolman
to see the effect put cout >> total in the loop after the calculation.


This is only if you mean that the effect is some sort of compilation error.:)




Last edited on
it's poor style creating variables before they are needed


i don't understand, i don't see variables are created before they are needed... what i saw, is the variable creation is done exactly when they needed...
closed account (zb0S216C)
@chipp: The declaration of both total & count could easily be postponed until they are actually referenced.

Wazzak
so it should be:
1
2
3
4
5
6
int number = 0;

cout << "Enter a number less than 10" << endl;
cin >> number;

int total = 0, count = 0;


like that?
closed account (zb0S216C)
More or less, but number could be declared after the std::cout statement.

Wazzak
It would be much better to write

for ( int i = 0; i < number; i++ ) total += number;
instead of

1
2
3
4
5
while(count < number )
 {
 total = total + number;
 count++;
 }
Topic archived. No new replies allowed.