0

I'm new to C++. I have errors. But, i dont know how to fix it. Could anyone please help me? Thank you. P - Print numbers A - Add a number M - Display mean of the numbers S - Display the smallest number L - Display the largest number Q - Quit

Errors : expected unqualified id before return 0 
         error : expected ';' before {}
   #include <iostream>
#include <vector>
using namespace std;
int main(){
    char input {};
    vector <double> numbers {};
    int number{};
    int sum{};
    int min_number{};
    int max_number{};
    bool condition {true};
    cout << "Enter a command" << endl;
    cin >> input;
    if(numbers.size() > 0){
    while(condition){
        if (input == 'P' || input == 'p'){
            for(auto x: numbers)
                cout << x << endl;
        }
        else if(input == 'A' || input == 'a'){
            cout << "Enter a number";
            cin >> number;
            numbers.push_back(number);
        }
        else if(input == 'M' || input == 'm'){
            for(auto x : numbers)
                sum += x;
        cout << sum / numbers.size() << endl;
        }
        else if(input =='S' || input == 's'){
                for(size_t i {0}; i < numbers.size(); ++i)
                    if(numbers.at(i) < min_number)
                        min_number =numbers.at(i);
                        }
         else if(input =='L' || input == 'l'){
                for(size_t i {0}; i < numbers.size(); ++i)
                    if(numbers.at(i) > max_number)
                        max_number =numbers.at(i);
            }
        else if(input =='Q' || input == 'q'){
            condition {false};
            
            }
        }
    cout << "[] - list is empty, unable to calculate" << endl;
    }
    
    return 0;
}
greyk0
  • 35
  • 5

2 Answers2

2

In your section dealing with Q/q, the statement:

condition {false};

is not a valid form of assignment, you should instead use:

condition = false;

The braces are fine for initialisation, but that's not what you're trying to do on that line.


As an aside, this line:

if(numbers.size() > 0){

seems a little strange. Since you initialise the list to empty, the main loop will never start (because it's inside the if block) even though you have already asked the user for input.

That's a runtime error rather than a syntax error but you'll still need to fix it at some point.

I suspect that particular should should be done only as part of the calculation of the mean, so as to avoid dividing by zero.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

I have written this for you. Since, you're a learner, I think that you should be practicing better things like STL functions and not using using namespace std; at top.

You may find some things new, but don't be frightened, just search them on some website like cppreference and see what that entity do and how to effectively use it.

There were many logical errors. @paxdiablo has mentioned them in his answer. I have removed every of them and this code works.

#include <algorithm>
#include <cctype>
#include <iostream>
#include <vector>

int main() {
    std::vector<double> numbers;
    while (true) {
        char input;
        std::cout << "Enter a command: ";
        std::cin >> input;
        switch (std::toupper(input)) {
        case 'P':
            if (numbers.empty())
                std::cerr << "The list is empty!" << std::endl;
            else {
                for (auto &&i : numbers)
                    std::cout << i << ' ';
            std::cout << std::endl;
            }
            break;
        case 'A': {
            int number;
            std::cout << "Enter a number: ";
            std::cin >> number;
            numbers.push_back(number);
            break;
        }
        case 'M':
            if (numbers.empty())
                std::cerr << "The list is empty! Cannot perform the operation!!";
            else {
                int sum = 0;
                for (auto &&i : numbers)
                    sum += i;
                std::cout << "Mean: " << (sum / numbers.size()) << std::endl;
            }
            break;
        case 'S':
            std::cout << "Smallest Number: " << *std::min_element(numbers.begin(), numbers.end()) << std::endl;
            break;
        case 'L':
            std::cout << "Largest Number: " << *std::max_element(numbers.begin(), numbers.end()) << std::endl;
            break;
        case 'Q':
            return 0;
        default:
            std::cerr << "Unrecognised Command!!" << std::endl;
        }
    }
    return 0;
}
brc-dd
  • 10,788
  • 3
  • 47
  • 67