GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Dec-2005, 16:49
AdamRA4 AdamRA4 is offline
New Member
 
Join Date: Dec 2005
Posts: 7
AdamRA4 is on a distinguished road

Need Quick Help!!


I have a program that I need to make and it has structs. I keep getting a 'illegal use of this type as an expression' error. Please help, any help is very appreciated!

CPP / C++ / C Code:

#include <iostream>
#include <cstdlib>

using namespace std;


struct Source
{
	float latitude1;
	float longitude1;
};
struct Travel
{
	float time;
	float distance;
	float altitude;
};
struct Target
{
	float latitude2;
	float longitude2;
};
struct Projectile
{
	float speed;
	float azimuthal;
	float equatorial;

	Source latitude1;
	Source longitude1;

	Travel time;
	Travel distance;
	Travel altitude;

	Target latitude2;
	Target longitude2;
};


//Projectile ReadEntry();
//int DisplayEntry (Projectile);


void main()
{

	cout << "Projectile Calculator\n\n";
	cout << "Please enter the launch speed: ";
    cin.getline(Projectile.speed,20);
	cout << endl;
//	cout << "\n\n" << Projectile.speed;
}


  #2  
Old 02-Dec-2005, 17:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need Quick Help!!


Quote:
Originally Posted by AdamRA4
I have a program that I need to make and it has structs. I keep getting a 'illegal use of this type as an expression' error.
CPP / C++ / C Code:
void main()
{

	cout << "Projectile Calculator\n\n";
	cout << "Please enter the launch speed: ";
    cin.getline(Projectile.speed,20);
	cout << endl;
//	cout << "\n\n" << Projectile.speed;
}



The definition of Projectile tells the compiler that Projectile is a struct. You don't have any of them until your program declares variables of that type.
Once you declare a variable of type Projectile, then you can use access its members.

Also, since Projectile.speed is a float, you must have an input function that works with floats. cin.getline(...,...) works with strings, not floats.

You could try something the following main() function, based on your code, with your definitions. It's a start, I hope.

CPP / C++ / C Code:
int main()
{
    Projectile my_projectile;

    cout << "Projectile Calculator\n\n";
    cout << "Please enter the launch speed: ";

    cin >> my_projectile.speed;

    cout << "\n\nmy_projectile.speed = " << my_projectile.speed << endl;

    return 0;
}

Regards,

Dave
  #3  
Old 02-Dec-2005, 17:09
AdamRA4 AdamRA4 is offline
New Member
 
Join Date: Dec 2005
Posts: 7
AdamRA4 is on a distinguished road

Re: Need Quick Help!!


Thanks, i will try it.

Thanks a million, that helped me out, I just couldnt decipher what it wanted me to do.
  #4  
Old 02-Dec-2005, 17:14
AdamRA4 AdamRA4 is offline
New Member
 
Join Date: Dec 2005
Posts: 7
AdamRA4 is on a distinguished road

Re: Need Quick Help!!


instead of my_projectile I used projectileData.

so what if I wanted to cin a Travel distance, would it be

CPP / C++ / C Code:
projectileData.Travel.distance
??
  #5  
Old 02-Dec-2005, 17:34
AdamRA4 AdamRA4 is offline
New Member
 
Join Date: Dec 2005
Posts: 7
AdamRA4 is on a distinguished road

Re: Need Quick Help!!


I am getting the same errors with the substructs, how do I do those?

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;


struct Source
{
	float latitude1;
	float longitude1;
};
struct Travel
{
	float time;
	float distance;
	float altitude;
};
struct Target
{
	float latitude2;
	float longitude2;
};
struct Projectile
{
	float speed;
	float azimuthal;
	float equatorial;

	Source latitude1;
	Source longitude1;

	Travel time;
	Travel distance;
	Travel altitude;

	Target latitude2;
	Target longitude2;
};



bool GetFloat(float *);
bool validNum = true;


