I need your help! Do While

I want my program to loop until the user enters 0 (for selection). Like in some way, I want to keep my program running until 0 is entered...resulting in the closing of the program. Hopefully that makes sense.

When I run the program with this code, it doesn't loop.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
 #include <iostream>
using namespace std;
void Right(int height, char mark);
void Isosceles(int height, char mark);
void Left(int height, char mark);
void drawBar(int length, char mark);
void Menu(int height, char mark, int selection);



/*************************************************
* Name: Menu()                                   *
* Type: void                                     *
* Parameters: none                               *
* Purpose: creates a menu that prompts the user 
for a height and triangle type
then calls the apropriate function and passes the height parameter 
*************************************************/
void Menu(int height, char mark, int selection)
{
	
	cout << "What is the height of the triangle? ";
	cin >> height;
	cout << endl;
	 
	cout << "What character do you want to use?" ;
	cin >> mark;
	cout << endl;

	cout << "What type of triangle would you like to draw? (Enter a number)" << endl;
	
	cout << "1) Right Aligned" << endl << "2) Isosceles" << endl << "3) Left Aligned" << endl;
	cin >> selection;
	
	
	if (selection <= 3) {
		
		switch(selection)
		{
		case 1:
		Right(height,mark);
		break;
		case 2:
		Isosceles(height,mark);
		break;
		case 3:
		Left(height,mark);
		break;
		}
	
	}

	else if (selection > 3)
	{ cout << "The number you selected was invalid. Please try again." ; }
	}


/*************************************************
* Name: Left                                     *
* Type: void                                     *
* Parameters: int height – height of triangle    *
*               height must be >=0               *
*												 *
* Purpose: creates a left-aligned triangle       *
based on the height parameter                    *
*************************************************/
void Left(int height, char mark)
{
	
	for (int i = 1; i <= height; i++)
	{
		drawBar(i,mark);
	}
}

/*************************************************
* Name:Isosceles                                 *
* Type: void                                     *
* Parameters: int height – height of triangle    *
*               height must be >=0               *
*												 *
* Purpose: creates a center-aligned triangle     *
based on the height parameter                    *
*************************************************/
void Isosceles(int height, char mark)
{
	
	for (int i = 1; i <= height; i++)
	{
		for (int j = 0; j < height - i; j++)
		{
			cout << " ";
		}
		drawBar((i * 2) - 1,mark);
	}
}

/*************************************************
* Name: Right                                    *
* Type: void                                     *
* Parameters: int height – height of triangle    *
*               height must be >=0               *
*												 *
* Purpose: creates a right-aligned triangle      *
based on the height parameter                    *
*************************************************/
void Right(int height, char mark)
{
	
	for (int i = 1; i <= height; i++)
	{
		for (int j = 1; j <= height - i; j++)
		{
			cout << " ";
		}
		drawBar(i, mark);
	}
}
/*************************************************
* Name: drawBar                                  *
* Type: void                                     *
* Parameters: int length – the length of the bar *
*               length must be >=0               *
*             char mark – the symbol for drawing *
* Purpose: Displays a bar of length specified    *
* using the specified in mark to draw  *
* it, followed by a newline.                     *
*************************************************/

void drawBar(int length, char mark)
{
	for(int i=0; i<length; i++)
		cout<<mark;
	cout<<endl;
}


int main()

{
	int height=0;
	char mark = 0;
	int selection=0;

	do{
	Menu(height,mark,selection);
	system("pause");
	system("cls");
	}

	while (selection != 0);
	

	
}
Last edited on
Your passing arguments into Menu() that the function really isn't using, since we get those arguments from the user inside of Menu(). Here are some changes you could make. The way you have it written now int selection in main doesn't get updated so your loop will not work.

void Menu();


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
42
void Menu()
{

	int height, selection;
	char mark;

	cout << "What is the height of the triangle? ";
	cin >> height;
	cout << endl;

	cout << "What character do you want to use?";
	cin >> mark;
	cout << endl;

	cout << "What type of triangle would you like to draw? (Enter a number)" << endl;

	cout << "1) Right Aligned" << endl << "2) Isosceles" << endl << "3) Left Aligned" << endl;
	cin >> selection;


	if (selection <= 3) {

		switch (selection)
		{
		case 1:
			Right(height, mark);
			break;
		case 2:
			Isosceles(height, mark);
			break;
		case 3:
			Left(height, mark);
			break;
		}

	}

	else if (selection > 3)
	{
		cout << "The number you selected was invalid. Please try again.";
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
int main()

{
	char ch = 'y';

	while (ch == 'Y' || ch == 'y'){
		Menu();
		cout << "Run again?(y/n): ";
		cin >> ch;
		cout << endl;
	}
	return 0;
}
Last edited on
Glaring issue - You are declaring selection in your main and passing a copy of it to Menu and instead of returning a copy back to your Main, you let it die in Menu.

Subtle issue
If you want your program to keep running until 0 is entered you will either have to read in a char and cast it to an int or read in an int and check the standard stream afterwards to see if it failed.

The easy way is to read in as a char and cast it via static_cast<int>(char). The safer approach is the latter.

If you choose nothing, the second someone enters a non-int value your program will fail the next time you try to get input.

P.S. - Your Menu documentation needs an update.
Last edited on
Thank you very much!

Now, I would like to understand what was done in int main().

Is there a rule where we're not allowed to use more than one character in...

(ch == 'Y' || ch == 'y')

Like when I changed that to
(ch == 'Yes' || ch == 'yes')

the program did not work.

And before I changed that, I did

char ch = 'Yes';

So is there something I'm missing?

I was just wondering if it could work like that.
A char contains one letter, and one letter only. There are also two different ways to declare strings, using single quotes and double quotes. Single quotes are single characters, whereas double quotes are for strings, or multiple characters. If you want to change your program to be able to do multiple character strings, either use char arrays or std::string, like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string str = "Yes"; // use double quotes
// you can input to a string
std::cin >> str;

// and you can test a string
if (str == "Yes" || str == "yes") {
    // do something;
}

// You can even add to a string
str = "Hello ";
str += "World!";
std::cout << str << std::endl; // => Hello World! 


See http://www.cplusplus.com/reference/string/string/

EDIT:
If you want to use strings, put this at the include statement for it in your program as well:
#include <string>
Last edited on
Topic archived. No new replies allowed.