TROUBLE with for loops

ok so my program works perfectly but when I try to add a for loop so it turns 2500 times the program starts takes the value to be taken by user but it never ends!!"press any button..." never shows up whyyy??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include"firstquestion.h"

using namespace std;

void main()
{
	FirstQuestion first;
	float expectedvalue;
	vector<float>dataholder;
	int z,l;
	cin>>z;
	cin>>l;
		//for(l=0;l<2500;l++){
	expectedvalue=first.process(l,z);
	dataholder.push_back(expectedvalue);//}
	cout<<dataholder.at(0);
	system("pause");
}
If you are using l for looping and intializing it to zero then you dont need cin>>l;

Are you modifying value of l inside "process" function?
l is always same throughout the process. ı'm not taking l if I use for loop I put it there just to show the version that works.
Since you haven't posted your full code I can only guess, but cout puts anything want to output into a buffer and you have to flush that buffer in order to have it print something out. I don't think it will flush when you call system("pause");

You can flush it by using endl like this
cout << dataholder.at(0) << endl;

Or if you don't want a newline, put cout.flush() after that statement.

If that doesn't fix it, post more of your code.
@Gulshan Singh I dont think that the flow is even reaching that statement and that is the problem dcftci is trying to solve here.

@dcftci, there is nothing wrong in the loop per say, thats why I was doubting first.process(l,z); (except the return type of main should always be int and type of z, l should be float rather than int as process accepts/at least returns float)

I put the process function (which is a member of my class now) into main to see if there is something wrong with my codings but it worked perfectly turned the right value and still while a class member it turns the right value and it works if I dont ask it to do it for 2500 times

so thats why I thought the problem might be the for loop

I also defined z,l as float but nothing changed the program started but didn't end :( the black rectangle is still there :((
@codewalker I've had this similar problem before, and I also thought that it was because my code wasn't reaching the statement. But the actual problem was that the buffer wasn't being flushed. It's actually a really common problem for beginners. See http://www.cplusplus.com/reference/iostream/ostream/flush/

@dcftci Did you try my suggestion?
@Gulshan Singh thanks for the suggestion but I put "cout<<dataholder.at(0);"to check whether the vector is formed or not. my actual intention is to transport the data in the vector into a file but once I start the program using for loop it never stops
I tried your code that you posted it works with or without the loop.
Except instead of your class I replaced the class member process with a global process function, and other simple modifications such as signature of main.

Here is the code

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
#include <iostream>
#include <vector>

using namespace std;

float process( float l, float z )
{
  return l+z;
}

int main()
{
  //FirstQuestion first;                                                                                                                                                                                        
  float expectedvalue;
  vector<float>dataholder;
  float z,l;
  cin>>z;

  for(l=0;l<2500;l++){
  expectedvalue=process(l,z);
  dataholder.push_back(expectedvalue); }


  cout << endl << dataholder.at(0);
  cout << endl << dataholder.at(2499);
}


Here is the output
$ ./generic
35.789

35.789
2534.79


So I still suspect your process function is doing something to "l"
thank you for helping I appreciate a lot finally it worked :)))
Topic archived. No new replies allowed.