Problem with do-while loop

error C2447: '{' : missing function header (old-style formal l

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 3164 Views - Last Post: 22 April 2009 - 08:03 PM Rate Topic: -----

#1 mvalor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 16-November 08

Problem with do-while loop

Posted 07 April 2009 - 09:12 PM

I cannot see where the "{" is missing in my program, and keep getting: error C2447: '{' : missing function header (old-style formal list?), can anyone find this for me?

/* 
It's July 1988 and the Battleship USS NEW JERSEY approches a South Korean land target for a 
firing demonstration and prepares a 16" 50 Caliber firing exercise to clear out imaginary enemy ground infantry. 
This would be the dialog between fire control and Turret #2 during the naval exercise.
*/
#include <iostream>
#include <cmath>
using namespace std;

/*FUNCTION DECLARATION*/
float distance (double x1, double y1, double x2, double y2);  //Distance function prototype.

int main ()
{
/*FUNCTION CALL*/
double x1; 
double y1;
double x2;
double y2;

//Entering coordinates
cout << "GENERAL QUATERS, GENERAL QUATERS ALL HANDS MAN THEIR BATTLE STATIONS!" "\n";								//Bridge announcement
cout << "SET CONDITION ZEBRA! \n";																					//Bridge announcement
cout << "SMOKING LAMP IS OUT! \n";																					//Bridge announcement
cout << "\n\n";
cout << "Turret 2, ground target sighted, elevation 37 degrees by 65 degrees relative... \n";						//Fire control to Turret
cout << "Ready 3 gun manual bracketed salvo, HC Frag Proximity full charge rounds...confirm.\n";					//Fire control to Turret
cout << "\n\n";
cout << "Fire Control, 3 gun bracketed salvo, HC Frag Proximity full charge rounds load confirmed aye.\n";			//Turret to Fire Control
cout << "\n\n";
cout << "Turret 2, 3 gun bracketed salvo confirmed aye! Enter range coordinates and conrirm.   \n";					//Fire control to Turret
cout << "\n\n";
	cout << "Firing coordinate x1: ";
	cin  >> x1;
	cout << "Firing coordinate y1: ";
	cin  >> y1;
	cout << "Firing coordinate x1: ";
	cin  >> x2;
	cout << "Firing coordinate y2: ";
	cin  >> y2;
	cout << "\n";
	cout << "Range 1 coordinates : "<<"x1: "<<x1 <<"\t"<<"y1: "<<y1<<  "confirmed\n";
	cout << "Range 2 coordinates : "<<"x2: "<<x2 <<"\t"<<"y2: "<<y2<<  "confirmed\n";
	cout << "Fire Control, firing solution confirmed. \n";															//Turret to Fire Control
	cout << "\n\n"; 
	cout << "Turret 2, Fire solution confirmed aye! \n";															//Fire control to Turret

// Distance call
	distance(x1, y1, x2, y2);

system("pause");
	return 0; 
} 

//COMPUTING FIRING RANGE
/*FORMULA LAY-OUT
distance = sqrt( (x1 - x2)^2 + (y1 - y2)^2 )
x1-x2 = a
y1-y2 = b
D= sqrt(a)^2 + (a)^2
(a)^2 = z1 = pow(a,2.0)
(b)^2 = z2 = pow(b,2.0)
z1+z2 = w
D= sqrt(w)
*/
float distance (double x1, double y1, double x2, double y2)
{   
double a = x1-x2; 
double b = y1-y2;
double z1 = pow(a,2.0);
double z2 = pow(b,2.0);
double d = sqrt( z1 + z2);

cout << "Fire Control, Target locked on firing solution "  << d  << "knots at 37 degrees by 65 degrees relative\n";	//Turret to Fire Control
cout << "Fire Control, Turret 2 standing by for firing order sir! \n";												//Turret to Fire Control
cout << "\n";
cout << "Turret 2...Fire when ready Griddly...";																	//Fire control to Turret
cout << "\n";

return 0;
}


{
char y=1; 
char n=0;

do {
	cout << "Fire control, 3 gun salvo fired and no mis-fires. Confirm reload. \n";
	cin >>	choice;

	if (choice == 'y' ||choice =='n')
		{
		cout << "Reload and wait for firing coordinates \n";
		}
		else if (choice =='n')
		{
		cout << "Negative, stand down and cease fire, finalize post fire proceedures and download all ammo. \n";
		}
} while (choice == 1);

return 0;
}






I do appreciate the help.

MV*/ :blink:

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with do-while loop

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Problem with do-while loop

Posted 07 April 2009 - 09:14 PM

This isn't part of anything:


{
char y=1;
char n=0;

do {
	cout << "Fire control, 3 gun salvo fired and no mis-fires. Confirm reload. \n";
	cin >>	choice;

	if (choice == 'y' ||choice =='n')
		{
		cout << "Reload and wait for firing coordinates \n";
		}
		else if (choice =='n')
		{
		cout << "Negative, stand down and cease fire, finalize post fire proceedures and download all ammo. \n";
		}
} while (choice == 1);

return 0;
}




Is it supposed to be part of your main function? If so, you need to delete one of the return 0;'s and move that snippet up into main's area
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,068
  • Joined: 10-May 07

Re: Problem with do-while loop

Posted 07 April 2009 - 09:15 PM

Quote

cout << "\n";

return 0;
}


