0
#include <iostream>

using namespace std;

int main()
{
    double donation[10];
    int index = 0;

    cout.setf(ios::fixed);

    cout << "Enter sum of money for donating: ";

    while (index < 10 && cin >> donation[index])
    {
        cout << "donation #" << 1 + index++ << ": " << donation[index] << endl;
    }

    return 0;
}

Result

That code couldn't display right value of donation...

I could check the mistake is '1 + index++', but I don't know why did.

Why my code using '1 + index++' has a difference with code when I use 'index++' in the next line.

ChoRyeon
  • 13
  • 2
  • What is the purpose of `1 + index++` in this piece? Why aren't you simply using `1 + index`? The way your code stands now, `index` gets incremented twice in each iteration, which is one of the problems. My advice would be to only use ++ as separate statements, unless you are completely sure what you are doing. – Michał May 05 '17 at 08:04
  • I'm sorry. My mistake. I modified the code. The question is if there is no 'index++' in the next line. – ChoRyeon May 05 '17 at 08:07
  • What do you expect to be displayed? Your code seems to be running fine to me - it asks for a donation and then immediately displays it – Michał May 05 '17 at 08:11
  • When I run that code, it display a strange value that is different from the input value like the picture I added. – ChoRyeon May 05 '17 at 08:15
  • please make sure that the code you post here really produces the same problem. For sure it isnt the same code that you used to produce the output in the image! Maybe there are more subtle difference that you missed – 463035818_is_not_an_ai May 05 '17 at 08:19
  • Your code runs fine on my machine. Can you provide your OS, and system lanugage? This is a wild guess, and it is probably completely off, but maybe your terminal accepts numbers separated with a comma instead of with a point? What happens if you type in simple integers instead of floating point numbers? Also, are you saying it only happens when you use 1 + index++, instead of index++ on the next line? – Michał May 05 '17 at 08:22
  • OS is Window10 and system language is Korean. Terminal can accept numbers separated with a comma instead of wit a point. When I type in simple integers, the result is same. And the result only happens when i use 1 + index++. – ChoRyeon May 05 '17 at 08:29

1 Answers1

0

Keep to the idiomatic way instead of trying to understand stranger constructions and also mixing increments with function/operator parameters should be avoided (see sequence points):

for (int index = 0; index < 10; ++index)
{
   if (! std::cin >> donation[index])
        break;
   std::cout << "donation #" << (1 + index) << ": " << donation[index] << std::endl;
}

This should do what is expected and should be understood with some knowledge of C++. It runs at most 10 times, tries to fill input to the array and displays when the input works or stops when the input fails. The only issue would be better validation of the user input.

Community
  • 1
  • 1
stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • Thank you for answering. But I want to know why my code using '1 + index++' has a difference with code when I use 'index++' in the next line. – ChoRyeon May 05 '17 at 08:38
  • Have a look at the sequence point link in the answer or http://stackoverflow.com/questions/3986361/behavior-of-post-increment-in-cout to see what using increments in other constructs do and why it can lead to unexpected results. You arre changing index and reading it in the std::cout statement. – stefaanv May 05 '17 at 08:59
  • But still, separating the loop and the input handling makes the code clearer and makes actual input validation easier. – stefaanv May 05 '17 at 09:04
  • Oh, thank you for giving me the answer I want. The link you gave me helped. It was wrong that I did not understand the side effects and sequence points well. Thanks to your answers, I understood the concept better. – ChoRyeon May 05 '17 at 09:12
  • Great that the link helped. I wanted to put focus on avoiding these things, but indeed it helps to understand what to avoid and why. – stefaanv May 05 '17 at 09:15