Infinite *'s

Hello, my program runs well, but outputs the same and incorrect output. It is supposed to output half a diamond for example, if a user inputs 5 it would look like this:

*
***
*****
***
*

Any input to my program and it will endlessly print *'s. The input also has to be an odd number, but my program is taking any kind of input and outputting the same. I believe the error is in my "output" function, but I might be wrong. I appreciate your time! Thanks!

#include <iostream>
using namespace std;

void input(int length);
void output(char selection, int length);

int main ()
{
int length;
char selection;

input(length);
output(selection, length);
}
void input(int length)
{
cout << "Hello! This program will show you half a diamond with the length\n"; //Explaining the program
cout << "you input! Just remember no negatives(includes 0) or even numbers\n";
cout << endl;
cout << "Now please enter the length of your diamond:\n"; //Prompting user
cin >> length;
//Echoing Input
if ((length <= 0) && (length / 2))
{
cout << "You didn't read the instructions. Start over...\n";
system("Pause");
system ("cls");
main();
}
}
void output(char selection, int length)
{
for (int i = 0; i < length; i++)
{
cout << "*";
}
for (int iI = length; iI >= 0; ++iI)
{
cout << "*";
}

//Repeating The Program
cout << endl;
cout << "If you want to try again press Y\n";
cout << "If you wish to end the program press N\n";
cout << endl;

cin >> selection;

if(selection == 'Y')//If "Y" is used, program will repeat itself
{
system ("cls");
main();
}
else //If anything else is pressed, program will end
{
cout << "Goodbye\n";
}


}
iI starts at length, the for continues while iI is non-negative, and after every iteration iI is incremented by 1. Can you guess what happens if length starts out positive?
You may not call main recursively in C++.
So iI should start below length opposed to equal to?
closed account (NwvkoG1T)
Actually iI would never come down. A little debugging should have shown you that iI starts from length(obviously non negative) and goes on till infinity. And yea. Main cant be called recursively. You wil simply need to return and u will have to implement an infinite loop in the main rather than in a function
closed account (18hRX9L8)
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
37
38
39
40
41
#include <iostream>

main()
{
	int length,counter,count;
	
	std::cout<<"This program makes half a diamond."<<std::endl;
	while(!false)
	{
		std::cout<<"Enter length (odd only)."<<std::endl<<std::endl;
		std::cout<<"  > ";
		std::cin>>length;
		std::cout<<std::endl;
		if(length%2==0)
		{
			std::cout<<"This number, "<<length<<" is even."<<std::endl;
			std::cout<<"Please try again."<<std::endl<<std::endl<<std::endl;
		}
		else
		{
			break;
		}
	}
	std::cout<<std::endl<<std::endl;
	for(counter=1;counter<=length;counter=counter+2)
	{
		for(count=1;count<=counter;count++)
		{
			std::cout<<"*";
		}
		std::cout<<std::endl;
	}
	for(counter=length-2;counter>=0;counter=counter-2)
	{
		for(count=counter-1;count>=0;count--)
		{
			std::cout<<"*";
		}
		std::cout<<std::endl;
	}
}


You shall never use system() again. /* http://www.cplusplus.com/articles/j3wTURfi/ */
You shall not call main more than once. /* 3.6.1.3:"The function main shall not be used within a program." 5.2.2.9:"Recursive calls are permitted, except to the function named main" */

(bad guy says: System("PASS");
Gandalf says: You shall not pass!)
Last edited on
Topic archived. No new replies allowed.