Hello FoaaD2506,
Going over your program. Some things I have changed:
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
|
#include <iostream>
using namespace std;
char menu(char c); // <--- Do not need to send anything to the function. The value returned from the function takes case of this.
void triangle(int rows);
int main()
{
char choice{}, choose{};
int x{};
choose = menu(choose);
while (choose != 'D' && choose != 'd')
{
if (choose == 'A' || choose == 'a')
{
while (choice != '4')
{
cout << "1-do something" << endl;
cout << "2-do something" << endl;
cout << "3-do something" << endl;
cout << "4-Return to the main menu." << endl;
cin >> choice;
}
}
else
if (choose == 'B' || choose == 'b')
{
}
else
if (choose == 'C' || choose == 'c')
{
while (choice != '2')
{
cout << "1- Draw Triangle " << endl;
cout << "2- Return to the main menu" << endl;
cin >> choice;
if (choice == '1')
{
cout << "please enter number of rows " << endl;
cin >> x;
triangle(x);
}
}
}
}
return 0;
}
void triangle(int rows)
{
for (int i = rows; i >= 1; i--)
{
for (int k = 1; k <= i; k++)
{
cout << "*";
}
cout << endl;
}
}
char menu(char c)
{
cout << "A or a. 1D Array." << endl;
cout << "B or b. 2D Array." << endl;
cout << "C or c. Draw a Triangle." << endl; // <--- This is not a recursive function.
cout << "D or d. Exit" << endl;
cin >> c;
return c;
}
|
You need to change line 5 to
char menu();
along with the function call and function definition.
Inside "main" you need to initialize your variables.
1 2
|
char choice{}, choose{};
int x{}; // <--- "x" should be called "rows". Try to avoid single letter variables.
|
The "uniform initializer" the empty {}s will set the "char"s to "\0" and the "int" to (0) zero. This is needed later on in the program.
Line 13 should be inside the while loop not outside. Also having initialized the variable "choose" the condition will evaluate to true.
In line 15 the while condition should be a logical AND (&&). My experience has bee when using 2 "!="s the (&&) works better most of the time.
Choice "A" and "B" I have not worked on yet.
In choice "C":
Line 36. First initializing the variable "choice" allow you to enter the while loop because it is not equal to '2'. By not initializing the variable it gave me a run time error saying "that the variable was being used uninitialized". This while condition allows for any ASCII character other than '2' to be a valid choice, but when you do enter a choice you never check if it is a valid choice.
When you finish the "if" statement for "C" you go back to the first while loop, but since "choose" never changes you have an endless loop with no way out. That is where moving line 13 inside the while loop is needed.
After line 40 you should check that what was entered is a (1) or a (2). Although the while loop keeps repeating you never know why.
Your code:
1 2 3
|
cout << "1- Draw Triangle " << endl;
cout << "2- Return to the main menu" << endl;
cin >> choice;
|
The code works, but I think you would be better with:
1 2 3 4 5
|
cout <<
"\n1- Draw Triangle "
"\n2- Return to the main menu"
"\nEnter choice: ";
cin >> choice;
|
This is a little better representation of what will be displayed on the screen and it is easier to maintain or change.
After "main" I just used your original functions just to test the program.
Andy