C++ :: Structures - How To Update Space Ship Position In Array
Apr 7, 2013
I want to do this but do not know exactly how to do it:
Create an array of space ship objects and write a program that continually updates their positions until they all go off the screen. Assume that the size of the screen is 1024 pixels by 768 pixels.
I have begun to write a bit code but the problem is when you want to see the space ship moving step for step on the screen I do not know how to create the ship visible so you can see it is moving. Here's my code so far
Code:
#include <iostream>
using namespace std;
struct SpaceShips
{
int x_coordinate;
int y_coordinate;
[Code ......
View 4 Replies
ADVERTISEMENT
Apr 9, 2014
I am creating a SpaceShip class, which has arrays for weapon names and damage, and variables for shields power, hull, engine power, and mass. I created two instances of the 'SpaceShip' class, 'ScoutShip', and 'Cruser'. When one of them calls the 'FireWeapons' function, it has the other call the 'TakeDamage' function, but for some reason the 'sheildPower' resets after 'TakeDamage' ends.
#include <iostream>
#include <vector>
using namespace std;
class SpaceShip {
[Code] ....
View 3 Replies
View Related
Mar 4, 2013
What I have to do is write a small program in C++ to parse the symbols that are used on 5 different lines of text in each position until position 30 is reached on each line. The goal of the parsing program is to interpret the symbols (characters), if there are any per each position, on the 5 lines of text in order to output the actual data that the group of symbols represents.
My question for is this: Is there anything special from a C++ environment that should go in to something like this outside of using standard stuff like the math associated with the search algorithm that has to happen here? The symbols are located in a file, so I know I have to include "iostream" and a few other headers. But outside of header inclusions and the code necessary to iterate and streamline the search and interpretation process, am I missing anything special that I couldn't otherwise find through simple google searches?
View 6 Replies
View Related
Jun 13, 2013
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
int Superior[4][6]={0};
int Economy[6][8]={0};
[Code] ....
What i want is key-in 2 diff seat ID and age and cout this 2 diff seat ID and age.
View 2 Replies
View Related
Dec 9, 2014
My program takes the values from one array and searches for their index position in another array (linear search algorithm). This is an example of the issue im having(its not part of the actual code below)
a[]={1,2,3,4,5,6}
Arr[]={1,2,2,3,4,5}
If it finds 1 in arr, it returns 0, which is fine but if it finds 2 in arr it return 1 and 1 instead of 1 and 2.
for (int q=0; q=size2;q++) {
int rs=secfunc(array1;size1;array2[q])
if(rs>=0) {
cout<<rs << "";
[Code] .....
View 4 Replies
View Related
Oct 12, 2013
In my code I have a template class.
In the contructor, I create a new array arrray.
A pointer to this array is saved as an attribute.
The purpose of this attribute is that I should be able to call the array from within another function be typing *p or *(p+1) etc.
The only thing I forgot is that the array doesn't exist anymore when the constructor is done, right?
This means the space is no longer reserved for this array and the space is used for other things.
Is there a way to keep the array space reserved (even when the array does not exist anymore at the end of the function)?
View 1 Replies
View Related
Apr 29, 2014
I have a character Array 'Hello'
I want to add a space in whatever space the users chooses.
"Where do you want to add space? --> " 3 String after space is added --> 'Hel lo
View 4 Replies
View Related
Dec 24, 2013
So for a project I'm working on, I'm using an array and generating it's values randomly but unique. Currently I'm working on a 3X3 array and the generated values are in the range from 1-9. So I wrote a function that will tell me the position of the cell whose value is 9. This is the function I wrote:
Code: void Llogaritje1(int t[3][3],int &i, int &j){
int y,l;
for(y=0;y<3;y++){
for(l=0;l<3;l++){
if(t[y][l]==9){
i=y;
j=l;
break;
}
}if(t[y][l]==9) break;
}
}
But it doesn't work on all cells. Seems like at cells t[1][0] and t[2][0] the values that i and j take are 0 0 since when I print them after excecuting the function that's what it returns. I really don't understand why.
View 4 Replies
View Related
Jul 25, 2013
The assignment is to read a data file with a list of names, capitalize the first character of the first and last name by making an array and subtracting 32 from the character, and then outputting the names onto the screen. So far I have:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("Names.txt");
[Code] ....
this allows me to read a name, and capitalize the first letter of the first name. How to capitalize the first letter of the last name.
View 2 Replies
View Related
Dec 9, 2014
My program takes the values from one array and searches for their index position in another array(linear search algorithm). This is an example of the issue I am having(its not part of the actual code below) :
a[]={1,2,3,4,5,6}
Arr[]={1,2,2,3,4,5}
If it finds 1 in arr, it returns 0, which is fine but if it finds 2 in arr it return 1 and 1 instead of 1 and 2. any thoughts on how to fix this?
Code:
for (int q=0; q=size2;q++) {
int rs=secfunc(array1;size1;array2[q])
if(rs>=0) {
cout<<rs << "
";
[Code] .....
View 12 Replies
View Related
Apr 25, 2013
This for loop replaces the stars ******** in an array that contains a word to be guessed with the correct letter (c) that the user inputs if the word contains that letter.
Problem: After every guess the same hidden string of stars ******* is displayed instead of ex: ***W**** (assuming W was entered by the user)
How can I update the old ******** string in the Stars array with the new string that includes the correct letters chosen, so after every correct guess at a letter in the word a new string is displayed including the correct letters chosen?
I'm pretty sure I have to use strcpy but not sure how to implement it using a loop.
Code:
for(i = 0; i < strlen(unscrambledWord); i++) {
if(unscrambledWord [i] == c) {
Stars[i] = c;
} }
View 1 Replies
View Related
May 14, 2013
How do you find the amount of space in a char array?
View 11 Replies
View Related
Feb 3, 2015
Getting back into programming after a few years off and a bit rusty.
My question is: Is this going to initialize the size of the vector array's position and color properly?
#include <GLFW/glfw3.h>
#include <vector>
class TerrainClass {
private:
struct VertexType {
std::vector<float> position[3];
[Code]...
View 6 Replies
View Related
Aug 9, 2013
How do I store pointers to a struct in an array ? I am using sprintf to concatenate some values together and then output it to an array in its 1st argument. A portion of my code is shown below.
Code:
int count = 0;
struct addx {
int a;
[Code].....
View 1 Replies
View Related
May 4, 2013
So i have a program with the structure
Code:
char record[20];
typedef struct{
int id;
char title[20];
char subject[20];
char faculty [20];
int level;
char fname[25];
}report;
int recno,i;
report list[100];
I need to store the hundred records into a file. What are the functions and elements i should use/introduce.
View 2 Replies
View Related
Feb 25, 2014
How to initialize this array of structures in my code
struct CustomerAddress; {
string street;
string city;
string state;
int zip;
[Code] ....
I am going to be using a boolean variable to mark whether or not a specific field has had data entered into it. I figure the best way to do that is to initialize all the elements of the structures to 0. However, with strings and with the nested structure, I'm not sure how to do this.
View 6 Replies
View Related
Jun 9, 2013
I have to write a program that reads from a text file, which contains a list of stock hourly prices and the company names. Something like this:
78.52 82.56 75.10 71.97 Water Company
22.40 25.68 21.37 22.96 Mega Shipping Inc
There's suppose to one array of companies, where each company will be kept in a structure that contains: a pointer for the name, an array of the prices, and the average price for the day. The structures will be kept in an array of structures.
My question is, how do I read the data from the file and put the data from each line into the next structure in the array of structures? I read the numbers in fine. I just use:
Code: fscanf(fp, "%f", &companyAry[i].hourlyPrices[j]);
But my program crashes when I try to read the name.
Code: fscanf(fp, "%[^]", &companyAry[i].companyName);
I'm thinking it has something to do with the fact the companyName is a pointer.
My structure looks like this:
Code:
typedef struct {
char *companyName;
float hourlyPrices[NUM_PRICES];
float avgPrice;
}
COMPANY;
View 8 Replies
View Related
Jul 21, 2014
I have an array of structures with structure def that looks like.
Code:
struct {
char * name;
char * school;
int age;
}
there are multiple people with same name but different ages so i want to list them like.
Name.
age 1
age 2 and so on
The array is sorted by name already.
View 3 Replies
View Related
Feb 26, 2013
so im trying to read in the name of a vendor into an array of structures. and write the name into a file.
#include <iostream>
#include <cstring>
#include <cctype>
#include <fstream>
using namespace std;
struct booth
[code]....
View 5 Replies
View Related
Apr 4, 2013
Any example to delete an element in an array of structure?Is it same as deleting an element in an array.ie like this....???
// to delete an element in an array
#include<iostream.h>
#include<conio.h> // for clrscr() and getch()
void main() {
clrscr();
int a[50],n,t,index,i;
[Code] .....
View 1 Replies
View Related
Dec 4, 2014
I am making a basic music library that stores information rather than files. I need to read from a .txt file the artist's name, the album name, the song name, and the song length. What I am confused about is storing the information in an array of Artist structures.
struct Song{
string songName;
int songLength;
};
struct Album{
[Code] ...
How to read the information into an array of Artists. Also, how would I be able to figure out if the artist already exists in the array when adding a song and if the artist does, add the album under the existing artist.This is how I am reading in the info:
Artist newArtist;
Album newAlbum;
Song newSong;
ifstream inF("library.txt");
while(!inF.eof()) {
[Code] ....
View 1 Replies
View Related
Feb 27, 2013
Suppose I have the following structure
struct b
{
char fullname[100];
char title[100];
bopname
};
and i declare a pointer to the struct as follows
b* bp;
and at runtime,
bp = new b[5];
In the end, is the statement delete[ ] bp enough to release the dynamically allocated memory. Or do i need to separately delete bp[0],bp[1]...
Does delete[ ] bp indicate that the array[ ] pointed by bp has to be deleted?? [I am confused as to how this happens, since the pointer only points at the 1st struct address]
View 2 Replies
View Related
Aug 26, 2014
I somewhere read "You cannot initialize a structure like that at run time."
Example:
struct item_info {
char itemname[15];
int quantity;
float retail;
float wholesale;
[Code] ....
But if you want to assign values at run time then you have to do it manually like:
strcpy(item[0].itemname, "rice");
item[0].quantity = 10;
item[0].retail = 40;
item[0].wholesale = 30;
I tried in internet but am unable to know the differences. I want to know the difference between those two in terms of run time and compile time. Explanation required also for below one. Is this run time or compile time? How does we actually decide which is run time and which is compile time!
struct item_info {
char itemname[15];
int quantity;
float retail;
float wholesale;
//int quatityonorder;
[Code] ....
View 3 Replies
View Related
Nov 3, 2014
So first I have to display a 2D array with all 0s, which is pretty easy.
#include <iostream>
using namespace std;
int main (){
int array[5][5];
for(int a=0; a<5; a++){
for(int b=0; b<5; b++){
array[a][b] = 0;
[Code] ....
So this displays
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Next, this is where it gets confusing. I have to create a virtual creature by storing a letter into a random position in the array (the array can be up to 20x20 in size). Then make a function that searches the array for creatures, so it would search for that character. When it finds a creature, it should randomly decide to either move the creature to an adjacent position, or have it stay where it is. After, it should ask the user to create a new creature, or quit.
So how would I go about adding & modifying the current code to achieve what is listed above?
View 2 Replies
View Related
Nov 2, 2014
I'm reading the following file:
Osgood,Marcus 298542123 CHM FR mosgood@whatever.edu
Cronk,Melissa 873489021 BIO SR mcronk@whatever.edu
Pry,Seth 349908431 MTH SO spry@whatever.edu
Langlais,Susan 783323545 ME SR slanglais@whatever.edu
Davis,Nicole 987543345 PHY FR ndavis@whatever.edu
It's supposed to split it up into name, ID number, major, year, and email. The file reads it without any errors, and assigns name to the first part of the structure. However, ID gets assigned the ID, major, year, and email. Then Major gets assigned major, year, and email. Year gets assigned year and email, while email just gets assigned email. I don't know if it has something to do with the loop. For example, this is what I get what I print just the name and the ID.
Cronk,Melissa 873489021BIOSRmcronk@whatever.edu
Pry,Seth 349908431MTHSOspry@whatever.edu
Langlais,Susan 783323545ME
Davis,Nicole 987543345 PHYFRndavis@whatever.edu
Anyway. This is my function code for reading the array. I have it printing the ID number just to see if I can catch the errors earlier:
Code:
#include <stdio.h>
#include <string.h>
#include "functions.h"
void read_data(char filename[], studentinfo_t s[], int *size) {
FILE *infilep;
int countstudents = 0;
infilep = fopen("student_data", "r");
[Code].....
the name is reading correctly, but not anything else!
View 1 Replies
View Related
Jul 27, 2014
I have to allocate memory for an array of structures, and my structure looks as following:
Code:
typedef struct {
char *name;
Uint *start_pos;
Uint len;
}
example_struct;
And now I want to allocate memory, for a variable number (so an array) of example_struct, so I first do:
Code:
example struct *all_struct;
int total_num = 3;
//will be set somehow, but for the example I set it on 3 all_struct = malloc (sizeof(example_struct) * total_num);
And now, as far as I now, I will have to allocate for each field of the structure memory, in order to be able to use it later. But I have problem at this point, a problem of understanding:
- I just allocated memory for 3 structures, but don't I have to allocate then memory for each structure separately, or can I just now allocate the fields like this:
Code: all_struct[0].name = malloc.....
But if yes, why the hell this works...
View 10 Replies
View Related