Please help with stuck

Input
The input to your program is a series of lines, each with three values. The first value is a celestial body- either a capital P (planet), M (moon), S(star), or G (galaxy). The next two values are integers representing the X and Y coordinates of the celestial body. X values range from 1 to 30, Y values from 1 to 15. When an F is read as the first character of the input line, that signals the end of the input.

Output
The output of your program is a 30x15 grid dispalying the celestial bodies at their locations. If a location doesn't have a celestial boday, the output would be a period "."

If two or more bodies occupy the same location, then the number of objects at that location should be displayed (you can assume that that number will never exceed 9).

The output of the grid should be oriented with (1,1) in the lower left, and (30, 15) at the upper right. The X axis is along the horizontal, and the Y axis is along the vertical. Your grid should be displayed with no additional spaces between characters, and no blank lines between the rows of your grid.

I don't even know where to begin
Last edited on
How about an array?

You have two choices (at least what I can think off top of my head)

Either take a normal 3 by 15 array of characters to get the input of celestial body
or
Take two arrays one of 15 (character) and another of 2 by 15 (integer)

Once you take input from user (or file I suppose) you'll have a pretty good idea to what to do next....
Or we can give you some


HOPE IT HELPS
Yeah I'm still having trouble with arrays so I'm watching videos to figure it out
Watch the videos and put up your code... we'll help :)
The only code i have so far is this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main() {
    const int HEIGHT = 15, WIDTH =30;
    string a[HEIGHT][WIDTH];
    int x, y, i, j;





    for (i =0; i <HEIGHT; i++){
            for (j=0;j<WIDTH; j++){
              a[i][j] = "*";
               cout << a[i][j];
            }
    cout <<endl;
    }

            return 0;
}



so i get this
******************************
******************************
******************************
******************************
******************************
******************************
******************************
******************************
******************************
******************************
******************************
******************************
******************************
******************************
******************************


I need mine to look like this

.....P.......................S
..............................
..............................
....G.........................
..............................
..............................
..............................
..............................
..............................
.....................3........
..............................
..............................
.........M....................
..............................
M.............................
To read a file: http://www.cplusplus.com/forum/general/222711/#msg1021407

A condition expression can have multiple sub-expressions combined with boolean operators.
Stream input/output operators can be chained.
For example:
1
2
3
4
5
6
7
char foo {};
int bar  {};
int gaz {};
while ( std::cin >> foo >> bar >> gaz && foo != 'F' )
{
  // do something with foo, bar and gaz
}


The type of each body is a single character. You have an array of strings.
You can append a character to a string. String knows how many character it has.

When you do get into the output phase and are looking at one element in the array,
the string in it should have 0, 1, or more characters. You should print '.', the character,
or number of characters, respectively.