{
char y=1;
char n=0;

do {




Above your 2 chars... you end int main with return 0, & then just have floating code. It belongs in a function.
Was This Post Helpful? 0
  • +
  • -

#4 mvalor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 16-November 08

Re: Problem with do-while loop

Posted 07 April 2009 - 09:23 PM

View Postno2pencil, on 7 Apr, 2009 - 08:15 PM, said:

Quote

cout << "\n";

return 0;
}


{
char y=1;
char n=0;

do {




Above your 2 chars... you end int main with return 0, & then just have floating code. It belongs in a function.


I'm sorry, I dont understand or either my brain is on formaldehyde, please explain.

MV*/
Was This Post Helpful? 0
  • +
  • -

#5 mvalor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 16-November 08

Re: Problem with do-while loop

Posted 07 April 2009 - 09:33 PM

View PostKYA, on 7 Apr, 2009 - 08:14 PM, said:

This isn't part of anything:


{
char y=1;
char n=0;

do {
	cout << "Fire control, 3 gun salvo fired and no mis-fires. Confirm reload. \n";
	cin >>	choice;

	if (choice == 'y' ||choice =='n')
		{
		cout << "Reload and wait for firing coordinates \n";
		}
		else if (choice =='n')
		{
		cout << "Negative, stand down and cease fire, finalize post fire proceedures and download all ammo. \n";
		}
} while (choice == 1);

return 0;
}




Is it supposed to be part of your main function? If so, you need to delete one of the return 0;'s and move that snippet up into main's area



Dear Kya,

I cant see it, its like I should be in front of me and I should be looking at it, but it eludes me.

Which return should I eliminate and wher should I put what snippet where?

MV*/
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Problem with do-while loop

Posted 08 April 2009 - 03:10 AM

View Postmvalor, on 7 Apr, 2009 - 08:33 PM, said:

Dear Kya,

I cant see it, its like I should be in front of me and I should be looking at it, but it eludes me.

Which return should I eliminate and wher should I put what snippet where?

MV*/


Look for this
HERE IT IS !!
in your code below

BTW: What or who are/is "general quaters"?

[quote name='mvalor' date='7 Apr, 2009 - 08:12 PM' post='598425']
I cannot see where the "{" is missing in my program, and keep getting: error C2447: '{' : missing function header (old-style formal list?), can anyone find this for me?

[code]
/* 
It's July 1988 and the Battleship USS NEW JERSEY approches a South Korean land target for a 
firing demonstration and prepares a 16" 50 Caliber firing exercise to clear out imaginary enemy ground infantry. 
This would be the dialog between fire control and Turret #2 during the naval exercise.
*/
#include <iostream>
#include <cmath>
using namespace std;

/*FUNCTION DECLARATION*/
float distance (double x1, double y1, double x2, double y2);  //Distance function prototype.

int main ()
{
/*FUNCTION CALL*/
double x1; 
double y1;
double x2;
double y2;

//Entering coordinates
cout << "GENERAL QUATERS, GENERAL QUATERS ALL HANDS MAN THEIR BATTLE STATIONS!" "\n";								//Bridge announcement
cout << "SET CONDITION ZEBRA! \n";																					//Bridge announcement
cout << "SMOKING LAMP IS OUT! \n";																					//Bridge announcement
cout << "\n\n";
cout << "Turret 2, ground target sighted, elevation 37 degrees by 65 degrees relative... \n";						//Fire control to Turret
cout << "Ready 3 gun manual bracketed salvo, HC Frag Proximity full charge rounds...confirm.\n";					//Fire control to Turret
cout << "\n\n";
cout << "Fire Control, 3 gun bracketed salvo, HC Frag Proximity full charge rounds load confirmed aye.\n";			//Turret to Fire Control
cout << "\n\n";
cout << "Turret 2, 3 gun bracketed salvo confirmed aye! Enter range coordinates and conrirm.   \n";					//Fire control to Turret
cout << "\n\n";
	cout << "Firing coordinate x1: ";
	cin  >> x1;
	cout << "Firing coordinate y1: ";
	cin  >> y1;
	cout << "Firing coordinate x1: ";
	cin  >> x2;
	cout << "Firing coordinate y2: ";
	cin  >> y2;
	cout << "\n";
	cout << "Range 1 coordinates : "<<"x1: "<<x1 <<"\t"<<"y1: "<<y1<<  "confirmed\n";
	cout << "Range 2 coordinates : "<<"x2: "<<x2 <<"\t"<<"y2: "<<y2<<  "confirmed\n";
	cout << "Fire Control, firing solution confirmed. \n";															//Turret to Fire Control
	cout << "\n\n"; 
	cout << "Turret 2, Fire solution confirmed aye! \n";															//Fire control to Turret

// Distance call
	distance(x1, y1, x2, y2);

system("pause");
	return 0; 
} 

//COMPUTING FIRING RANGE
/*FORMULA LAY-OUT
distance = sqrt( (x1 - x2)^2 + (y1 - y2)^2 )
x1-x2 = a
y1-y2 = b
D= sqrt(a)^2 + (a)^2
(a)^2 = z1 = pow(a,2.0)
(b)^2 = z2 = pow(b,2.0)
z1+z2 = w
D= sqrt(w)
*/
float distance (double x1, double y1, double x2, double y2)
{   
double a = x1-x2; 
double b = y1-y2;
double z1 = pow(a,2.0);
double z2 = pow(b,2.0);
double d = sqrt( z1 + z2);

cout << "Fire Control, Target locked on firing solution "  << d  << "knots at 37 degrees by 65 degrees relative\n";	//Turret to Fire Control
cout << "Fire Control, Turret 2 standing by for firing order sir! \n";												//Turret to Fire Control
cout << "\n";
cout << "Turret 2...Fire when ready Griddly...";																	//Fire control to Turret
cout << "\n";

return 0;
}

// ^^/>^^^/>^^^/>^^  HERE IT IS !! ^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>

{
char y=1; 
char n=0;

do {
	cout << "Fire control, 3 gun salvo fired and no mis-fires. Confirm reload. \n";
	cin >>	choice;

	if (choice == 'y' ||choice =='n')
		{
		cout << "Reload and wait for firing coordinates \n";
		}
		else if (choice =='n')
		{
		cout << "Negative, stand down and cease fire, finalize post fire proceedures and download all ammo. \n";
		}
} while (choice == 1);

return 0;
}





This post has been edited by janotte: 08 April 2009 - 03:12 AM

Was This Post Helpful? 0
  • +
  • -

#7 mvalor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 16-November 08

Re: Problem with do-while loop

Posted 08 April 2009 - 07:47 PM

View Postjanotte, on 8 Apr, 2009 - 02:10 AM, said:

View Postmvalor, on 7 Apr, 2009 - 08:33 PM, said:

Dear Kya,

I cant see it, its like I should be in front of me and I should be looking at it, but it eludes me.

Which return should I eliminate and wher should I put what snippet where?

MV*/


Look for this
HERE IT IS !!
in your code below

BTW: What or who are/is "general quaters"?

[quote name='mvalor' date='7 Apr, 2009 - 08:12 PM' post='598425']
I cannot see where the "{" is missing in my program, and keep getting: error C2447: '{' : missing function header (old-style formal list?), can anyone find this for me?

[code]
/* 
It's July 1988 and the Battleship USS NEW JERSEY approches a South Korean land target for a 
firing demonstration and prepares a 16" 50 Caliber firing exercise to clear out imaginary enemy ground infantry. 
This would be the dialog between fire control and Turret #2 during the naval exercise.
*/
#include <iostream>
#include <cmath>
using namespace std;

/*FUNCTION DECLARATION*/
float distance (double x1, double y1, double x2, double y2);  //Distance function prototype.

int main ()
{
/*FUNCTION CALL*/
double x1; 
double y1;
double x2;
double y2;

//Entering coordinates
cout << "GENERAL QUATERS, GENERAL QUATERS ALL HANDS MAN THEIR BATTLE STATIONS!" "\n";								//Bridge announcement
cout << "SET CONDITION ZEBRA! \n";																					//Bridge announcement
cout << "SMOKING LAMP IS OUT! \n";																					//Bridge announcement
cout << "\n\n";
cout << "Turret 2, ground target sighted, elevation 37 degrees by 65 degrees relative... \n";						//Fire control to Turret
cout << "Ready 3 gun manual bracketed salvo, HC Frag Proximity full charge rounds...confirm.\n";					//Fire control to Turret
cout << "\n\n";
cout << "Fire Control, 3 gun bracketed salvo, HC Frag Proximity full charge rounds load confirmed aye.\n";			//Turret to Fire Control
cout << "\n\n";
cout << "Turret 2, 3 gun bracketed salvo confirmed aye! Enter range coordinates and conrirm.   \n";					//Fire control to Turret
cout << "\n\n";
	cout << "Firing coordinate x1: ";
	cin  >> x1;
	cout << "Firing coordinate y1: ";
	cin  >> y1;
	cout << "Firing coordinate x1: ";
	cin  >> x2;
	cout << "Firing coordinate y2: ";
	cin  >> y2;
	cout << "\n";
	cout << "Range 1 coordinates : "<<"x1: "<<x1 <<"\t"<<"y1: "<<y1<<  "confirmed\n";
	cout << "Range 2 coordinates : "<<"x2: "<<x2 <<"\t"<<"y2: "<<y2<<  "confirmed\n";
	cout << "Fire Control, firing solution confirmed. \n";															//Turret to Fire Control
	cout << "\n\n"; 
	cout << "Turret 2, Fire solution confirmed aye! \n";															//Fire control to Turret

// Distance call
	distance(x1, y1, x2, y2);

system("pause");
	return 0; 
} 

//COMPUTING FIRING RANGE
/*FORMULA LAY-OUT
distance = sqrt( (x1 - x2)^2 + (y1 - y2)^2 )
x1-x2 = a
y1-y2 = b
D= sqrt(a)^2 + (a)^2
(a)^2 = z1 = pow(a,2.0)
(b)^2 = z2 = pow(b,2.0)
z1+z2 = w
D= sqrt(w)
*/
float distance (double x1, double y1, double x2, double y2)
{   
double a = x1-x2; 
double b = y1-y2;
double z1 = pow(a,2.0);
double z2 = pow(b,2.0);
double d = sqrt( z1 + z2);

cout << "Fire Control, Target locked on firing solution "  << d  << "knots at 37 degrees by 65 degrees relative\n";	//Turret to Fire Control
cout << "Fire Control, Turret 2 standing by for firing order sir! \n";												//Turret to Fire Control
cout << "\n";
cout << "Turret 2...Fire when ready Griddly...";																	//Fire control to Turret
cout << "\n";

return 0;
}

// ^^/>^^^/>^^^/>^^  HERE IT IS !! ^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>

{
char y=1; 
char n=0;

do {
	cout << "Fire control, 3 gun salvo fired and no mis-fires. Confirm reload. \n";
	cin >>	choice;

	if (choice == 'y' ||choice =='n')
		{
		cout << "Reload and wait for firing coordinates \n";
		}
		else if (choice =='n')
		{
		cout << "Negative, stand down and cease fire, finalize post fire proceedures and download all ammo. \n";
		}
} while (choice == 1);

return 0;
}






Thanks!, but I am still getting a missing "{" error right were I started. It is still driving me nuts.

By the way, I had served onboard this ship during this time and was a gunner for turret 2 and to let you know, General Quarters is the actual voice alert issued by the personel on the bridge which call for everyone to man there battle stations though out the ship.

Set condition zebra comes from a classification on how watertight security is through out the ship. Contition X-ray, Yoke and zebra are the three condtions, where each Letter is assignged to a particular hatchway and X being that most hatchways are open during normal working hours, Medium security is Yoke and Zebra meaning that all Hatchways are closed, making sure that if the ship is hit, it will remain bouyant.

Smoking Lamp is an old naval term coming from the days of the wooden ships where there was only one source of flame to light a pipe or cigar for smoke break (located at the main mast), which was turned off when a certain condtion applied in order not to have an accidental fire.

What I am attemption with this program is to be able to form a loop where one the guns were fired, if the captain had to order Fire control to reload, then the coordinates can be given again.

In actuality this sample game can be more complicated since we are dealing with different types of ammo, such as armour piercing, non-frag, frag, anti-personell (diamond) which released diamond shape grenades from the rear of the projectile, and so on. Also, I have to be able to take in account of the spead of the ship, gun angle elevation, wind speed, max range for full charge and reduced charge, etc.

But right now I am just trying to keep it simple.
Let me know if you have any ideas.
Many thanks for your input.

MV*/
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,068
  • Joined: 10-May 07

Re: Problem with do-while loop

Posted 08 April 2009 - 07:52 PM

View Postmvalor, on 8 Apr, 2009 - 08:47 PM, said:

Thanks!, but I am still getting a missing "{" error right were I started.


Think of it like this. Your functions are commands. If I were to give you directions, you would read the command, one at a time. By just having the brackets, you have no function!

For example :

output (int a) {
  printf("%i",a);
}

int main(void) {
  i=0;
  for(i=0;i<10;i++) {
	output(i);
  }
  return 0;
}



In the given example above, when we want to output the character in the loop, we call the function output.

But see how it defines both the name of the function as well as the argument that is passed to it? What you have in your code is just... floating code. No definition, no return type, no setup of any kind.

The compiler basically sees this code, passed return 0;, & is like... wtf do you want me to do with this?

The computer does as it's told. Not what it thinks you are up to. So you have to be exact sometimes, otherwise it freaks out.
Was This Post Helpful? 0
  • +
  • -

#9 mvalor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 16-November 08

Re: Problem with do-while loop

Posted 08 April 2009 - 09:14 PM

View Postno2pencil, on 8 Apr, 2009 - 06:52 PM, said:

View Postmvalor, on 8 Apr, 2009 - 08:47 PM, said:

Thanks!, but I am still getting a missing "{" error right were I started.


Think of it like this. Your functions are commands. If I were to give you directions, you would read the command, one at a time. By just having the brackets, you have no function!

For example :

output (int a) {
  printf("%i",a);
}

int main(void) {
  i=0;
  for(i=0;i<10;i++) {
	output(i);
  }
  return 0;
}



In the given example above, when we want to output the character in the loop, we call the function output.

But see how it defines both the name of the function as well as the argument that is passed to it? What you have in your code is just... floating code. No definition, no return type, no setup of any kind.

The compiler basically sees this code, passed return 0;, & is like... wtf do you want me to do with this?

The computer does as it's told. Not what it thinks you are up to. So you have to be exact sometimes, otherwise it freaks out.


No. 2 Pencil,
I do appreciate the advice, but Keep in mind that I am still just a rookie in this and fail to associate the definitions of some of the wording that you gave me for the example that I can relate to.

I tried to do a function based on "char choice( char y, char n)" but it does not work for me. I need to know how do I make this return back to the entering of the coordinates when a "y" is selected and how to terminate if a "n" is entered.

It does have to in a function form, with a do-while loop. Being that I am hard headed. I need for you to exp[lain within the same descriptions, then I will understand what I am doing wrong.

Please help me out with this.

MV*/
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Problem with do-while loop

Posted 09 April 2009 - 03:59 AM

OK.

Get your code open and go through from line one until the last line it and very carefully ensure all the indentations are as they should be.

This will help you find errors but if you don't it will help use find the error/s for you.

It's all about attention to detail.

Speaking of which.
I know what General Quarters means.
I don't know what "GENERAL QUATERS" means.
Think about what I might mean by that while you are going through your code line by line.

Once you have finished your code scrub if your still have a problem post your scrubbed code here.
Was This Post Helpful? 0
  • +
  • -

#11 mvalor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 16-November 08

Re: Problem with do-while loop

Posted 10 April 2009 - 08:19 AM

View Postjanotte, on 9 Apr, 2009 - 02:59 AM, said:

OK.

Get your code open and go through from line one until the last line it and very carefully ensure all the indentations are as they should be.

This will help you find errors but if you don't it will help use find the error/s for you.

It's all about attention to detail.

Speaking of which.
I know what General Quarters means.
I don't know what "GENERAL QUATERS" means.
Think about what I might mean by that while you are going through your code line by line.

Once you have finished your code scrub if your still have a problem post your scrubbed code here.


OOPS!
You are right on the mis-spelling General Quarters!
I will check line by line on the coding and keep you posted.

Thanks in advance

MV*/
Was This Post Helpful? 0
  • +
  • -

#12 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: Problem with do-while loop

Posted 10 April 2009 - 08:50 AM

Did anybody actually look at this code?

That last block:
{
char y=1;
char n=0;

do {
	cout << "Fire control, 3 gun salvo fired and no mis-fires. Confirm reload. \n";
	cin >>	choice;

	if (choice == 'y' || choice =='n')
		{
		cout << "Reload and wait for firing coordinates \n";
		}
		else if (choice =='n')
		{
		cout << "Negative, stand down and cease fire, finalize post fire proceedures and download all ammo. \n";
		}
} while (choice == 1);

return 0;
}


has no function name it is a block of code sitting out in the middle of nowhere.

You will need to give it a function name (preferably a verb) like:
  int getChoice( char choice)



and place the declaration of the function somewhere before the main() function ;)
Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Problem with do-while loop

Posted 10 April 2009 - 01:56 PM

View Postajwsurfer, on 10 Apr, 2009 - 07:50 AM, said:

Did anybody actually look at this code?


Yes we did.

The suggestions from KYA were based on the idea of incorporating the 'lost code' into main().

Your idea of making it a separate function and calling it from main() is a fine idea too.

There is more than one way to do it but everything in the thread has been about that block of dislocated code that you are pointing to so yes, you aren't the first one to see that as the problem.
Was This Post Helpful? 0
  • +
  • -

#14 mvalor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 16-November 08

Re: Problem with do-while loop

Posted 12 April 2009 - 06:13 PM

View Postjanotte, on 10 Apr, 2009 - 12:56 PM, said:

View Postajwsurfer, on 10 Apr, 2009 - 07:50 AM, said:

Did anybody actually look at this code?


Yes we did.

The suggestions from KYA were based on the idea of incorporating the 'lost code' into main().

Your idea of making it a separate function and calling it from main() is a fine idea too.

There is more than one way to do it but everything in the thread has been about that block of dislocated code that you are pointing to so yes, you aren't the first one to see that as the problem.



Dear Janotte,
I want to thank you for addressing the point of looking at details and appreciate that you are helping me work this out. The training is good, however, understanding functions is a little confusing for me right now. I have tried, looking through the theory, and all arrangements possible. (The book that I am using is the Herbert Schildt second edition).

Yes the program works for me as you will see however, it is not the way I want it to work, here is a modified version of the earlier program:

/* 
It's July 1988 and the Battleship USS NEW JERSEY approches a South Korean land target for a 
firing demonstration and prepares a 16" 50 Caliber firing exercise to clear out imaginary enemy ground infantry. 
This would be the dialog between fire control and Turret #2 during the naval exercise.
*/
//================================================================================================
#include <iostream>
#include <cmath>
using namespace std;
//================================================================================================
/*FUNCTION DECLARATION*/
float distance (double x1, double y1, double x2, double y2);  //Distance function prototype.
int yesno();
//===============================================================================================
int main ()

{
double x1; 
double y1;
double x2;
double y2;
double elevation;
double rb;  //relative bearing
int start;	// Selecting Turret Number
//Entering coordinates
cout << "GENERAL QUARTERS, GENERAL QUARTERS ALL HANDS MAN THEIR BATTLE STATIONS!" "\n";								//Bridge announcement
cout << "SET CONDITION ZEBRA! \n";																					//Bridge announcement
cout << "SMOKING LAMP IS OUT! \n";																					//Bridge announcement
cout << "\n\n";
cout << "Bridge to Fire control. Ground target sighted...obtaining target data. \n";						
cout << "Ready 3 gun manual bracketed salvo, HC Frag Proximity full charge rounds...confirm.\n";					//Fire control to Turret
cout << "\n\n";
cout << "Fire Control to Bridge, 3 gun bracketed salvo, HC Frag Proximity full charge rounds load confirm aye.\n";			//Turret to Fire Control
cout << "\n\n";
cout << "Fire Control to Gunnery Stations: Ready 16in, 3 gun bracketed salvo load and confirm!  \n";					//Fire control to Turret
cout << "Fire Control to Gunnery Stations: target data to computer. Confirm.\n\n";
cout << "Fire Control to Gunnery Stations: Selecting gun station.\n";
cout << "Entering Turret Number:  \n";
cin >> start;
cout << "\n";

		// see if it is between 1 and 3
		{
		if ( start < 1 || start > 3)
		{
			cout << "Error Turret number!\n\n";
		}
		else
	{
	cout << "Turret  " << start << "  loaded and ready. \n";
	cout <<"Fire control, target data confirmed entered!\n";

	cout << "Turret  " << start << " entering data now: \n";
	cout << "Recieving gun elevation and relative bearing.\n";
	cout << "Entering gun elevation...\n";
	cin >>	elevation;
	cout << "Gun elevation set at:" <<  elevation <<" degrees.\n";
	cout << "Entering Relative bearing...\n";
	cin >>	rb;
	cout <<"Rotating guns to:" << rb  << "degrees relative!\n\n";
	cout << "Firing coordinate x1:  ";
	cin  >> x1;
	cout << "Firing coordinate y1:  ";
	cin  >> y1;
	cout << "Firing coordinate x1:  ";
	cin  >> x2;
	cout << "Firing coordinate y2:  ";
	cin  >> y2;
	cout << "\n";
	cout << "Range 1 coordinates : "<<"x1: "<<x1 <<"\t"<<"y1: "<<y1<<  " confirmed\n";
	cout << "Range 2 coordinates : "<<"x2: "<<x2 <<"\t"<<"y2: "<<y2<<  " confirmed\n";
	cout << "Turret" << start << " to Fire Control, firing solution confirmed. \n";															//Turret to Fire Control
	cout << "\n\n"; 
	cout << "Turret " << start << "Fire solution confirmed aye! \n";															//Fire control to Turret

// Distance call
	distance(x1, y1, x2, y2);
} // end else place here 2:23pm

} 
}
//=============================================================================================================
//COMPUTING FIRING RANGE
/*FORMULA LAY-OUT
distance = sqrt( (x1 - x2)^2 + (y1 - y2)^2 )
x1-x2 = a
y1-y2 = b
D= sqrt(a)^2 + (a)^2
(a)^2 = z1 = pow(a,2.0)
(b)^2 = z2 = pow(b,2.0)
z1+z2 = w
D= sqrt(w)
*/

float distance (double x1, double y1, double x2, double y2)
{   
double a = x1-x2; 
double b = y1-y2;
double z1 = pow(a,2.0);
double z2 = pow(b,2.0);
double d = sqrt( z1 + z2);
cout << "Fire Control, Target at "  << d  << " knots, standing by for firing order. \n";	
cout << "\n";
cout << "Fire Control to Turret...Fire when ready Griddly...\n";																	//Fire control to Turret
cout << "\n";
cout << "B O O M!!!!\n\n";

//==============================================================================================================
//Reloading orders to Turret
{
char yesno = 'y';	// ask user yes or no - start at yes
int i;
while ( yesno == 'y'){
if(i>0)  cout << "Reload and wait for firing coordinates \n";
i++;
cout << "Fire control, Turret reports one 3 gun salvo fired and no mis-fires. Confirm reload. \n";
cin >> yesno;
}
cout << "Negative, cease fire, cease fire, secure all guns and stand by. \n";
}

system("pause");
	return 0; 

}




Yes, notice that I corrected the mispelling od "quarters"...thanks, I can blame this on my wide fingers and poor glasses.

What I want it to do is when Fire control gets permision from the bridge to reload and fire, the program should take me back to the Turret selection andthrough the sequuence of firing until it is given a cease fire.

Help me out on this and tell me what am I doing wrong?

MV*/
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Problem with do-while loop

Posted 12 April 2009 - 08:17 PM

I am having a bit of trouble seeing the flow of your code.

More about me being tired than your code I suspect.

From what I can understand from your question you want to program to keep looping through the reload / fire sequence until the bridge denies permission?

I think it will be trivial to do but it's a bit hard for me to see where the loop goes.

Can I suggest you make things a little easier on yourself by breaking your program down into more functions. You have done some of this but you can go further.

I would pull all the stuff about general quarters and target acquisition and the like into a series of functions so main() ends up being a short series of calls to functions.

More like:
int main() {
	generalQuarters();
	targetFound();
	int selectedTurret = selectTurret();
	layGuns();
	setFireCoords();
	fireGuns();
	bool reload = reloadPermission();
...
   return 0;
}



Breaking it down into little chunks will let you control each piece far more easily (much like the co-ordinated sequence of calls and responses needed to have the guns fired in your example makes each step simple and foolproof a lot of calls to smallish functions makes things safe and robust.

Then it should be far easier to work out which chunks belong in a fire/reload/fire again loop and it will be fairly trivial to enclose them in a do/while() loop (or whatever type of loop you think is best).

EDIT: Fixed code tags. How embarrassing!!

This post has been edited by janotte: 12 April 2009 - 10:44 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2