convert do\while to a for statement

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 3584 Views - Last Post: 16 August 2009 - 11:00 PM Rate Topic: -----

#1 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

convert do\while to a for statement

Post icon  Posted 14 August 2009 - 02:47 AM

I was wondering if anyone could explain to me how to go about taking a do while statement in a program and converting it into a for statement.

Trying not to post the code from the assignment so I can try and figure it out as much as possible but I am just not getting how to convert it.

TIA!!
Is This A Good Question/Topic? 0
  • +

Replies To: convert do\while to a for statement

#2 redkid  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 32
  • Joined: 23-April 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 03:37 AM

Three things you have to find out: where to start with counting, what to do and end of your count.

Then a do while kind of looks like this,
//start counting
do {
//do what you have to
//go towards the end of counting
} while (//when to end count
)


And a for loop looks like:
for (//start of count; when to end count; go towards the end
) {
//do what you have to
}


This post has been edited by redkid: 14 August 2009 - 03:38 AM

Was This Post Helpful? 0
  • +
  • -

#3 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 05:15 AM

Ok here is the original program
//Ch8AppE02.cpp
//Calculates and displays each region's
//total sales amount
//Created/revised by Jeremy Dean Lynch on 08/13/09
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	int sales	   = 0;
	int totRegSales = 0;

	for (int region = 1; region < 3; region += 1)
	{
		cout << "First sales amount for Region " << region << ": ";
		cin >> sales;

		do
		{
			totRegSales = totRegSales + sales;
			cout << "Next sales amount for Region " << region << ": ";
			cin >> sales;
		}  
		while (sales > 0);

		cout << endl << "Region " << region << " sales: $" << totRegSales << endl << endl;
		totRegSales = 0;

	}  //end for



I am suppose to replace the do while with a for statement. I will post what I came up with it doesnt work properly but I dont think I am going about this correctly. It is not adding correctly and I need it to ask for a total of four inputs. As always thank you a bunch!!

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	int sales	   = 0;
	int totRegSales = 0;

	for (int region = 1; region < 3; region += 1)
	{
		cout << "First sales amount for Region " << region << ": ";
		cin >> sales;
		sales = 0; sales = 4; sales =+1;
		totRegSales = totRegSales + sales;
		cout << "Next sales amount for Region " << region << ": ";
		cin >> sales;
		cout << endl << "Region " << region << " sales: $" << totRegSales << endl << endl;
		
		}//end for

	cout << "End of program" << endl;
	return 0;
}	//end of main function


Was This Post Helpful? 0
  • +
  • -

#4 aquaman721  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-April 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 05:27 AM

Try adding another for loop after totRegSales
code/ for (int numSales = 1; numSales <4; numSales +=1)
{

cout << "Next sales amount for Region "
<< region << ": ";
cin >> sales;
totRegSales = totRegSales + sales;
}
Was This Post Helpful? 0
  • +
  • -

#5 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 06:02 AM

If I add another for loop how you suggested I get an error saying region is an undeclaired identifier. Maybe I did it wrong I will keep fiddling
Was This Post Helpful? 0
  • +
  • -

#6 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 07:19 AM

Anyone else have any suggestions?
Was This Post Helpful? 0
  • +
  • -

#7 redkid  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 32
  • Joined: 23-April 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 07:30 AM

View Postvwyodapink, on 14 Aug, 2009 - 06:19 AM, said:

Anyone else have any suggestions?

Hey! I think you need to see the code from a real world perspective and realize what it's trying to do. Then go on from there.

You're supposed to replace the do while loop but you removed it.
So hint: it's a loop inside a loop.
Was This Post Helpful? 1
  • +
  • -

#8 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 09:23 AM

Are you just trying to input 4 sales amount and display the total for that specific region?
int main()
{
	int sales	   = 0;
	int totRegSales = 0;

	for (int region = 1; region < 3; region += 1)
	{
		cout << "First sales amount for Region " << region << ": ";
		cin >> sales;

		for( int x = 0; x < 3; x++)//replaces the post test loop
		{
			totRegSales = totRegSales + sales;
			cout << "Next sales amount for Region " << region << ": ";
			cin >> sales;
		}	 
		totRegSales = totRegSales + sales;
	 
		cout << endl << "Region " << region << " sales: $" << totRegSales << endl << endl;
		totRegSales = 0;

	}  //end for
}

This post has been edited by seeP+: 14 August 2009 - 09:37 AM

Was This Post Helpful? 1
  • +
  • -

#9 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 02:12 PM

