c++ Newb while loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
power = 3;
check = 2;


	while (power <= 23)
	{

		nämnare = power * check;

		täljare = pow(sinp1, power);

		serie = täljare / nämnare;

		power = power + 2;
		check = check + 2;

	}


so what i'm trying to do is get the while loop to repeat until power hits 23 which it does fine.. but i also want it to do a series of calculations..
so first it runs through the code in the while loop from top then it comes to "serie" at this part i want it to store the value of "serie " somewhere else so on the next loop when it comes to serie it can take the new value of serie + the old value

so for exampel

1
2
3

serie = serie + serieOld - serieOlder + serieOldest


sry for the bad explanation really new to this and not quite sure on how to express my problems :(
Last edited on
If you want to store multiple values, I suggest an array of some kind. if you know the size beforehand, create your array on the stack:
int array[6]; // an array with 6 elements
If you don't know the size, use something dynamic, such as std::vector:
std::vector<int> array;

Further reading
Arrays: http://cplusplus.com/doc/tutorial/arrays/
Vectors: http://cplusplus.com/reference/stl/vector/

PS: You may wish to consider a for loop. It seems to me that it would be more appropriate for your application than a while loop.

-Xander

EDIT:Oh, I may have misunderstood your post. An array would be appropriate if you wanted to store the value from every iteration. If you just wanted to store the last two values then, naturally, you just need two variables.
Last edited on
use pointers to ints on the heap
can be accessed anytime anywhere in the program
This:
so on the next loop when it comes to serie it can take the new value of serie + the old value
....doesn't seem to equate with this:
serie = serie + serieOld - serieOlder + serieOldest
if under 'serie'....you inserted tot_serie+=serie;//would this give you what you are looking for?
Last edited on

If you want to store multiple values, I suggest an array of some kind. if you know the size beforehand, create your array on the stack:
int array[6]; // an array with 6 elements
If you don't know the size, use something dynamic, such as std::vector:
std::vector<int> array;

Further reading
Arrays: http://cplusplus.com/doc/tutorial/arrays/
Vectors: http://cplusplus.com/reference/stl/vector/

PS: You may wish to consider a for loop. It seems to me that it would be more appropriate for your application than a while loop.

-Xander




1
2
3
int store [11];
	store[0] = serie;
	store++;


if i were to use an array it would look something like this now i know the "store++" does not work.. i just thought it would make an good exampel :)

so the loop goes through the code stores the first value of series in store[0] then store++ would add 1 to store[0] so it would become store[1] and then contione and stores the new value of serie in store[1] and so on until it hits 11. would it be possibel with arrays?


This:
so on the next loop when it comes to serie it can take the new value of serie + the old value
....doesn't seem to equate with this:
serie = serie + serieOld - serieOlder + serieOldest
if under 'serie'....you inserted tot_serie+=serie;//would this give you what you are looking for



sorry for the confusion.
 
serie = serie + serieOld - serieOlder + serieOldest
this is what i'm looking to do without having to add a ton of variables since the program is suppost to calculate 11 diffrent values of "serie"

then with those values the program would do a calculation that would look like this

1
2
3

serie + serieOld - serieOlder + serieOldest


so for exampel to put it simply

2+5 = 7 this would be serie
4+4 = 8 this would be serieOld
4+5 = 9 this would be serieOlder
8+2 = 10 this would be serieOldest

so the value of the variables that creates "serie" change everytime the loop restarts and creates a new value for serie which needs to be stored somewhere without overlapping the old value


Last edited on
Yes you can use arrays - that was my first suggestion. Don't increment the original array pointer, since each time you do, you're effectively "losing track" of an array element.

But you can do this:
1
2
3
4
5
6
7
8
9
10
11
12
int array[10];
int iterator = 0;
while (/* blah */)
{
   // blah
   // get array element
   int temp = array[iterator];
   // do something with array variables...
   //
   // move on to next array entry for next loop iteration
   ++iterator;
}


There are several different ways of working with arrays - you can use pointer notation as you did above, but not quite as you represented it. Personally, I think the method I demonstrated here is less confusing. In case you'd like to see the pointer way of doing it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int array[10];
int* iterator = array[0]; // set our iterator to the address of the first array element
while (/* blah */)
{
   // blah
   // get array element
   int temp = *iterator; // now to get array element value, we dereference our pointer
   // do something with array variables...
   //
   // move on to next array entry for next loop iteration
   // this is pointer arithmetic: adding 1 to a pointer results in enough being added to it
   // to get the address of the next element
   ++iterator; 
   
}


PS: You'll notice I used prefix rather than suffix incrementation. I didn't know this as a beginner, but you may as well: prefix incrementation is better where it is possible as the performance is better (with suffix incrementation, the variable's old value is returned rather than it's new one so this must be stored in a temporary memory location). At least, I think that's how it works. If I'm wrong then I'm sure someone will correct me!

Yes you can use arrays - that was my first suggestion. Don't increment the original array pointer, since each time you do, you're effectively "losing track" of an array element.

But you can do this:
1
2
3
4
5
6
7
8
9
10
11
12
int array[10];
int iterator = 0;
while (/* blah */)
{
// blah
// get array element
int temp = array[iterator];
// do something with array variables...
//
// move on to next array entry for next loop iteration
++iterator;
}


There are several different ways of working with arrays - you can use pointer notation as you did above, but not quite as you represented it. Personally, I think the method I demonstrated here is less confusing. In case you'd like to see the pointer way of doing it:



now i feel stupid that i didn't think of using a integer instead of a number for the arrays size =)


well that simplified things.. thanks for the help :)
Last edited on
The values in an integer array:
int array[10];
will not default to anything in particular. But if you want to set them all to defaults, just loop through the array setting them all:
1
2
for (unsigned i=0; i<10; i++)
  array[i] = 1; // or whatever other default value you want 


i didn't think of using a integer instead of a number for the arrays size =)

What exactly do you mean? The size of an array allocated like this:
int array[a number
must be known at compile time, i.e. you can't use a non constant variable as the size.
Topic archived. No new replies allowed.