void main()
{
	Projectile projectileData;

	cout << "Projectile Calculator\n\n";
	cout << "Please enter the launch speed: ";
	validNum = GetFloat(&projectileData.speed);
//	cin >> projectileData.speed;
	cout << endl;

	cout << "Please enter the launch azimuthal angle: ";
    cin >> projectileData.azimuthal;
	cout << endl;

	cout << "Please enter the launch equatorial angle: ";
    cin >> projectileData.equatorial;
	cout << endl;

	cout << "Please enter the source latitude: ";
    cin >> projectileData.Source.latitude1;
	cout << endl;

	cout << "Please enter the source longitude: ";
    cin >> projectileData.longitude1;
	cout << endl;



	projectileData.time = 2.0*projectileData.speed*sin(projectileData.azimuthal)/9.81;

	projectileData.distance = projectileData.speed*cos(projectileData.azimuthal)*projectileData.time;

	projectileData.altitude = 9.81*projectileData.time*projectileData.time/2.0;

	projectileData.latitude2 = projectileData.latitude1 + projectileData.distance*sin(projectileData.equatorial);

	projectileData.longitude2 = projectileData.longitude1 + projectileData.distance*cos(projectileData.equatorial);


	cout << "The projectile is launched with a speed of: " << projectileData.speed << endl;
	cout << "                     an azimuthal angle of: " << projectileData.azimuthal << endl;
	cout << "                     an equtorial angle of: " << projectileData.equatorial << endl;
	cout << "          The projectile travels a time of: " << Projectile.time << endl;
	cout << "                        over a distance of: " << Projectile.distance << endl;
	cout << "                    reaches an altitude of: " << Projectile.altitude << endl;
	cout << "               It departs from coordinates: " << Projectile.latitude1 << ", " << Projectile.longitude1 << endl;
	cout << "                and arrives at coordinates: " << Projectile.latitude2 << ", " << Projectile.longitude2 << endl;



}


bool GetFloat(float *value)
{
	char string[20];
	bool validNum = true;
	int i;

	cin >> projectileData.speed;

//	cin.getline(string,20);

	i = 0;
	do
	{
		if ((string[i] < '0') || (string[i] > '9'))
		{
			validNum = false;
		}
		i++;
	} while (i < strlen(string) && validNum);

	if (validNum)
	{
		*value = atoi(string);
	}

	return validNum;
}


void DisplayEntry(Projectile)
{
	cout << "The projectile is launched with a speed of: " << Projectile.speed << endl;
	cout << "                     an azimuthal angle of: " << Projectile.azimuthal << endl;
	cout << "                     an equtorial angle of: " << Projectile.equatorial << endl;
	cout << "          The projectile travels a time of: " << Projectile.speed << endl;
	cout << "                        over a distance of: " << Projectile.time << endl;
	cout << "                    reaches an altitude of: " << Projectile.altitude << endl;
	cout << "               It departs from coordinates: " << Projectile.latitude1 << ", " << Projectile.longitude1 << endl;
	cout << "                and arrives at coordinates: " << Projectile.latitude2 << ", " << Projectile.longitude2 << endl;
}
  #6  
Old 02-Dec-2005, 17:49
AdamRA4 AdamRA4 is offline
New Member
 
Join Date: Dec 2005
Posts: 7
AdamRA4 is on a distinguished road

Re: Need Quick Help!!


Also I just noticed that my DisplayEntry was their twice, I deleted the entries from the main() function.
  #7  
Old 02-Dec-2005, 18:13
AdamRA4 AdamRA4 is offline
New Member
 
Join Date: Dec 2005
Posts: 7
AdamRA4 is on a distinguished road

Re: Need Quick Help!!


Updated my code, I only get about 18 errors, most of which are the 'illegal use of this type as an expression' error with the SubStructs. This is an assignment which I have to turn-in in about 10 min. If I could figure out how to get rid of the 'illegal use of this type as an expression' errors, I would help greatly.

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;


struct Source
{
	float latitude1;
	float longitude1;
};
struct Travel
{
	float time;
	float distance;
	float altitude;
};
struct Target
{
	float latitude2;
	float longitude2;
};
struct Projectile
{
	float speed;
	float azimuthal;
	float equatorial;

	Source latitude1;
	Source longitude1;

	Travel time;
	Travel distance;
	Travel altitude;

	Target latitude2;
	Target longitude2;
};



bool GetFloat(float *);
bool validNum = true;
Projectile projectileData;

