/b

I am trying to understand this 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
//---------------------------------------------------------------------------

#include <clx.h>
#include <iostream.h>
#include <conio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv)
{
        cout << endl << "Starting program...." << endl << endl;
        int i = 6;
        while (i-- > 0)
        {
                cout << endl << "Today I have " << i;
                cout << " problems to worry about.";
        }
        cout << "\b!\nYipee!";
        cout << endl << endl << "Press any key to continue..";
getch();
return 0;
}


It is code in a book I am reading. I noticed the output on one of the last lines changes.

"Today I have 1 problems to worry about."

Then the next one shows this.

"Today I have 0 problems to worry about!"

From another post on this forum I see that /b is a back space.

So from that I concluded that

 
cout << "\b!\nYipee!";

will backspace one time, replace the '.' with a '!' and display "Yipee!" on the next line.

So is the /n the same as endl?

That is the way I see it, can someone confirm?

If so, I should also be able to do the same with the line by nesting another loop to check if the i = 1 so it will not display 'problems.' and replace it with 'problem.'

-BHill

Last edited on
So I tried this,

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 <clx.h>
#include <iostream.h>
#include <conio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv)
{
        cout << endl << "Starting program...." << endl << endl;
        int i = 6;
        while (i-- > 1)
        {
                cout << endl << "Today I have " << i;
                if (i = 1)
                {
                        cout << " problem to worry about.";
                }
                else
                {
                cout << " problems to worry about.";
                {
        }

        cout << "\b!\nYipee!";
        cout << endl << endl << "Press any key to continue..";
getch();
return 0;
}


I am getting an error and it will not build. I am using C++ Builder 6.

-BHill
I tried this and output seems to cease my loop.

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
//---------------------------------------------------------------------------

#include <clx.h>
#include <iostream.h>
#include <conio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv)
{
        cout << endl << "Starting program...." << endl << endl;
        int i = 6;
        while (i-- > 1)
        {
                cout << endl << "Today I have " << i;
                if (i = 1)
                {
                        cout << " problem to worry about.";
                }
                else
                        cout << " problems to worry about.";

        }

        cout << "\b!\nYipee!";
        cout << endl << endl << "Press any key to continue..";
getch();
return 0;
}


-BHill
in your else statement the curly braces is 2 times open, close the second one.
your if i = 1 must be if i == 1.

then it worked for me when i deleted the getch(); line

i am using codeblocks compiler.
No go with this either.
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
33
//---------------------------------------------------------------------------

#include <clx.h>
#include <iostream.h>
#include <conio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv)
{
        cout << endl << "Starting program...." << endl << endl;
        int i = 6;
        while (i-- > 1)
        {
                cout << endl << "Today I have " << i;
                if (i = 1)
                {
                        cout << " problem to worry about.";
                }
                else
                        {
                        cout << " problems to worry about.";
                        }
                }


        cout << "\b!\nYipee!";
        cout << endl << endl << "Press any key to continue..";
getch();
return 0;
}


-BHill
Ok, I figured it out. Maybe someone can make a suggestoin on a better way.


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 <clx.h>
#include <iostream.h>
#include <conio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv)
{
        cout << endl << "Starting program...." << endl << endl;
        int i = 6;
        while (i-- > 2)
        {
                cout << endl << "Today I have " << i;
                cout << " problems to worry about.";

        }
        cout << endl << "Today I have " << i;
        cout << " problem to worry about.";
        i=i-1;
        cout << endl << "Today I have " << i;
        cout << " problems to worry about.";
        cout << "\b!\nYipee!";
        cout << endl << endl << "Press any key to continue..";
getch();
return 0;
}


-BHill
And now I have this.

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
//---------------------------------------------------------------------------

#include <clx.h>
#include <iostream.h>
#include <conio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv)
{
        cout << endl << "Starting program...." << endl << endl;
        int i = 6;
        char str[] = " problems";
        while (i-- > 2)
        {
                cout << endl << "Today I have " << i;
                cout <<  str << " to worry about.";

        }
        cout << endl << "Today I have " << i;
        cout << str << "\b " << " to worry about.";
        i=i-1;
        cout << endl << "Today I have " << i;
        cout << " problems to worry about.";
        cout << "\b!\nYipee!";
        cout << endl << endl << "Press any key to continue..";
getch();
return 0;
}


-BHill
Another question. Can you nest a For loop inside a While loop? As you can see from the examples I tried to but failed. Was I just doing it wrong?

-BHill
You can nest any control loop in any control loop, as often as you want.

I'm not sure where you tried that, though. Haven't seen a for anywhere in your code.
im pretty sure you can nest a for inside a while loop.

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
33
//---------------------------------------------------------------------------


#include <cstdlib>
#include <iostream>


//---------------------------------------------------------------------------
using namespace std;

int main()
{
        cout << endl << "Starting program...." << endl << endl;
        int i = 6;
        while (i-- > 1)
        {
                cout << endl << "Today I have " << i;
                if (i == 1)
                {
                        cout << " problem to worry about.";
                }
                else
                        {
                        cout << " problems to worry about.";
                        }
                }


        cout << "\b!\nYipee!";
        cout << endl << endl << "Press any key to continue..";
cin.get();
return 0;
}


this code works for me.

i changed some of the header files, used namspace std, and changed ur if to if (i == 1) like @Nelphin said and it shows

OUTPUT:

1
2
3
4
5
6
7
8
9
10
11
Starting program....


Today I have 5 problems to worry about.
Today I have 4 problems to worry about.
Today I have 3 problems to worry about.
Today I have 2 problems to worry about.
Today I have 1 problem to worry about!
Yipee!

Press any key to continue..


if this is the output your looking for then hope this helps, if you want it to display ' today you have 0 problems to worry about! ' then Yipee then i suggest using a switch case.
@ Olzi, that is what I was looking for, but wanted the 'today I have 0 problems to worry about!' as well. I knew there had to be a better way to do it than I was doing. I just got to the part of the book where they talk about switch case. So I will look into it. I have to get some sleep today but will go through it tonight. I apreciate your help. This is different from what I have done before.

-BHill

@Gaminic

I attempted here. It didn't work, so I am obviously not understanding something.

1
2
3
4
5
6
7
8
9
10
11
12
13
while (i-- > 1)
        {
                cout << endl << "Today I have " << i;
                if (i = 1)
                {
                        cout << " problem to worry about.";
                }
                else
                        {
                        cout << " problems to worry about.";
                        }
                }


Maybe I am misunderstanding terminology, but I thought that was what I was attempting there.

-BHill
Last edited on
That's not a for loop. A for loop can be recognized by the keyword "for", hence its name.

You can rewrite that while loop as a for loop:
1
2
3
for(int i  = 6; i > 0; --i) {
  // Statements
}


The difference between while and for is that a for loop is (supposed to be) used for cases where the amount of iterations is known (e.g. 0->20, 0->n, 0->myVec.size(), ...) and a while uses a conditional statement. However, every while can be written as a for and vice versa. It's just not always the most elegant way.
BHill Your problem is that you are messing up your if statement.

if (i=1) is setting i to 1 and then checking if it is not zero. Since it is not zero, it will display the wrong thing. It will also force you to exit the while on the first attempt. To have everything good use the following:
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 <clx.h>
#include <iostream.h>
#include <conio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv)
{
        cout << endl << "Starting program...." << endl << endl;
        int i = 6;
        
        while (i-- > 0)
        {
                cout << endl << "Today I have " << i << " problem";
                if (i != 1) cout << "s";
                cout <<  "  to worry about.";
        }
        cout << "\b!\nYipee!";
        cout << endl << endl << "Press any key to continue..";
getch();
return 0;
}
Thanks Gaminic, I obviously had a huge DUH moment. I appreciate your time.

-BHill
Topic archived. No new replies allowed.