Help with loops, total noob here.

Hey guys im a programming noob and i would need some help with a litle loop.

can you rewrite it in for and while loops. (it was a on a test and its only supposed to be rewriten in other loop kinds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  do
{
zn=getche(); zn=zn+32;
cout << zn; i++;}
while(zn>='A' && zn <='Z');
cout << "there were:" << i << endl;













Last edited on
Code layout is really important. You could start by revisitng your indentation, then it will be much easier to provide alternative implementations.
Perhaps..

1
2
3
4
for(zn = getche(); zn>='A' && zn <= 'Z'; zn += 32)
{
    cout << "there were: " << i << endl;
}
On test i was given only this with instructions to rewrite the code in other forms.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


char zn; int i=0;

  do
{
zn=getche(); zn=zn+32;
cout << zn; i++;}
while(zn>='A' && zn <='Z');
cout << "there were:" << i << endl;






Ok, from that code, show us what code you think is in the loop?
in the instructions it says that its only a part of a program.
1
2
3
4
5
6
7
8
 

 do
{
zn=getche(); zn=zn+32;
cout << zn; i++;}
while(zn>='A' && zn <='Z')


I understand this that you have to cin the char until the char is bigger or the same as A or lower or the same as Z. In the code you cout the zn, and you count how many zn there were?


1
2
3
4
5
6
7
8
{char zn;int i=0;
    for(zn;(zn>='A' && zn<='B');zn=zn+32)
        { zn=getche();
    cout << zn;i++;}

    cout << "there were:" << i << endl;



this was my attempt on for loop but i dont think its right
Last edited on
No. The first thing you have to do is fix the formatting so you can see what's going on.
1
2
3
4
5
6
7
8
9
do
{
    zn=getche();
    zn=zn+32;
    cout << zn;
    i++;
}
while(zn>='A' && zn <='Z');
cout << "there were:" << i << endl;


Hopefuly it's now clear what's in the loop, and the kind of loop is do/while where the check is at the end.

So all you have to do it put that code in the other kinds of loops as required. Got it?
Yes, it way better your way.

so for for loop i did

1
2
3
4
5
 for(zn;(zn>='A' && zn <='Z');zn=zn+32)
      {
      zn=getche();
      cout << zn;
      i++;


and for while i did

1
2
3
4
5
while((zn>='A' && zn <='Z'));
     {zn=getche();
     zn=zn+32;
     cout << zn;
     i++;


and they work perfectly fine! thanks mate!
Topic archived. No new replies allowed.