compilation of 2 games, 1 simple grocery and a grading system.

How can I execute all the program one by one by choosing them in sub menu under the main menu. That's my problem. But I already put the codes here.

here's my code....



#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>
int main()
{
int pressMenu;

std::cout << " PLEASE CHOOSE WISELY!!\n";
std::cout <<" ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~\n\n";
std::cout <<" +===============+ " << " +===============+\n";
std::cout <<" | (1) PROGRAMS | " << " | (2) GAMES |\n";
std::cout <<" +===============+ " << " +===============+\n";
std::cin >> pressMenu;
switch (pressMenu)
{
case 1: (pressMenu=='1');
std::cout <<" +======================+ " << " +======================+\n";
std::cout <<" | (1) ARRAY | " << " | (2) GRADING SYSTEM |\n";
std::cout <<" +======================+ " << " +======================+\n";
std::cout <<" +======================+ " << " +======================+\n";
std::cout <<" | (3) FUNCTION | " << " | (4) SIMPLE GROCERY |\n";
std::cout <<" +======================+ " << " +======================+\n\n";
break;
case 2: (pressMenu=='2');
std::cout <<" +======================+ " << " +======================+\n";
std::cout <<" | (1) PLAY LOTTO | " << " | (2) GUESS THE NUMBER |\n";
std::cout <<" +======================+ " << " +======================+\n";
break;
default : std::cout<<"Wrong Choice: \n";
break;
}

{
//////// generate random numbers /////////
std::srand( std::time(0) );

const int rand_num1 = std::rand() % 10 ;
const int rand_num2 = std::rand() % 10 ;
const int rand_num3 = std::rand() % 10 ;


//////// accept three guesses ///////////
int guess1, guess2, guess3 ;
std::cout << "Enter three guesses:\n" ;
std::cin >> guess1 >> guess2 >> guess3 ;


//////// count exact and inexact matches ////////////
int exact_matches = 0 ;
int inexact_matches = 0 ;

if( guess1 == rand_num1 ) ++exact_matches ;
else if( guess1 == rand_num2 || guess1 == rand_num3 ) ++inexact_matches ;

if( guess2 == rand_num2 ) ++exact_matches ;
else if( guess2 == rand_num1 || guess2 == rand_num3 ) ++inexact_matches ;

if( guess3 == rand_num3 ) ++exact_matches ;
else if( guess3 == rand_num1 || guess3 == rand_num2 ) ++inexact_matches ;


/////////// check counts and print out result ///////////////
if( exact_matches == 3 ) std::cout << "three matching in exact order" ;

else
{
const int matches = exact_matches + inexact_matches ;

// a switch could be used here
if( matches == 0 ) std::cout << "no matches at all\n" ;
else if( matches == 3 ) std::cout << "three matching, not in order\n" ;
else std::cout << matches << " matching\n" ;
}
}
}
void games()
{
std::cout << "SIMPLE NUMBER GUESSING GAME\n";
std::cout << "Given a number from 0-10.\n";
std::cout << "Guess whether the number is going to be higher or not.\n";
std::cout << "You have 3 tries only.\n";
std::cout << "Starting with number 5.\n";
std::cout << " Write (H) if the next number is higher\n";
std::cout << " Write (L) if the next number is lower\n";

int mistakes = 0;
int correctGuesses;
int prevNum = 5, nextNum;
char choice;

do
{
srand ( time(NULL) );
do
nextNum = rand() % 11;
while (nextNum == prevNum);

std::cin >> choice;

if (choice == 'H')
{
if (prevNum < nextNum)
{
std::cout << "Correct! "<< nextNum << " is the new number.\n";
correctGuesses++;
}
else if (prevNum > nextNum)
{
std::cout << "Wrong choice! " << nextNum << " is the new number.\n";
mistakes++;
}
}

if (choice == 'L')
{
if (prevNum > nextNum)
{
std::cout << "Correct! "<< nextNum << " is the new number.\n";
correctGuesses++;
}
else if (prevNum < nextNum)
{
std::cout << "Wrong choice! " << nextNum << " is the new number.\n";
mistakes++;
}
}

prevNum = nextNum;
}
while(mistakes < 3);

std::cout << "GAME OVER!\n";
std::cout << "You Guess "<< correctGuesses << " correct answer.\n ";
}
int choice()
{
std::string reply;
double php, change, total, prc, qty;
reply='y';
while (reply=="y")
{
std::cout<<" Price: ";
std::cin>>prc;
std::cout<<" Quantity: ";
std::cin>>qty;
total=qty * prc;
total=total+total;
std::cout<<" Another Item? [y]/[n]: ";
std::cin>>reply;
}
std::cout<<"Total: "<<total <<"\n";
std::cout<<"Cash: ";
std::cin>>php;
change=php-total;
std::cout<<"Change: "<<change<<"\n";
}
int grade()
{
float p, m, sf, f, ave;
std::cout << " Prelim: ";
std::cin >> p;
std::cout << " Midterm: ";
std::cin >> m;
std::cout << " Semifinal: ";
std::cin >> sf;
std::cout << " Final: ";
std::cin >> f;
ave = (p+m+sf+f)/4;
{
if (ave >=75 && ave <=80)
std::cout << " The average is: " << ave << " Fair ";
else if (ave >=81 && ave <90)
std::cout << " The average is: " << ave << " Good ";
else if (ave >=91 && ave <=100)
std::cout << " The average is: " << ave << " Excellent ";
else
{
std::cout << "Invalid";
}
}
}
int arr()
{
int numbers[5];

std::cout << "Enter 5 numbers: \n";


for (int i = 0; i < 5; ++i) {
std::cin >> numbers[i];
}

std::cout << "The numbers are: ";


for (int n = 0; n < 5; ++n) {
std::cout << numbers[n] << "\n";
}
}
int evenOdd()
{
int i=0;
std::cout<<"Odd Numbers with CONTINUE statement\n";
do
{
if(i%2==0)
{
i++;
continue;
}
std::cout <<i++<<"\n";
}
while(i<10);
std::cout<<"Printing Odd or Even NUmber with SWITCH CASE BREAK statement\n";
i=0;
do
{
std::cout<<i++;
switch(i%2)
{
case 0: std::cout<<" is Even Number\n";
break;
case 1: std::cout<<" is Odd Number\n";
break;
}
}
while(i<9);
return 0;
}

