Cicles

hello, First Time here. i dont know what to say but i have a problem. i am doing a program that is showing odd numbers using three cycles, for, while and do-while. ive managed to show the odd numbers in while and do while, but it only starts at the numer 7 on the for cycle, i want it to show staring from the first number (in this case 1), is there any way i can make it start at number 1. sorry if my english is bad, im from mexico.

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
27
28
29
30
  #include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std; 
int main(int argc, char** argv)
{	system ("color 1B");
	
int k;
for (k=1;k<199;k+=2)
cout <<"IMPAR "<<k<<endl;
cout<<"\n";
	
	int j=-1;
	while (j<199)
	{ 
		if (j+=2) 
		cout<<"IMPAR "<<j<<endl;
	}
	cout<<"\n";
	
int l=-1;
do{if (l+=2)
cout<<"IMPAR "<<l<<endl;
j++;

	}while (l<199);	

	system ("PAUSE");
	return 0;
}
Edit: i Ran it In c++ Shell and it runs all the cycles starting at number one. why is that?
http://cpp.sh/2vkrn
im using devC++ By the way
Last edited on
Hello rexorzz,

Like rexorzz it worked for me in Visual Studio. I believe what has happened is after all three loops have run you have to many lines in your display to scroll back to the very first line. Try putting a pause after the first two loops and see what happens when you scroll back.

Hope that helps,

Andy
Last edited on
Hi Handy Andy,
i tried what you said and it works, but i want the program to run all the cycles, i tried chaning the order of it and the same problem is still happening.

edit: I tried taking only the Do While Cycle to run on its own and it works, and the other programs runs fine with For and while, so i think theres a problem with the Do while Cycle. is there any way i can put the three of them together?
Last edited on
What is your program all about?
What is your expected output?
closed account (49iURXSz)
Tranaslated al español con Google Translate:

Usted ya ha intentado cambiar el búfer de la consola para Windows cmd?

https://translate.googleusercontent.com/translate_c?act=url&depth=1&hl=en&ie=UTF8&prev=_t&rurl=translate.google.com&sl=en&tl=es&u=http://superuser.com/questions/378313/windows-command-prompt-how-do-i-increase-my-buffer&usg=ALkJrhjmlEAW6QMCA9NYGeUpplPDmlbBsg
hello Student Of Jack,
im trying to create a program with c++ using three cycles (Do while, For and While). as ive said earlier, i took The Do while Cycle out of the program to run on its own, and it works, but i want it to run all three cycles in the same program. hope i answered your question.
But I want it to run all three cycles in the same program

What do you mean? Did you just run three cycles in your program already?
Last edited on
closed account (49iURXSz)
Tranaslated al español con Google Translate:

De acuerdo, de recibir una prueba para el lazo y funciona! Ahora vamos a verificar si se trata de la consola o del programa que es el problema. Poner su tiempo y do-while bucle de vuelta en su código fuente y ver si el problema vuelve a aparecer. Si lo hace, entonces sabemos que es la consola, y necesitamos cambiar el tamaño del búfer para que podamos ver toda la producción. Si el problema no vuelve a aparecer, entonces yo soy sólo pasmado ...

Basically, lets test the program again with the three loops all in your source code and see if the Windows console is messing with us.
No. The windows console is not the problem. Go away.

@rexorzz
Your English is fine.

Your program does start at 1, see Handy Andy's response.

You don't need if statements if you are knowingly generating odd numbers. Just make sure to add 2 every time.

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
27
28
29
30
31
32
#include <iostream>
#include <cstdlib>
#include <string.h>
using namespace std; 
int main(int argc, char** argv)
{
  system ("color 1B");
	
  // FOR
  for (int k=1; k<200; k+=2)
    cout <<"IMPAR "<<k<<endl;
  cout<<"\n";
	
  // WHILE
  int j=1;
  while (j<200)
  { 
    cout<<"IMPAR "<<j<<endl;
    j+=2;
  }
  cout<<"\n";
	
  // DO..WHILE
  int l=1;
  do{
    cout<<"IMPAR "<<l<<endl;
    l+=2;
  }while (l<200);	

  system ("PAUSE");
  return 0;
}

By the way, l is a terrible variable name -- it looks too much like a one.

Hope this helps.
Hello rexorzz,

Maybe you misunderstood what I meant about the pause. The lines with comments is all I added to your program. This allows each loop to do its work and pause before the next loop runs. All three can be in the same program yet run separately of each other.

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
	system("color 1B");

	int k;
	for (k = 1; k<199; k += 2)
		cout << "IMPAR " << k << endl;
	cout << "\n";
	
	_getch();  // quick pause. Waits for any key press
	
	int j = -1;
	while (j<199)
	{
		if (j += 2)
			cout << "IMPAR " << j << endl;
	}
	cout << "\n";
	
	_getch();  // quick pause. Waits for any key press
	
	int l = -1;
	do {
		if (l += 2)
			cout << "IMPAR " << l << endl;
		j++;

	} while (l<199);


I hope that clears things up for you,

Andy

@duoas,

Thank you for letting me know that I did something worthwhile.
Topic archived. No new replies allowed.