I need help on this coding

It asks me to code as this, "Write a function that has one argument called number_of_lines that is an integer. The function should print one dot and then one plus symbol on the first line, then two dots and two plus symbols on the second line, etc. until it produces number_of_lines.
For example, if number_of_lines were 3, your function would print:
.+
..++
…+++
and if number_of_lines were 5, your function would print:
.+
..++
…+++
….++++
…..+++++
"
This is my code but it didn't give me the result the same as the question asked me to. If i type 3 the result would come out as 5. The same goes to 1 2 and 4.

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
#include <iostream>
using namespace std;

int main()
{

	int number_of_lines;

	cout << " Enter the number of line: ";
	cin >> number_of_lines;

	if (number_of_lines = 1)
		{
		cout << ".+" << endl;
		}
	if (number_of_lines = 2)
		{
		cout << "..++" << endl;
		}
	if (number_of_lines = 3)
		{
		cout << "...+++" << endl;
		}
	if (number_of_lines = 4)
		{
		cout << "....++++" << endl;
		}
	if (number_of_lines = 5)
		{
		cout << ".....+++++" << endl;
		}

	system("pause");
	return 0;

}
You are using = when you should be using ==.

1
2
3
4
  if (number_of_lines = 1) // <----here
  {
  cout << ".+" << endl;
  }
Last edited on
If you're comparing equality, not assigning a value, you want to use the == operator, not the = operator.

If you fix that though, the code will just print out one line, so you'd need to tweak the code further. Usually I'd use some sort of loop with an assignment like this. e.g. loop through a set of statements for however many lines the person input.
closed account (2UD8vCM9)
You're approaching this all wrong.

First, one reason your code doesn't work is because your ifs are not doing comparisons.

where you have

 
if (number_of_lines=1)


What this is doing is actually setting the value of number_of_lines to be 1. Then the if is looking at whether or not the variable number_of_lines is true for the if condition. Any number > 0 is considered true, so this condition is not really a condition at all.

When doing comparisons, you need to have to equals signs if you're comparing.

It should read
 
if (number_of_lines==1)


That being said, even if you fix your conditions in your if statements, your logic is still wrong.


Even then, you are still approaching this the wrong way.

You are expected to do this using a nested for loop like so

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
#include <iostream>
using namespace std;

int main()
{
	int Number_Of_Lines = 0;
	cout << "Enter the number of lines: ";
	cin >> Number_Of_Lines;

	//For loop to iterate through each line
	for (int i = 1; i <= Number_Of_Lines; i++) //For each line
	{
		//For loop to draw dots
		for (int dotcounter = 0; dotcounter < i; dotcounter++) //Draw # of dots corresponding to line #
		{
			cout << ".";
		}

		//For loop to draw pluses
		for (int pluscounter = 0; pluscounter < i; pluscounter++) //Draw # of pluses corresponding to line #
		{
			cout << "+";
		}
		//After dots and pluses have been drawn for each line, make a new line for the next line
		cout << endl;
	}
        system("pause");
	return 0;
}
Last edited on
@Music
Yes, i did fix the syntax and result came out to be the same as wildblue expected. But thank you for your help and i appreciate it.

@wildblue
What type of loop will you suggest me to use wildblue?
Thank you @Pindrought
Topic archived. No new replies allowed.