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.
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.
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
#include <iostream>
usingnamespace 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;
}