OK i think I am understanding why you did what you did. Can you explain why the second for statement adds int x? reason I am asking is because I am not seeing it used other than in that line.

Also if I wanted to add a sentinel value you the program how and where would I do that ? If I wanted it to keep asking sales amounts until -1 is struck. I know it should be written something like I think?
for (x != -1) 



I have tried adding it in to the exsisting statement and in other areas but I am not seeing how to get it in there correctly.

Thanks a bunch !!!

This post has been edited by vwyodapink: 14 August 2009 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

#10 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 04:39 PM

I can not figure out how to add it in there correctly :P Crappy! Gonna go start the next program and come back maybe that will help
Was This Post Helpful? 0
  • +
  • -

#11 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 05:58 PM

The x is only used there as a counter.

for (x != -1) 


The "for" loop takes three arguments. If you decide not to use any argument you still need to include the semicolon that separates them.
for(; x != -1;)


Was This Post Helpful? 1
  • +
  • -

#12 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 08:50 PM

ok I do not think I get how to use the sentinel value correctly. Here is the program as I have it solving the earlier problem. I am not suppose to transform the program below to keep asking for sales until the user enters -1. My thinking was adding that sentinel to the for() statement but either I am not doing it correctly or something. I am thinking also maybe I shouldnt be putting the sentinel value in the for statement.
#include <iostream>

using std::cout;
using std::cin;
using std::endl;


int main()
{
	int sales	   = 0;
	int totRegSales = 0;

	for (int region = 1; region < 3; region += 1)
	{
		cout << "First sales amount for Region " << region << ": ";
		cin >> sales;

		for( int x = 0; x < 3; x++)
		{
			totRegSales = totRegSales + sales;
			cout << "Next sales amount for Region " << region << ": ";
			cin >> sales;

		}	
		totRegSales = totRegSales + sales;
	 
		cout << endl << "Region " << region << " sales: $" << totRegSales << endl << endl;
		totRegSales = 0;

	}  //end for

return 0;
}



Sorry guys I felt like I was just starting to get things then these last few programs have just been making me feel lost as heck :P Thanks again for your time and patience !! Or maybe its the fact that I am having to just change a certain section of something already made instead of slowly trying to figure out how to write the whole program.

This post has been edited by vwyodapink: 14 August 2009 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#13 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 09:38 PM

If you want to exit the loop before the condition of sales is met add an "if" statement to check if variable sales is -1(if it is then do...etc).
Was This Post Helpful? 0
  • +
  • -

#14 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: convert do\while to a for statement

Posted 14 August 2009 - 11:37 PM

OK I feel like screaming this is frustrating me. I have tried every way of entering an if statement that I can think of in the I added some notes in the if statement to let you know what else I have tried. I have tried looking in my book and reading ifs and I dont understand how to put it in that loop correctly.
#include <iostream>

using std::cout;
using std::cin;
using std::endl;


int main()
{
	int sales	   = 0;
	int totRegSales = 0;
	

	

	for (int region = 1; region < 3; region += 1)
	{
		cout << "First sales amount for Region " << region << ": ";
		cin >> sales;

		for(int x = 0; x < 3; x++)
		{
			cout << "Next sales amount for Region " << region << ": ";
			cin >> sales;
			totRegSales = totRegSales + sales;
			if (x == -1)
				break;// i have tried putting the cout that I would desire it to print once the -1 is entered. 
			//I have tried it with {} around the break or cout..putting it inside and outside this for statement and intializing
			//int x in the beginning. 

			
		}
		
		totRegSales = totRegSales + sales;
	 
		cout << endl << "Region " << region << " sales: $" << totRegSales << endl << endl;
		totRegSales = 0;

	}  //end for



THANK YOU
Was This Post Helpful? 0
  • +
  • -

#15 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: convert do\while to a for statement

Posted 15 August 2009 - 09:11 AM

#include <iostream>

//using std::cout;
//using std::cin;
//using std::endl;

using namespace std;

int main()
{
	int sales = 0;
	int totRegSales = 0;




	for (int region = 1; region < 3; region++) // same as previous
	{
		cout << "First sales amount for Region " << region << ": ";
		cin >> sales;
	 if (sales == -1) // moved if statement
		break;

		for(int x = 0; x < 3; x++)
		{
			cout << "Next sales amount for Region " << region << ": ";
			cin >> sales;

			totRegSales += sales; // made smaller



		}

		totRegSales += sales;

		cout << endl << "Region " << region << " sales: $" << totRegSales << endl << endl;
		

}
	} // added }


Comments in code.

This post has been edited by redhotfire0: 15 August 2009 - 09:13 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2