-2

I have 5 programs that run perfectly fine individually, but when I combine them I get error messages and won't build. I have a menu to pick which program to run using if statements. Also a do while loop to repeat the programs. I believe it has something to do with the functions because I haven't had this problem before with simple programs. The program should first ask which program you want to run from the menu. It will run that program, then ask if you want to repeat.

I don't know what to try other than what is now in the program. I did take the do while loop out but still had the issue with the if statements.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
int choice;
char repeat;
if (repeat == 'm' || 'M'){
cout << "1. Perfect Scores\n"
"2. Larger Than n\n"
"3. Roman Numeral Converter\n"
"4. Monkey Business\n"
"5. Lottery\n"
"6. Exit\n";


cout << "Pick which program you would like to run." << endl;
cin >> choice;
}


else if (choice == 1){  //  -----------Perfect Scores------------
    do{
int countPerfect(int a[])
{
int i=0;
for(int l=0; l<10; l++)
    if(a[1]==100)
        i++;
return i;
}

{
int score[10];
for(int i=0; i<10; i++)
{
    cout << "Enter score " << i+1 << endl;
    cin >> score[i];
    while(score[i]<0 || score[i] > 100)
    {
        cout << "Enter score between 1 and 100." << endl;
        cin >> score[i];
    }
}
int n = countPerfect(score);
cout << "No of perfect scores: " << n << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
        }while(repeat == 'Y' || repeat == 'y');
}
//        ------------Larger Than n--------------
   else if (choice == 2){
       do{
void display_greator(int A[], int size, int n)
{
int i;
for(i=0; i< size; i++)
{
    if(A[i]>n)
    {
        cout << A[i] << endl;
    }
}
}

int main(void)
{
int i, size;
cout << "Enter the size of your array:"<< endl;
cin >> size;
int N[size];
cout << "Enter a list of " << size << " numbers:" << endl;
for( i=0; i<size; i++)
{
    cin >> N[i];
}
int num;
cout << "Enter your number n:" << endl;
cin >> num;
display_greator(N, size, num);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
       }while(repeat == 'Y' || repeat == 'y');
}
//  ----------------------Roman Numeral Converter------------------
else if (choice == 3){
    char repeat;
    do{
{
int n;
string romanNumbers[]={"I", "II", "III", "IV", "V", "VI", "VII",
    "VIII", "IX", "X", "XI", "XII",
    "XIII", "VIX", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"};
cout << "Enter a decimal number or enter 0 to quit."  << endl;
cin >> n;
if(n==0)
exit(0);
do
{

    cout << "Enter number between 1 and 20" << endl;
    cout << "Enter number or enter 0 to quit" << endl;
    cin >> n;
} while(n < 0 || n > 20);
{
    cout << "Enter decimal equivalent roman number:" << endl;
    cout << "Enter a number between 1 and 20:" << endl;
    cin >> n;
    if(n==0)
        exit(0);
    cout << "To repeat, press Y. For main menu, press M" << endl;
    cin >> repeat;
}
while(n > 0 || n < 20);
return 0;
}
        }while(repeat == 'Y' || repeat == 'y');
}
//    ---------------Monkey Business--------------------
else if(choice == 4){
   do{
const int DAYS = 7;
double getTotalAmountOfFood(int[][DAYS],int);
double getLeastAmountOfFood(int[][DAYS],int, double);
double getGreatestAmountOfFood(int[][DAYS],int, double);

{

    const int MONKEYS = 3;
    double totalFood, averageFood, leastFood, greatestFood;
    int foodInfo[MONKEYS][DAYS];

    for(int i= 0; i< MONKEYS; i++)
    {
        cout << "Enter the food information of the monkey" <<
        (i + 1) << ":" << endl;
        for(int j = 0; j < DAYS; j++)
        {
            cout << "Day" << (j + 1) << ":" << endl;
            cin >> foodInfo[i][j];
            while(foodInfo[i][j] < 0)
            {
                cout << "Day " << (j+1) << ":" << endl;
                cin >> foodInfo[i][j];


            }
        }
        cout << endl;
    }
    totalFood = getTotalAmountOfFood(foodInfo, MONKEYS);
    leastFood = getLeastAmountOfFood(foodInfo, MONKEYS, totalFood);
    greatestFood = getGreatestAmountOfFood(foodInfo, MONKEYS, 0);
    averageFood = totalFood / DAYS;
    cout << "The average amount of food per day for three monkeys(in pounds):"
    << averageFood << endl;
    cout << "The least amount of food per week for monkeys(in pounds) is:"
    << leastFood << endl;
    cout << "The greatest amount of food per wek for a monkey is(in pounds):"
    << greatestFood << endl;
    cout << "To repeat, press Y. For main menu, press M" << endl;
    cin >> repeat;
    return 0;
}

double getTotalAmountOfFood(int food[][DAYS],int mnks)
{
    double total = 0;

    for(int i = 0; i < mnks; i++)
    {
        for(int j = 0; j < DAYS; j++)
        {
            total += food[i][j];
        }
    }
    return total;
}
double getLeastAmountOfFood(int food[][DAYS], int mnks, double leastAmount)
{
    double least = leastAmount;
    double weekTotal;
    for(int i = 0; i < mnks; i++)
    {
        weekTotal = 0;
        for(int j = 0; j < DAYS; j++)
        {
            weekTotal += food[i][j];
        }
        if(least > weekTotal)
            least = weekTotal;
    }
    return least;
}
double getGreatestAmountOfFood(int food[][DAYS], int mnks, double greatestAmount)
{
    double greatest = greatestAmount;
    double weekTotal;
    for(int i = 0; i < mnks; i++)
    {
        weekTotal = 0;
        for(int j = 0; j < DAYS; j++)
        {
            weekTotal +=food[i][j];
        }
        if(greatest < weekTotal)
            greatest = weekTotal;
    }

    return greatest;
}
       }while(repeat == 'Y' || repeat == 'y');
}
//                    ----------------Lottery--------------------
else if (choice == 5){
    char repeat;
    do{

    srand(time(NULL));
    int winningDigits[5];
    int player[5];
    int num;
    int matchCount = 0;

    for (int i = 0; i < 5; i++)
    {
        winningDigits[i] = rand() % 10;
    }

    cout << "Enter 5 integers in the range of 0 to 9." << endl;
    for(int i = 0; i < 5; i++)
    {
        cout << "Number #" << (i + 1) << ": " << endl;
        cin >> num;

        while (num < 0 || num > 9)
        {
            cout << "Invalid number! It should be in the range of 0 through 9." << endl;
            cout << "Number #" << (i + 1) << ": " << endl;
            cin >> num;
        }
        player[i] = num;
    }
    for (int i = 0; i < 5; i++)
    {
        if (winningDigits[i] == player[i])
        {
            matchCount++;
        }
    }
    cout << "Winning digits: " << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << winningDigits[i] << " " << endl;
    }
    cout << "Player's digits: " << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << player[i] << " " << endl;
    }
    cout << endl << endl << "Number of digits matched: " 
<< matchCount << endl;
    cout << "To repeat, press Y. For main menu, press M" << endl;
    cin >> repeat;
    return 0;

        }while(repeat == 'Y' || repeat == 'y');
}
else if (choice == 6)
{cout << "Bye" << endl;}
}


