I had to write a code for range [40,200] and random numbers should have printed until it printed 58. The code is right and it does that. However, the second part is to print how many integers did it print until it reached 58. and I do not know how to do it, can you help me please and tell me what code should I use for that? I searched all over the internet,however I could not find it. Help me please, I am beginner and I want to study how to do it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include<time.h>
usingnamespace std;
int main()
{
int x;
do {
x=rand()%161+40;
cout<<x<<endl;
}
while (x!=58);
return 0;
}
so should I declare int counter; and than how to increase it by one with each iteration? should I write x+counter? or how ? I do not know much in c++, I only had 2 lessons and I am trying and trying and I like it, but I do not know much :)) Can you help me please and tell me how can I do it?
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std ;
int main()
{
// seed the random number generator
srand( time(0) ) ;
// initialize a counter to zero
int cnt = 0 ;
int x ;
do {
// increment the counter
++cnt ;
x=rand()%161+40;
cout << cnt << ". " << x << '\n' ;
}
while (x!=58);
}