Reputation: 39
Hello stack overflow users! I have a question about my program, and a problem I am encountering. I have practically finished the program, but I can't seem to have a separate void function for my dimensions (width and length of rocket). I currently have them inside my void function that creates my rocket, void draw box, but I cant seem to figure out how to have a separate void function for my dimensions. Any tips or things I am doing wrong to fix this? Any help is appreciated, thanks once again!
#include <iostream>
using namespace std;
void dimensions (int, int);
void drawBox (int , int , int);
int main ()
{
int numXs, numSpaces, numRows, width, height;
//dimensions (width, height);
drawBox (numXs, numSpaces, numRows);
return 0;
}
void dimensions (int width, int height)
{
cout << "Enter width: ";
cin >> width;
cout << "Enter height: ";
cin >> height;
cout << endl;
}
void drawBox (int numXs, int numSpaces, int numRows)
{
int count, rowCount, spaceCount, width, height;
cout << "Enter width: ";
cin >> width;
cout << "Enter height: ";
cin >> height;
cout << endl;
for (count = 0; count < width; count++)
{
cout << "X";
}
cout << endl;
for (rowCount = 0; rowCount < height; rowCount++)
{
cout << "X";
if ( width % 2 == 0)
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << " ";
}
cout << "X" << endl;
}
else
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << "X";
}
cout << "X" << endl;
}
}
cout << "X";
for (spaceCount = 0; spaceCount < numSpaces; spaceCount++)
{
cout << " ";
}
for (count = 0; count < width - 1; count++)
{
cout << "X";
}
cout << endl;
//second box is being created below
for (count = 0; count < width; count++)
{
cout << "X";
}
cout << endl;
for (rowCount = 0; rowCount < height; rowCount++)
{
cout << "X";
if ( width % 2 == 0)
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << " ";
}
cout << "X" << endl;
}
else
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << "X";
}
cout << "X" << endl;
}
}
cout << "X";
for (spaceCount = 0; spaceCount < numSpaces; spaceCount++)
{
cout << " ";
}
for (count = 0; count < width - 1; count++)
{
cout << "X";
}
cout << endl;
}
Upvotes: 0
Views: 548
Reputation: 57749
Pass your variables by reference:
void dimensions(int& height, int& width)
{
height = 5;
width = -6; // Because int is signed, this is possible.
}
int main()
{
int tall = -42;
int length = 22;
dimensions(tall, length);
std::cout << "tall: " << tall << "\n";
std::cout << "length: " << length << "\n";
return 0;
}
Upvotes: 2
Reputation: 1
Have you tried making height and width global variables, so that you can use them in both functions?
Upvotes: 0