-10

Can someone tell me why this programm gives only the cout text?

#include "iostream"
#include "stdafx.h"
#include <iostream>

using namespace std;
int main()
{
    float x;
    int y[50];
    int n;
    cout << "Dati notele despartindule prin Enter";
    for ( n = 0; n == 5; n++) {
        cin >> y[n];
        if (n >= 1) {
            y[n] = y[n - 1] + y[n];
        }
    }
    x = y[n] / (n + 1);
    cout << x;
    return 0;
}
UKMonkey
  • 6,941
  • 3
  • 21
  • 30
  • 6
    Do you know how `for` loops work? `n` gets assigned `0`, and then it is compared to `5`, since the result of such comparison is `false` (`0` is **not** equal to `5`) - the loop is not entered. Consider reading a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Nov 29 '17 at 15:59
  • 3
    Yes, your debugger can tell you. *Fix-my-code* questions are off-topic here. BTW `"stdafx.h"` is not a standard C++ header (look on some [C++ reference](http://en.cppreference.com/w/) site). Next time, compile with all warnings and debug info (e.g. `g++ -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)...), improve the code to get no warnings, and **use the debugger** (e.g. `gdb`). For your next question on SO, please provide some [MCVE] – Basile Starynkevitch Nov 29 '17 at 15:59
  • 2
    You are just getting a preview of what your program will look like when you start it from a shortcut on your desktop. Set a breakpoint at the end of main() so you can read the console before it closes. – Hans Passant Nov 29 '17 at 16:02

2 Answers2

2
for ( n = 0; n == 5; n++)

Is not what you want. A for-loop is nothing but a while-loop using different syntax:

n = 0;

while(n == 5)
{
   // you loop body here
    n++;
}

As you can see, it executes while your condition is true. In this case... not at all because it's not true to begin with.

You probably meant

for ( n = 0; n < 5; n++)
nvoigt
  • 75,013
  • 26
  • 93
  • 142
1

n == 5 is the problem. When came to for function and makes the test to see if the block needs to run it get false because you initialize with n = 0; better than n == 5 use n!=5 but if you skip to make n = 5 you get an infinite loop.

For the best case use n < 5.

Stoica Mircea
  • 782
  • 10
  • 22