Number List and Operations Project in C++

Number List and Operations Project in C++ (With Source Code)

Number List and Operations Project in C++ (With Source Code)

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create a Number List and Operations Project in C++ (With Source Code).

Number List and Operations Project in C++

A Number List and Operations Project implemented in C++ is a console based software which gives the user the ability to execute different operations on a list of integers. The user can add integers to the list by inputting the numbers. On the numbers inside the list, the users can perform as many operation they want to perform.The system displays a menu with choices for printing the numbers, adding numbers to the list, calculating the mean (average) of the numbers, finding the lowest number, finding the greatest number, and exiting the program.

Palindrome Checker using Stack in C++ (With Source Code)

In this project we will be using vector to store the entered numbers by the user. The user, by selecting items from the menu, may interact with the system. This system also deals with the errors as it prints out error statements whenever the user makes a wrong input.

Features of the System:

The features state below work together to provide a useful and user-friendly number management system that enables users to conduct a variety of actions on a list of numbers.

  1. Menu driven Interface as the system provides a menu-based interface which allows the user to select from several choices.
  2. Numbers can be added in the list by the user.
  3. The user can perform operations the numbers added in the list.
  4. The system can calculate mean.
  5. The system can display the smallest number in the list.
  6. The system can display the largest number in the list.
  7. The user can quit from the program and terminate execution.
  8. The system hadles the errors by printing error statements upon wrong inputs.
  9. The sorting technique is used while finding smallest and largest numbers.

Overview of the UI:

Print numbers
A – Add numbers
M – Display the mean of the numbers
S – Display the smallest number
L – Display the largest number
Q – Quit
Enter your choice:

The user will choose an option to perform the operation of their choice. User will then enter a values according to the prompts that will be displayed on the console to perform that operation.

 Number List and Operations Project in C++ Source code:

You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.

#include <iostream>
#include <string>
#include <vector>
#include <bits/stdc++.h>

using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::sort;

vector <int> list;
char get_user_input();
void process_user_input(char user_input);
bool PROGRAM_STATE = true;

int main () {
    
    while (PROGRAM_STATE)
    {
      char user_input =  get_user_input();
      process_user_input(user_input);
    }
    
    return 0;
}

char get_user_input() {

    cout << "\n" << "P - Print numbers" << "\n";
    cout << "A - Add numbers" << "\n";
    cout << "M - Display the mean of the numbers" << "\n";
    cout << "S - Display the smallest number" << "\n";
    cout << "L - Display the largest number" << "\n";
    cout << "Q - Quit" << "\n";
    cout << "Enter your choice: ";
    char user_input;
    cin >> user_input;
    return user_input;
}

void process_user_input(char user_input) {


    if (user_input == 'p' || user_input == 'P')
        { 
            if (list.size() > 0)
            {
                 cout << "\n" << "[";
                for (size_t i = 0; i < list.size(); i++)
                {
                cout << " " << list.at(i) << " ";
                }
                cout << "]" << "\n"; 
            }
            else
            {
                cout << "\n" << "[] - The list is empty" << "\n";
            }
            
        }

        else if (user_input == 'a' || user_input == 'A')
        {
            cout << "\n" << "Enter an integer to add to the list: ";
            long double new_user_input;
            cin >> new_user_input;

            list.push_back(new_user_input);

            cout  << "\n" << new_user_input << " added" << "\n";
        }

        else if (user_input == 'm' || user_input == 'M')
        {
            if (list.size() > 0)
            {
                double average, total {0};
                for (size_t i = 0; i < list.size(); i++)
                {
                total += list.at(i);
                };
                average = total/list.size();

                cout << "\n" << "The mean is: " << average << "\n";
            } else {
                cout << "\n" << "Unable to calculate the mean - no data" << "\n";
            }
            
        } 

        else if (user_input == 's' || user_input == 'S') 
        { 
            if (list.size() > 0)
            {
                sort(list.begin(), list.end());
                cout << "\n" << "The smallest number is: " << list.at(0) << "\n";
            } else {
                cout << "\n" << "Unable to calculate the smallest number - no data" << "\n";
            }
        }

        else if (user_input == 'l' || user_input == 'L') 
        { 
            if (list.size() > 0)
            {
                sort(list.begin(), list.end());
                cout << "\n" << "The largest number is: " << list.at(list.size()-1) << "\n";
            } else {
                cout << "\n" << "Unable to calculate the largest number - no data" << "\n";
            }
        }

        else if (user_input == 'q' || user_input == 'Q') 
        {
            PROGRAM_STATE = false;
        }
        
        else
        {
            cout << "Unknown selection, please try again." << "\n";
        }

}