Hello CStudet,

To start with:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Next you should compile the program and try to fix all the errors that you can before posting. If there is an error that you do not understand then post the whole error message and not what you think it means.

When I tried to compile the program I found that you are missing the header file "<string>". You may not have defined any "std::string"s, but it is needed for the "cout" statements.

I did notice that you are using the "\n" over "std::endl" and that is a good choice.

After I clean up the errors I will have a better idea what the program is doing.

Andy
Hello CStudet,

I have worked on your program a little and came up with 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
//#include <time.h>  // <--- Covered by "ctime". You do not need both.

int main()
{
    int pressMenu{};

    //////// generate random numbers /////////
    //std::srand(std::time(0));
    srand(static_cast<unsigned int>(time(nullptr)));  // <--- Only needs done once.

    std::cout <<
        "\n       PLEASE CHOOSE WISELY!!\n"
        " ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*\n\n"
        " +===============+  +===============+\n"
        " | (1) PROGRAMS  |  | (2) GAMES     |\n"
        " +===============+  +===============+\n"
        " | (3) EXIT      |\n"
        " +===============+\n"
        "  Enteer choice: ";
    std::cin >> pressMenu;

    switch (pressMenu)
    {
        case 1:
            (pressMenu == '1');  // <--- Not sure about the use of this yet.

            std::cout <<
                "\n\n           PROGRAMS\n"
                " +======================+  +======================+\n"
                " | (1) ARRAY            |  | (2) GRADING SYSTEM   |\n"
                " +======================+  +======================+\n"
                " +======================+  +======================+\n"
                " | (3) FUNCTION         |  | (4) SIMPLE GROCERY   |\n"
                " +======================+  +======================+\n\n";
            break;

Line 13 is the better way to write this line. Have a look at https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful Well worth the time to watch.

Writing the 2 "cout" statements this way has its advantages. First it becomes 1 very large string to print and it looks more what will display on the screen making it easier to change. You have made good use of the "\n" in each line, but you do not need a "cout" for each line. Also the insertion operator (<<) is not needed between the 2 halves of a line. You only need the (<<) when mixing strings and variables to chain them together.

Case 1 prints a nice menu, but you never use "cin" to make a choice. You just break out of the switch and go to something that looks like it should be in its own function. Case 1 should accept a choice from the menu and call the proper function before you leave the switch.

The same for case 2.

I added case 3 to exit the program. If you would use the switch properly then you could change the return to something else to exit the program.

What you should be doing is using "main" to direct the flow of the program and exit the program when you are finished.

I am thinking that the block of code that follows the switch should be in its own function and called from case 2.

This would allow you to use main to direct the program flow. Also you could put from line 15 to the end of the switch in a do/while loop to keep this running until you choose 3 from the main menu.

You could do the same with the sub-menus in cases 1 and 2 to leave the switch and return to the main menu. I have not worked on this part yet.

Andy
Topic archived. No new replies allowed.