Note that the body positions have X in [1,30], but array indices are [0,30[.
An index could thus be X-1.
Last edited on
If you decide to retain your array structure, storing contents at nodes as strings (which is reasonable: it retains a record of all celestial bodies at that point) then the following could be a general framework. This site has an excellent tutorial on arrays, including multidimensional arrays, at http://www.cplusplus.com/doc/tutorial/arrays/
I would strongly advise you to READ, not just watch videos to learn.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int WIDTH = 30;                  // Going to use i for rows, counting 0, ... , (HEIGHT - 1) downward
const int HEIGHT = 15;                 //              j for columns, counting 0, ... , (WIDTH - 1) across
                                       // Conversion:  X = j + 1          or   j = X - 1
                                       //              Y = HEIGHT - i     or   i = HEIGHT - Y

string a[HEIGHT][WIDTH];               // To store bodies at each node(!); automatically initialised as "" (empty)

// Prototypes
void readFile();
void display();

//======================================================================

int main()
{
   readFile();
   display();
}

//======================================================================

void readFile()
{
     // Write your code here
}

//======================================================================

void display()
{
     // Write your code here
}

//====================================================================== 



If you can't cope with functions then simply put the relevant code at those points in main instead - however, breaking a problem down makes things simpler.

readFile() - read Keskiverto's post and the tutorial at http://www.cplusplus.com/doc/tutorial/files/. However, if you just wanted to test the rest of the program you could simply put a single cin >> body >> x >> y; statement first and come back and do your file reading later: build your program up in steps.

display() - go through every element of your string array; finds its length; if zero, then cout "."; if greater than 1, then print the length; otherwise print the content of that string (which will be a single character).

You may need to use the conversions between (X,Y) and (i,j) listed to either store (my personal preference) or output your array.

.....P.......................S
..............................
..............................
....G.........................
..............................
..............................
..............................
..............................
..............................
.....................3........
..............................
..............................
.........M....................
..............................
M.............................
Last edited on
This is what i have now the only problem is i can not put multiple inputs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
using namespace std;

int main (){
const int HEIGHT=15, WIDTH=30;
string a[HEIGHT][WIDTH];
char c[]= {'Moon', 'Star', 'Planet', 'Galaxy'};
int input, x, y, i, j;
cout <<"Enter which celestial body (1 for moon, 2 for star, 3 for planet, 4 for galaxy),"
<<" or enter 0 to end program" <<endl;
cin >>input;
switch (input){
case 0:
cout <<"Good bye" <<endl;
return 0;
break;
case 1:
cout <<"Selected moon" <<endl;
cout <<"Enter x (0 to 29) and y (0 to 14) coordinated" <<endl;
cin >>x;
if (x<0 || x>29){
cout <<"invalid input, please enter a # between 0-29" <<endl;
cin >>x;}
cin >>y;
if (y<0 || y>14){
cout <<"invalid input, please enter a # between 0-14" <<endl;
cin >>y;}
for (i=0; i<HEIGHT; i++){
for (j=0; j<WIDTH; j++){
a[i][j] = "*";
a[x][y] = "M";
cout <<a[i][j];
}
cout <<endl;
}
return 0;
break;
case 2:
cout <<"Selected Star" <<endl;
cout <<"Enter x and y coordinated" <<endl;
cin >>x;
if (x<0 || x>29){
cout <<"invalid input, please enter a # between 0-29" <<endl;
cin >>x;}
cin >>y;
if (y<0 || y>14){
cout <<"invalid input, please enter a # between 0-14" <<endl;
cin >>y;}
for (i=0; i<HEIGHT; i++){
for (j=0; j<WIDTH; j++){
a[i][j] = "*";
a[x][y] = "S";
cout <<a[i][j];
}
cout <<endl;
}
return 0;
break;
case 3:
cout <<"Selected planet" <<endl;
cout <<"Enter x and y coordinated" <<endl;
cin >>x;
if (x<0 || x>29){
cout <<"invalid input, please enter a # between 0-29" <<endl;
cin >>x;}
cin >>y;
if (y<0 || y>14){
cout <<"invalid input, please enter a # between 0-14" <<endl;
cin >>y;}
for (i=0; i<HEIGHT; i++){
for (j=0; j<WIDTH; j++){
a[i][j] = "*";
a[x][y] = "P";
cout <<a[i][j];
}
cout <<endl;
}
return 0;
break;
case 4:
cout <<"Selected galaxy" <<endl;
cout <<"Enter x and y coordinated" <<endl;
cin >>x;
if (x<0 || x>29){
cout <<"invalid input, please enter a # between 0-29" <<endl;
cin >>x;}
cin >>y;
if (y<0 || y>14){
cout <<"invalid input, please enter a # between 0-14" <<endl;
cin >>y;}
for (i=0; i<HEIGHT; i++){
for (j=0; j<WIDTH; j++){
a[i][j] = "*";
a[x][y] = "G";
cout <<a[i][j];
}
cout <<endl;
}
return 0;
break;
default:
cout <<"invalid choice, please select one of the given numbers" <<endl;
cin >>input;
}

return 0;
}
Topic archived. No new replies allowed.