Now let us understand the code:-

  • We will start by writing the header of the code with the required libraries – iostream (input and output stream), string (string manipulation), vector (for dynamic arrays), bits/stdc++.h ( to include all the standard libraries).
  • We will be using the ‘vector <int> list’ which basically declares a vector named list, it will store a list of integers.
  • We will declare get_user_input() function. This function will ask the user to enter their choice from a menu and then reads their input character.
  • Now declaring the process_user_input (char user_input) function. It will take the user’s input character and performs the corresponding operation based on the input by the user.
  •  We will initialize a boolean type variable called ‘PROGRAM_STATE’ to true and this variable will be required to control the program’s execution and it will determine whether the program should continue or not.
  • The main() function will contain a while loop and it will continue to run till the PROGRAM_STATE is true. Inside the loop we will call the get_user_input() function to get the user’s input and it will get assigned to a variable ‘user_input’. Then we will call the process_user_input() function to process the user’s input.
  • Now we will defined the functions after their main functions as we already made forward declarations for them.
  • The get_user_input() function will print using cout, the menu of operations, a user can perform in this system – Print numbers, Add numbers, Display the mean of the numbers, Display the smallest number, Display the largest number, Quit. The user will enter their choice of operation they want to perform and then the character will be returned from the function.
  • The process_user_input() function will take user_input as an argument. It will use nested if else if statements for the functionality.
  • For instance, if the user inputs ‘p’ or ‘P,’ then the function will determine whether or not the list vector is empty. If it is not empty, the function iterates across the vector, printing each member. If the list is empty, an empty list message is displayed.
  • If the input made by the user is ‘a’ or ‘A,’ then function will ask for an integer to add to the list. cin is used to read the input, and push_back() is used to add it to the list vector. The number added will be shown by a message.
  • The function computes the mean (average) of the values in the list vector if the user enters ‘m’ or ‘M’. If the list is not empty, the mean is calculated by adding the entire sum of the numbers and dividing it by the number of entries. The mean is then shown. If the list is empty, a notice stating that no data is available is displayed.
  • If the user makes an input of ‘s’ or ‘S,’ then the function will determine whether or not the list vector is empty. If it is not empty, it uses sort() to sort the vector in ascending order and shows the lowest number (first element). If the list is empty, a notice stating that no data is available is displayed.
  • If the user makes an input of ‘l’ or ‘L,’ then the  function will determine whether or not the list vector is empty. If it is not empty, it uses sort() to sort the vector in ascending order and shows the greatest number (last element). If the list is empty, a notice stating that no data is available is displayed.
  • If the user makes an input of ‘q’ or ‘Q,’ then the variable PROGRAM_STATE will be set to false value and it will cause the while loop in the main() function to finish. The program will terminate.
  • This sums up our project of Creating a Number List and Operations Project in C++.

Personal Diary Management System using C++ (With Source Code)

Final Output:-

Here is an example to show how this project works.

Number List and Operations Project in C++

  • The user will enter their choice. We have added 2 numbers – 7 and 12 in the list.

Number List and Operations Project in C++

Number List and Operations Project in C++

  • Again the main menu appears and now we will perform the operations.

Number List and Operations Project in C++

Number List and Operations Project in C++

Number List and Operations Project in C++

Number List and Operations Project in C++

Video Output:

Conclusion

We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

Player Management System using C++ (With Source Code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Follow: CodewithRandom

ADVERTISEMENT



Leave a Reply