check my code in do-while loop.

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 7460 Views - Last Post: 26 March 2011 - 10:56 AM Rate Topic: **--- 2 Votes

#16 jimblumberg  Icon User is online

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: check my code in do-while loop.

Posted 24 March 2011 - 09:37 PM

Ok lets start from the top:
#include <<iostream.h>>
#include<<conio.h>>
#include<<stdio.h>>



All three of these include files have too many << and >> . They should be:
#include <iostream>
If you are writing a C++ program you should not need the second and third include file.

Also what is your compiler and compiler version?

Next lets look at the first do{}while(); loop.
int i=10;
int x=2;

do
 {
    cout<<x <===what should i write here? DO code.. the statement? i tried many things but it doesn't work.
 }
while (x <= i)



In this code you are missing a semicolon on the end of your while line. So lets make this loop do something.

#include <iostream>

using namespace std;

int main()
{
   int i=10;
   int x=2;

   do
   {
      cout << x << endl;
      x += 2;  // Add 2 to x
    }while (x <= i);
} // end of main function.




This should print out even numbers from 2 to 10.

Please enter and compile and run this program. Does it work? Do you understand what this small program is doing?



Jim
Was This Post Helpful? 2
  • +
  • -

#17 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 10:03 PM

Thank expert JM. but when i tried to add the DO code in my program it change the my output. i just only to declare 2 variables? for x and i only?
I'm using a DEV C C++ version 2.

My output look like this:

0
0 2
0 2 4
0 2 4 6
0 2 4 6 8
0 2 4 6 8 10

when I added DO its ruin my program. i have no idea how and where i put DO code.
Was This Post Helpful? 0
  • +
  • -

#18 jimblumberg  Icon User is online

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: check my code in do-while loop.

Posted 24 March 2011 - 10:03 PM

I did not say to add the code to your program. I am trying to show how to use a do-while loop. I will get back to your problem in a bit. Do you understand the what the program in my last post is doing?

Jim
Was This Post Helpful? 0
  • +
  • -

#19 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 10:05 PM

View Postjimblumberg, on 24 March 2011 - 10:03 PM, said:

I did not say to add the code to your program. I am trying to show how to use a do-while loop. I will get back to your problem in a bit. Do you understand the what the program in my last post is doing?

Jim



[qoute]

YES! i understand. thanks so much JM


[/qoute]
Was This Post Helpful? 0
  • +
  • -

#20 jimblumberg  Icon User is online

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: check my code in do-while loop.

Posted 24 March 2011 - 10:13 PM

Good. Now lets add a while statement.
#include <iostream>

using namespace std;

int main()
{
   int i=10;
   int x=2;

   do
   {
      cout << x << endl;
      x += 2;  // Add 2 to x
    }while (x <= i);

    i = 10;
    x = 2;
    while (x <= i)
    {
      cout << x << endl;
      x += 2;  // Add 2 to x
    }


} // end of main function.





This should show how to use both a do-while loop and while loop. Both loops should do the same thing. If you will notice the only difference between the two loops is missing do and the location of the while clause.

Now after studying this code you should be able to take the code from post #8, which you said was working, and convert the while loops into do-while loops.

Jim
Was This Post Helpful? 0
  • +
  • -

#21 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 10:57 PM

This is my new code now:


#include <iostream>

using namespace std;

int main()
{
        int i=10;
        int x=2;
       int y=0;

do
  {
         cour<<y<,endl;
         x+=2;
   }
    while (y <=x);
    {
    cout <<y<<" ";
     y += 2;
     }
     cour <,endl;
     x+=2;
     }

   getch();
   return 0;
}




There's a problem/error in my getch and retun, don;t know how to fixed this. JM? is this what u meant for convert from WHILE to DO-WHILE LOOP? :(
Was This Post Helpful? 0
  • +
  • -

#22 jimblumberg  Icon User is online

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: check my code in do-while loop.

Posted 24 March 2011 - 11:09 PM

Does your code look anything like the code you had working in post 10?

#include <iostream>

using namespace std;

int main()
{
   int i=10;
   int x=2;
   int y=0;

   while (x <= i)
   {
      y = 0;
      while ( y <= x)
      {
         cout << y << " ";
         y += 2;
      }
      cout <<endl;
      x += 2;
   }
   return 0;
}



Answer: No it doesn't.

Now take this code and convert the while() loops to do-while.

Jim
Was This Post Helpful? 0
  • +
  • -

#23 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 25 March 2011 - 12:35 AM

OMG! i failed. Do i need to make two DO and two WHILE??
i'm getting pissed of this thing. Nothing happen in my program. It's seems i'm not learning.
Was This Post Helpful? 0
  • +
  • -

#24 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: check my code in do-while loop.

Posted 25 March 2011 - 05:22 AM

View Postbenchz23, on 25 March 2011 - 03:35 AM, said:

OMG! i failed. Do i need to make two DO and two WHILE??
i'm getting pissed of this thing. Nothing happen in my program. It's seems i'm not learning.


No matter how hard you try you can't build a house without a foundation. I suggest you take up a C++ book or head on the internet in search of some beginner tutorials and start reading.

The answer(s) is/are actually right in-front of you. If you can't grasp the concept of a Do While loop you can't grasp the concept of nested Do While loop.
Was This Post Helpful? 1
  • +
  • -

#25 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 26 March 2011 - 09:09 AM


#include <stdio.h>
#include <iostream.h>
#include <conio.h>

using namespace std;

int main()
{

int i=10;
int x=0;
int y=0;


do {
y = 0;
do {
cout << y << " ";
y += 2;
}while ( y <= x);
cout <<endl;
x += 2;
}while (x <= i);

getch();
return 0;
}




i finally solved my problem with my OWN self. tskk

This post has been edited by JackOfAllTrades: 26 March 2011 - 11:05 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? -1
  • +
  • -

#26 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: check my code in do-while loop.

Posted 26 March 2011 - 10:56 AM

Not really, you wouldn't have got past compiling the program without help from the people here. Give credit where credit is due.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2