I'm expecting to be able to choose a program to run, repeat it, and     repeat the entire program from main menu.
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 6
    What are the error messages? – Vlad from Moscow Oct 17 '19 at 15:50
  • 4
    There are two main functions. That's not allowed. `int N[size];` is variable-length array. That's not defined in ISO C++. You should use std::vector, dynamic memory allocation or an fixed array. https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – Thomas Sablik Oct 17 '19 at 15:52
  • 1
    @Vlad - Lots of them, I would imagine! – Adrian Mole Oct 17 '19 at 15:54
  • 1
    You can't start an `if` statement with `else if`. That first one needs to be just `if`. – computercarguy Oct 17 '19 at 15:54
  • 3
    You can't define function inside other functions. – Adrian Mole Oct 17 '19 at 15:54
  • 3
    Step 1: Read the error messages. – eerorika Oct 17 '19 at 16:02
  • ` char repeat;` is used uninitialized. This causes undefined behavior. `if (repeat == 'm' || 'M')` does not do what you expect. – Thomas Sablik Oct 17 '19 at 16:03
  • `if (repeat == 'm' || 'M')` this condition can never be false because `'M'` is a non-zero constant. – eerorika Oct 17 '19 at 16:03
  • I tried to reformat the code to make it more readable, but there are so many problems with this code that I couldn't figure out the proper way to format it. There's missing and extra parens, missing << in `cout`, and a few other things (besides me being a little rusty in C++), that prevent this code from being compilable. The OP needs a decent IDE and JIT compiler to help them figure this out. There's too much going on that needs fixed for us to be completely accurate in advice. – computercarguy Oct 17 '19 at 16:05
  • "{ function not allowed here"... – Justin Windham Oct 17 '19 at 16:08
  • Consider partitioning your code to use functions. Instead of "else if (choice == 1) // -----------Perfect Scores------------", consider "else if (choice == 1) { PerfectScores() }, etc ... I see 5 or 6 should-be-functions identified by comments and braces. This will clean up your code, and make it easier to balance your braces. – 2785528 Oct 17 '19 at 19:43

1 Answers1

0

I am not going to point out all errors in your code, they are just too many. You went too fast too far. If you think the complexity of code and the errors it procudes are intimidating you are right. I wrote codes with more lines, but yours is too complicated for me. Go in small steps. Start with something along the line of:

int choose() { return 0; }

void func1() {}
void func2() {}

int main() {
    int choice = 0;
    while ( choice = choose() ) { 
        switch(choice) {
            case 1 : func1(); break;
            case 2 : func2(); break;
        }
    }
}

Your main function does not have to be more complex than that.

Write this, not more. Make sure it compiles, then in tiny steps fill the gaps, after each step compile, see if it does what you expect and only then continue to put more.

Some problems in your code (partly stolen from comments):

  • you cannot have more than one main
  • you cannot define functions inside functions
  • you cannot start an if statement with else if
  • int N[size]; is not standard C++ for a non-compile-time-constant size. Use std::vector instead
  • see here why using namespace std is considered bad practice
  • if (repeat == 'm' || 'M') is not doing what you expect, it should be if (repeat == 'm' || repeat == 'M'). In yours 'M' is taken as a bool which is always true (because it isnt 0).
  • make sure to initialize variables. Using variables that are not initialized causes undefined behaviour.
  • please next time reduce your code to a mcve and try to concentrate on a single problem, also include the error in the question
  • I cannot help myself than to point that for each single problem there are duplicate questions, which brings me back to: Don't do too many things at once. Fixing 100 errors at once is extremely difficult, fixing 1 error is doable.
  • last but not least, pay attention to compiler errors and warnings while you write the code (again: not after you wrote several pages, but after each single line)
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185