need help with project

I have to design a rocket that looks like this.
/\
/ \
/ \
+------+
| |
| |
+------+
| |
| |
+------+
| |
| |
+------+
/\
/ \
/ \
(the sides line up with the +'s and the top and bottom are a cone. I dont know why the forum wont recognize my spaces.)
Indent 10 spaces from left screen margin.

Honestly i am a visual learner, i need someone showing me how to do each step exactly otherwise i wont be able to figure it out or make sense of it. We have to use the <string> fuction to build it and so far i have this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//rocketship
#include <iostream> 
#include <string>
using std::string;
Void Main ()
{
// String sides + spaces + sides
String sides (1, ‘|’)
String spaces (6, ‘ ‘)
//string top/bottom
String top = “/\”
//Display 

cout<< display << endl;
}


Obviously nowhere near complete because everytime i try and test something in my display it always give me a crap loud of syntax errors and i see nothing wrong.

Also how do i indent the entire code when it is finished? Do i have to Brute force space it or is there a function?

And to build a string for say the top should i be able to make it like "/\" /n "/ \" and so on so when it renders the string it will be in order like that?

Just started the class to use dumb people terms for me :D


Thanks!
Last edited on
For one void is lowercase since c++ is case sensitive and secondly never use void main it is always int main.

with line 8 if it is only a string of one character you don't exactly have to fill it simply put String sides( "|" ); Actually looking at your program it should proabably be 2 | and not just 1.

Lastly you never declare 'display' and are trying to output it.

Also is it me or are you using
`
instead of
'


you can also tab by doing '\t'

So to get 10 space indent you could do something like
cout << "\t\t " << "this is where your stuff goes" << endl;

I would do this project something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
    std::string side( 2 , '|' ) , top( "/\\" ) , platform( "+------+" ) , indent( "\t\t  " ); //not sure what that +------+ thing is you have
    //Also the top string has two backslashes since the first one signals an escape sequence
    for( int i = 0; i < 3; ++i ) std::cout << indent << top << std::endl;
    for( int i = 0; i < 3; ++i ) std::cout << indent << platform << std::endl << indent << side << std::endl;
    std::cout << indent << platform << std::endl;
    for( int i = 0; i < 3; ++i ) std::cout << indent << top << std::endl;
}
                  /\
                  /\
                  /\
                  +------+
                  ||
                  +------+
                  ||
                  +------+
                  ||
                  +------+
                  /\
                  /\
                  /\

Process returned 0 (0x0)   execution time : 0.028 s
Press any key to continue.


*edit
added output
**edit
added the missing code tag
Last edited on
the sides line up with the +'s and the top and bottom are a cone. I dont know why the forum wont recognize my spaces.

HTML and similar parsers treats all runs of whitespace as (replace all... with) a single space.

Here, if you put you text in [output][/output] tags, it will leave the spacing just like you want it.

             /\
            /  \
           /    \
          +------+
          |      |
          |      |
          +------+
          |      |
          |      |
          +------+
          |      |
          |      |
          +------+
             / \
            /   \
           /     \


Andy

Edit forgot the "Indent 10 spaces from left screen margin."...
Last edited on
Oh that makes a lot more sense to me now. I went AFK when I was typing mine so didn't see his message
the sides line up with the +'s and the top and bottom are a cone. I dont know why the forum wont recognize my spaces.


He could still use loops easily for that though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;
void body();
int main ()
{
    cout<<"   /\\   "<<endl;
    cout<<"  /  \\  "<<endl;
    cout<<" /    \\ "<<endl;
    body();
    cout<<"   / \\  "<<endl;
    cout<<"  /   \\ "<<endl;
    cout<<" /     \\"<<endl;
    return 0;
}
void body()
{
    string a="+------+";
    string b="|      |";
    for(int x=0;x<3;x++)
    cout<<a<<endl<<b<<endl;
    cout<<a<<endl;
}
Line 21 is missing a b heads up.
OK sorry ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;
void body();
int main ()
{
    cout<<"   /\\   "<<endl;
    cout<<"  /  \\  "<<endl;
    cout<<" /    \\ "<<endl;
    body();
    cout<<"   / \\  "<<endl;
    cout<<"  /   \\ "<<endl;
    cout<<" /     \\"<<endl;
    return 0;
}
void body()
{
    string a="+------+";
    string b="|      |";
    for(int x=0;x<3;x++)
    cout<<a<<endl<<b<<endl<<b<<endl;
    cout<<a<<endl;
}
Thanks chris! and everyone else! I half expected a "do you're own work" response, but this really helped! I understand the strings now and i was just doing them funky.

I few questions to understand it though.

Whats the difference between int main() and void body ()?

And what is the For(int x=0;x<3;x++)? is that the indent?

And how exactly did you get the top and bottom part of the rocket to show up at the top and bottom? Is that what the return is for?


Thanks again!
int main is your programs entry point... it's literally your "main" function. void body is a function itself it's being declared at the top then defined at the bottom. If you look a the bottom...
1
2
3
4
void body()
{
    //.....
}

Everything inside the opening { and closing } bracket will happen when you call the function inside int main().
1
2
3
4
5
6
int main()
{
    body(); //This will do everything inside the body function.

    return0;
}


int main is similar but since it's your programs entry point it will be called at the startup of the program and do whatever code you put inside it.

for(int x=0;x<3;x++)
This is a for loop and all it does is repeat the code within it's open and close brackets {...} while x is smaller then 3. more or less...
1
2
3
4
5
int x = 0;
while(x < 3) //if x is smaller then 3
{ //open bracket
    x++; // add 1 to x each pass thru this code.
}//close brasket 

If you are unfamiliar with while loops you should really go thru a few basic c++ tutorials because loops are rather important.

The reason the top and bottom of the ship draw like they should is because you call the body() function between the top and bottom "cout" calls.

Every function with a type like "int main" must return a value of the type it was created with, so "int main" must return a int which is why you can return 0 and avoid any compiler errors or complaints.

void functions like "void body()" have a void return type which means they return nothing at all.


Edit: I just noticed he didn't put the open and close brackets on his for loop... c++ allows this for the next line down. It's actually like this..
1
2
3
4
5
6
7
8
9
void body()
{
    string a="+------+";
    string b="|      |";
    for(int x=0; x<3; x++)
        cout<<a<<endl<<b<<endl<<b<<endl; //only this one line is repeated

    cout<<a<<endl;
}

look closely and it's just printing a b b 3 times then at the bottom a again.

You could combine what he has into one function really easy. instead of the body() function you could do something like..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void drawSpaceship()
{
    string a="+------+";
    string b="|      |";

    cout<<"   /\\   "<<endl;
    cout<<"  /  \\  "<<endl;
    cout<<" /    \\ "<<endl;
    for(int x=0; x<3; x++)
        cout<<a<<endl<<b<<endl<<b<<endl;
    cout<<a<<endl;
    cout<<"   / \\  "<<endl;
    cout<<"  /   \\ "<<endl;
    cout<<" /     \\"<<endl;

}

If you were to call this drawSpaceship function inside your main it would do the same thing.
1
2
3
4
5
int main()
{
    drawSpaceship();
    return 0;
}
Last edited on
Exempt you saved my time to wright it lol ;)
Alright i understand. Greatly appreciate that explanation. So exactly how would i go about indenting? Say we use chris's code would i just add /t after each item? or is there an easier apply all function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;
void body();
int main ()
{
    cout<<"   /\\   "<<endl; // /t
    cout<<"  /  \\  "<<endl; // /t 
    cout<<" /    \\ "<<endl; // so on.. ect..
    body();
    cout<<"   / \\  "<<endl;
    cout<<"  /   \\ "<<endl;
    cout<<" /     \\"<<endl;
	return 0;
}
void body()
{
    string a="+------+";
    string b="|      |";
    for(int x=0;x<3;x++)
    cout<<a<<endl<<b<<endl<<b<<endl;
    cout<<a<<endl;
}
You're using a string so anything you put inside it will be there... add spaces if you must.

1
2
3
4
5
string a = "          +------+";
string b = "          |         |";
//same in all the cout...
cout<<"          /\\"<<endl;
cout<<"         /  \\"<<endl; //so on... 


Look into strings more and you'll find some neat stuff. Also I edited my post up there.

edit: \t is the same as pressing tab I believe but I'm not sure what sets it's default value.
Last edited on
You mean \t this is a tabulator like cout<<"\tHello";
Alright i see now. I figured i would have to put the spaces in the string i just wasn't sure it wouldn't screw everything up. :)

Thank you both for all the explanations and examples, i am sure you will see more posts by me as the semester goes on. I tend to get stuck a lot.. Nice to know i have a good source of info. Honestly my professor does not teach anything we need to know to complete the assignment and its not readily available in the book, plus google is a cruel mistress when looking for example codes.

Thanks again!
Just go here and practice and you'll learn everything quickly.

http://www.cplusplus.com/doc/tutorial/
What Exempt said :)
also check out this site which has some great tutorials.
learncpp.com

Also if you have like 2$ you can buy a copy of "A first book of c++" by gary J bronson(edition 2). It's a pretty good book I thought.

http://www.ebay.com/ctg/A-First-Book-of-C-From-Here-to-There-by-Gary-J-Bronson-1999-Paperback/440292
Topic archived. No new replies allowed.