void main()
{
	cout << "Projectile Calculator\n\n";

	do  //reads the Projectile Speed
	{
		cout << "Please enter the launch speed: ";
		validNum = GetFloat(&projectileData.speed);
		if (validNum==false)
		{
			cout << "Invalid number. Try again...\n";
		}
	} while (validNum==false);


	cout << "Please enter the launch speed: ";
	validNum = GetFloat(&projectileData.speed);
	cout << endl;

	cout << "Please enter the launch azimuthal angle: ";  //reads the Projectile azimuthal angle
    cin >> projectileData.azimuthal;
	cout << endl;

	cout << "Please enter the launch equatorial angle: ";  //reads the Projectile equatorial angle
    cin >> projectileData.equatorial;
	cout << endl;

	cout << "Please enter the source latitude: ";  //reads the Projectile source latitude
    cin >> projectileData.latitude1;
	cout << endl;

	cout << "Please enter the source longitude: ";  //reads the Projectile source longitude
    cin >> projectileData.longitude1;
	cout << endl;



	projectileData.time = 2.0*projectileData.speed*sin(projectileData.azimuthal)/9.81;  //calculation for Projectile time

	projectileData.distance = projectileData.speed*cos(projectileData.azimuthal)*projectileData.time;  //calculation for Projectile distance

	projectileData.altitude = 9.81*projectileData.time*projectileData.time/2.0;  //calculation for Projectile altitude

	projectileData.latitude2 = projectileData.latitude1 + projectileData.distance*sin(projectileData.equatorial);  //calculation for Projectile target latitude

	projectileData.longitude2 = projectileData.longitude1 + projectileData.distance*cos(projectileData.equatorial);  //calculation for Projectile target longitude


}


bool GetFloat(float *value)
{
	char string[20];
	bool validNum = true;
	int i;

	cin >> projectileData.speed;

	i = 0;
	do
	{
		if ((string[i] < '0') || (string[i] > '9'))
		{
			validNum = false;
		}
		i++;
	} while (i < strlen(string) && validNum);

	if (validNum)
	{
		validNum  = true;
	}

	return validNum;
}


void DisplayEntry(Projectile)  //Display's the final results of the calculations and such
{
	cout << "The projectile is launched with a speed of: " << Projectile.speed << endl;
	cout << "                     an azimuthal angle of: " << Projectile.azimuthal << endl;
	cout << "                     an equtorial angle of: " << Projectile.equatorial << endl;
	cout << "          The projectile travels a time of: " << Projectile.speed << endl;
	cout << "                        over a distance of: " << Projectile.time << endl;
	cout << "                    reaches an altitude of: " << Projectile.altitude << endl;
	cout << "               It departs from coordinates: " << Projectile.latitude1 << ", " << Projectile.longitude1 << endl;
	cout << "                and arrives at coordinates: " << Projectile.latitude2 << ", " << Projectile.longitude2 << endl;
}
  #8  
Old 02-Dec-2005, 20:29
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Need Quick Help!!


Hi Adam,

First, Change the Source definition like this:
CPP / C++ / C Code:
struct Position
{
    float latitude;
    float longitude;
};
Change the name to position because we can use position to indicate both source and target.

Remove the target struct definition.

Then, change the projectile definition like this:
CPP / C++ / C Code:
struct Projectile
{
    float speed;
    float azimuthal;
    float equatorial;

    Position Source;   // Source is a position
    Position Target;   // Target is a position

    Travel Path;
};

Here are the variables that you should use in your functions:
Code:
projectileData.speed projectileData.azimuthal projectileData.equatorial projectileData.Source.latitude projectileData.Source.longitude projectileData.Target.latitude projectileData.Target.longitude projectileData.Path.time projectileData.Path.distance projectileData.Path.altitude

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #9  
Old 04-Dec-2005, 01:23
AdamRA4 AdamRA4 is offline
New Member
 
Join Date: Dec 2005
Posts: 7
AdamRA4 is on a distinguished road

Re: Need Quick Help!!


thanks, I will try that
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick, Insertion, and Partition silicon C++ Forum 0 18-May-2005 21:49
Merge and Heap...which is really faster silicon C++ Forum 0 10-May-2005 14:46
Google toolbar 2.0 - Has popup blocker, Autofill and quick weblog posting features jrobbio Computer Software Forum - Windows 11 26-Jun-2003 09:20
Dave's Quick Search Taskbar Toolbar Deskbar jrobbio Computer Software Forum - Windows 2 03-Apr-2003 08:42
A quick way to CHMOD jrobbio Web Hosting Forum 0 16-Feb-2003 14:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:45.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.