I need some help on an array lab.

I have a c++ program lab that needs to be done. It is to prompt the user for the month they were born in number such as 1, 2, … 12. Use this number as a subscript to an array or parallel arrays to display the month and the flower for that month. You should have an error message for the wrong number entered. You need to prompt the user if they like to continue with another number.
Last edited on
Here is the code, I know it's not correct, but hopefully someone can help.

int main()
{
// declare variables
int mynum = 0;
char ans = ' ';
string allMonths[12] = { "January","Feburary","March","April","May","June","July","August","September","October","November","December" };
string allFlowers[12] = {"Carnation","Iris","Daffodil","Daisy","Lily of the Valley","Rose","Sunflower","Gladiolus","Aster","Snapdragon","Chrysanthemum","Orchid"};
do
{
system("cls");
system("color f0");
cout << "\t\t******************************" << endl;
cout << "\t\t* Anonymous *" << endl;
cout << "\t\t* CPT-168-A01 *" << endl;
cout << "\t\t* Month Array *" << endl;
cout << "\t\t******************************" << endl << endl;

cout << "Please enter the month you were born (1-12) : " << endl;
cin >> mynum;
} while (mynum > 0 && mynum <= 12);
{
cout << "Please enter the month you were born (1-12) :";
cin >> mynum;







while (toupper(ans) == 'Y');
cout << "" << endl;
cout << " T H A N K Y O U " << endl << endl;
system("pause");
return 0;
}
When posting code, please use code tags. Highlight your code and then click the <> button to the right of the edit window.

Here is your code modified slightly so it compiles. See the "TO DO" comments.
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
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;
using std::endl;

int
main()
{
    // declare variables
    int mynum = 0;
    char ans = ' ';
    string allMonths[12] =
	{ "January", "Feburary", "March", "April", "May", "June",
	  "July", "August","September", "October", "November", "December" };
    string allFlowers[12] =
	{ "Carnation", "Iris", "Daffodil", "Daisy", "Lily of the Valley",
	  "Rose","Sunflower", "Gladiolus", "Aster", "Snapdragon",
	  "Chrysanthemum", "Orchid" };

    // Print the header only once
    system("cls");
    system("color f0");
    cout << "\t\t******************************" << endl;
    cout << "\t\t* Anonymous *" << endl;
    cout << "\t\t* CPT-168-A01 *" << endl;
    cout << "\t\t* Month Array *" << endl;
    cout << "\t\t******************************" << endl << endl;

    // When validating input, the test to break out of the loop
    // is in the middle of the logic.  Rather than fighting it, I
    // just code it that way.
    while (true) {
	cout << "Please enter the month you were born (1-12) : " << endl;
	cin >> mynum;
	if (mynum > 0 && mynum <= 12) {
	    break;
	}
	cout << "Month must be between 1 and 12\n";
    }

    cout << "You entered month " << mynum << '\n';

    // TO DO: Print the month and flower.  Remember that the user
    // entered a number from 1 to 12, but the array indexes go from 0
    // to 11.

    // TO DO: put everything above in a loop.  Add code to prompt user
    // to continue


    cout << " T H A N K Y O U " << endl << endl;
    system("pause");
    return 0;
}

1. do loops suck. use while alone.
2. I took out your system calls so the code would actually compile on my system.
3. I gave the program a serious case of std's since you didn't mention using a namespace.
4. I added the output statements and questions for the second part.

OK so don't try to write the whole program all at once before you compile it. That way you'll be a lot less likely to end up with a hopeless mess. Start with just getting the menu to display at all. You'd have caught the namespace/std issue right away there.

Next, try putting it in a loop.
Whatever kind of loop you put it in, doesn't really matter.
You can even use a for loop indefinitely by decreasing the variable back down within the loop, or defining a test that doesn't use whatever you're counting with in the for loop... This may not be true in higher languages and its probably considered bad practice, but be aware it can happen in a low level language like C.

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
#include <string>
#include <iostream>
int main()
{
// declare variables
        int mynum = 4;
        char ans = ' ';
        std::string allMonths[12] = { "January","Feburary","March","April","May","June","July","August","September","October","November","December" };
        std::string allFlowers[12] = {"Carnation","Iris","Daffodil","Daisy","Lily of the Valley","Rose","Sunflower","Gladiolus","Aster","Snapdragon","Chrysanthemum","Orchid"};
        std::string tmppause;
        ans = 'Y' ; //If the user actually executed this program, that's implied consent for doing this to them at least once.

        while (toupper(ans) == 'Y') {
                mynum=-69; // negative sixty nine avoids the error message.
                while ((mynum < 1 || mynum > 12)) {
                        //system("cls");
                        //system("color f0");
                        if (mynum != -69) {
                                std::cout << "\n\nWRONG! BAD INPUT! USER ERROR DETECTED! TRY AGAIN OR PRESS POWER BUTTON TO GIVE UP!\n\n";
                                }
                        std::cout << "\t\t******************************" << std::endl;
                        std::cout << "\t\t* Ash Ketchum                *" << std::endl;
                        std::cout << "\t\t* CPT-101                    *" << std::endl;
                        std::cout << "\t\t* Month Array                *" << std::endl;
                        std::cout << "\t\t******************************" << std::endl << std::endl;

                        std::cout << "Please enter the month you were born (1-12) : " << std::endl;
                        std::cin >> mynum;
                        }
                std::cout << "You were attacked by " << allFlowers[mynum-1] << " last " << allMonths[mynum-1] << " and it hurt, too!\n" ;
                std::cout << "Do you wish to repeat this again (Y/N)\n";
                std::cin >> ans;
                }
        std::cout << "" << std::endl;
        std::cout << " T H A N K Y O U " << std::endl << std::endl;
        getline(std::cin, tmppause);  //pause
        return 0;
        }


Finish your program by enhancing the input checking for yes and no, so that it only accepts the correct key combinations.
int main()
{
// declare variables
int mynum = 0;
char ans = ' ';
char f1 = 'y';
bool flag = true;
string allMonths[12] = { "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
string allFlowers[12] = { "Carnation", "Iris", "Daffodil", "Daisy", "Lily of the Valley", "Rose", "Sunflower", "Gladiolus", "Aster", "Snapdragon", "Chrysanthemum", "Orchid" };
while (flag == true)
{
system("cls");
system("color f0");
cout << "\t\t******************************" << endl;
cout << "\t\t* Anonymous *" << endl;
cout << "\t\t* CPT-168-A01 *" << endl;
cout << "\t\t* Month Array *" << endl;
cout << "\t\t******************************" << endl << endl;

cout << "Please enter the month you were born (1-12) : " << endl;
cin >> mynum;

switch (mynum)
{
case 1:
cout << "januray";
system("color 2");
break;

case 2:
cout << "feburary";
break;

case 3:
cout << "March";
break;

case 4:
cout << "April";
break;

case 5:
cout << "May";
break;

case 6:
cout << "June";
break;

case 7:
cout << "July";
break;

case 8:
cout << "August";
break;

case 9:
cout << "September";
break;

case 10:
cout << "October";
break;

case 11:
cout << "November";
break;

case 12:
cout << "December";
break;
}
if (!(mynum>=1&&mynum<=12))
{
cout << "\nenter valid number";
}
_getch();
cout << "\ndo you want to continue?(y/n)";
bye:
f1 = _getch();
if (f1 == 'n')
break;
else if (f1 == 'y')
{
}
else
{
cout << "\nwrong answar.";
goto bye;
}

}

cout << "" << endl;
cout << " T H A N K Y O U " << endl << endl;
system("pause");
return 0;
}
1. do loops suck. use while alone.

Use whichever loop makes sense. If the loop always executes once, use a do loop. If it can execute zero times, use a while loop. If the exit condition occurs in the middle of the loop, use a break statement. The code will be clearer this way.

3. I gave the program a serious case of std's since you didn't mention using a namespace.

Another option is to add using std::cout; etc. That way you aren't bringing in the whole namespace, but you can still avoid std::adding std::prefixes std::everywhere.

Get in the habit of using \n instead of endl. Endl flushes the stream which can take a long time. Use endl only when you need to flush the stream. Juyst yesterday I demonstrated to someone on another site that their code was running 10x slower than necessary for this one reason.

Here is zaphraud's nice solution with these changes. I find the loops easier to read and code like this.
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
#include <string>
#include <iostream>

using std::cin;
using std::cout;
using std::string;
using std::endl;

int
main()
{
    // declare variables
    int mynum = 4;
    char ans = ' ';
    string allMonths[12] = {
	"January", "Feburary", "March", "April", "May", "June", "July",
	"August", "September", "October", "November", "December"};
    string allFlowers[12] = {
	"Carnation", "Iris", "Daffodil", "Daisy", "Lily of the Valley",
	"Rose", "Sunflower", "Gladiolus", "Aster", "Snapdragon",
	"Chrysanthemum", "Orchid"};
    string tmppause;
    
    do {
	while (true) {	 // loop exits on valid input below
	    cout << "\t\t******************************\n";
	    cout << "\t\t* Ash Ketchum                *\n";
	    cout << "\t\t* CPT-101                    *\n";
	    cout << "\t\t* Month Array                *\n";
	    cout << "\t\t******************************\n\n";

	    cout << "Please enter the month you were born (1-12) : " << endl;
	    cin >> mynum;
	    if (mynum >= 1 && mynum <= 12) {
		break;		// valid input
	    } else {
		//system("cls");
		//system("color f0");
		cout << "\n\nWRONG! BAD INPUT! USER ERROR DETECTED! TRY AGAIN OR PRESS POWER BUTTON TO GIVE UP!\n\n";
	    }
	}

	cout << "You were attacked by "
	      << allFlowers[mynum - 1] << " last "
	      << allMonths[mynum - 1] << " and it hurt, too!\n";
	cout << "Do you wish to repeat this again (Y/N)\n";
	cin >> ans;
    } while (toupper(ans) == 'Y');

    cout << "\n";
    cout << " T H A N K Y O U \n\n" << std::flush;
    getline(cin, tmppause);		 //pause
    return 0;
}

Topic archived. No new replies allowed.