Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
People are mentioning that my question isn't clear enough so hopefully to make it easier for everyone I'm going to restructure it.

The code below is designed to print a function that creates an arrow made out of '*' for example:
1.__*
2._* * *
3.* * * * *
4.__ *
5.__ *
6.__ *

However, my arrow must be calculated from user input. The arguments of the arrow must take: The width of the arrow head, the width of the tail, length of the arrow from tail to point and the character used to draw the arrow (in this case '*')

Please take a look at the code, and then below I will post the errors I'm receiving when I try entering debug mode. I will update the code as I go along with peoples advice. Also please note I'm not an expert with C++, this is really my first task. So if you could please spell everything out for me as you would want it explained when you were a beginner. Thank you

C++
// program to run an arrow function
// Author: Douglas Tyler
// Date: 30.10.2014
// Edited: 06.11.2014

#include <iostream> 
#include <sstream> 
using namespace std;


void draw_arrow(int width, int tail, int length, char draw){

	for (int i = 1; i <= width; i += 2){ //print Head width

			for (int j = 0; j<(width - i) / 2; j++) //print Tail width
				std::cout << " "; // print space

			for (int j = 0; j<(length - i) / 2; j++); // print length
			std::cout << " "; // print space

		for (int j = 0; j			std::cout << draw;

		std::cout << "\n";
	}
	//print tail 
	for (int i = 0; i < width / 2; i++){
		for (int j = 0; j<(width - tail) / 2; j++)
			std::cout << " ";
		for (int j = 0; j<tail;>			std::cout << draw;
		std::cout << std::endl;
	}


}

int main() // main function
{
	cout << "Welcome to printArrow" << endl;
	cin >> " " >> endl;

	int width_of_arrow = 0;
	int width_of_tail = -1;
	int length_of_arrow = 0;
	char drawing_char = '\0';
	std::string input;
	std::cout << "Please input the Width of the Arrow head: "; // ask the user to input arrow head width information
	std::getline(std::cin, input);
	std::stringstream(input) >> width_of_arrow;

	std::cout << "Please input the Width of the Tail: 1";
	std::getline(std::cin, input);
	std::stringstream(input) >> width_of_tail;
	if (width_of_tail == -1) width_of_tail = 1;

	std::cout << "Please input the length of the Arrow: ";
	std::getline(std::cin, input);
	std::stringstream(input) >> length_of_arrow;
	if (length_of_arrow == -2) length_of_tail = 2;

	std::cout << "Input Drawing Character: *";
	std::getline(std::cin, input);
	drawing_char = input[0];
	if (drawing_char == '\0') drawing_char = '*';

	draw_arrow(width_of_arrow, width_of_tail, length_of_arrow, drawing_char);
	return 0;
} 

Error 1 error C2086: 'std::string input' : redefinition
Error 2 error C2065: 'length_of_arrow' : undeclared identifier
Error 3 error C2065: 'length_of_arrow' : undeclared identifier
Error 4 error C2065: 'length_of_arrow' : undeclared identifier
Error 5 IntelliSense: identifier "length_of_arrow" is undefined"

This may seem simple to some of you but it isn't to me so I appreciate you being understanding. Thank you for any help you can give
Posted
Updated 7-Nov-14 4:57am
v7
Comments
Nelek 7-Nov-14 13:07pm    
You have deleted something important in your first "print tail" for

Have a look and review / correct your code. As it is (v7), it can't compile

C++
      void draw_arrow(int width, int tail, char draw, int length){
Line 62:   draw_arrow(width_of_arrow, width_of_tail, drawing_char);


Seems obvious to me.
 
Share this answer
 
Comments
Member 11206644 7-Nov-14 10:15am    
Ah yes, of course. Thank you for pointing that out. However I've changed it and I'm still getting the same thing :(
PIEBALDconsult 7-Nov-14 10:19am    
Then update the queestion with the new code.
Here is your code with all the irrelevant parts removed so it will build. You need to check carefully the logic of the draw_arrow function to make it work. There were two statements (marked) which were incomplete so they need correcting. Note, if you include a using namespace std; statement then you do not need to add the std:: prefix to all your STL statements.

C++
// program to run an arrow function
// Author: Douglas Tyler
// Date: 30.10.2014
// Edited: 06.11.2014

#include <iostream> 

using namespace std;
 

void draw_arrow(int width, int tail, int length, char draw)
{

	for (int i = 1; i <= width; i += 2)
	{
		//print Head width

		for (int j = 0; j < (width - i) / 2; j++) //print Tail width
			cout << " "; // print space

		for (int j = 0; j < (length - i) / 2; j++) // print length
			cout << " "; // print space

		for (int j = 0; j < (width - i) / 2; j++) // not sure what you want here			
			cout << draw;

		cout << endl;
	}
	//print tail 
	for (int i = 0; i < width / 2; i++)
	{
		for (int j = 0; j < (width - tail) / 2; j++)
			cout << " ";
		for (int j = 0; j < tail;) // not sure about this either
			cout << draw;
		cout << endl;
	}

}
 
int main() // main function
{
 
	int width_of_arrow = 0;
	int width_of_tail = -1;
	int length_of_arrow = 0;
	char drawing_char = '\0';


	cout << "Welcome to printArrow" << endl;
	cout << "Please input the Width of the Arrow head: "; // ask the user to input arrow head width information
	cin >> width_of_arrow;
 
	std::cout << "Please input the Width of the Tail: 1 ";
	cin >> width_of_tail;
	if (width_of_tail == -1)
		width_of_tail = 1;
 
	cout << "Please input the length of the Arrow: 2 ";
	cin >> length_of_arrow;
	if (length_of_arrow == -2)
		length_of_arrow = 2;
 
	cout << "Input Drawing Character: * ";
	cin >> drawing_char;
	if (drawing_char == '\0')
		drawing_char = '*';
 
	draw_arrow(width_of_arrow, width_of_tail, length_of_arrow, drawing_char);
	return 0;
}
</iostream>
 
Share this answer
 
Comments
Member 11206644 7-Nov-14 11:27am    
Thank you very much. That's very helpful. In the marked parts I have to say it's a combination of tutorial work and bad calculation by me by the looks of it. I would like there to calculate all four arguments including the head width, the tail width, the length from point to tail and the character used (in this case '*') and I'm not too sure how to go about that.
Richard MacCutchan 7-Nov-14 11:35am    
The way to deal with problems like this is to start at the right point. Do not start by writing code in the hope that it will somehow come out right. Draw a sample arrow on paper, something like:

___*
__***
_*****
___*
___*
___*

(Underscore characters represent spaces)
Now you can figure out how many spaces, and stars are required on each line, and how those numbers relate to your input values. From that information you can figure out the calculation required for each part. Only then should you start writing the code.
Member 11206644 7-Nov-14 11:52am    
Okay, perfect. I've drawn the right side of the arrow and labelled each argument required. I'm not quite sure how to implement a calculation for it though
Richard MacCutchan 7-Nov-14 11:59am    
Look carefully at the numbers for each line; write them down alongside each line and look for the pattern.

For example, the arrow is 5 wide, and in the first line we print only one character. That means (5-1) will be spaces. Since we do not need the spaces after the star then we need to print (5-1)/2 spaces first, then the single star. On the next line we need 3 stars so it is (5-3)/2 spaces, and so on, until we print the full width of the arrow head.

Next the tail, which is a fixed width, so we only need the calculation once, and then repeat the printing of that number of spaces and stars until we reach the limit of the length.
Member 11206644 7-Nov-14 12:07pm    
Would I need to code each line individually?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900