Output formatting and Inputting lower/upper

Hello All! I was trying to finish up my program and I had the majority of the program finished however my output is slightly off and I'm wondering how to fix it.

Sample of code (excluding main, other functions)

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
void removeOption(vector<int> &selectedPrices, vector<string> &selectedOptions, vector<int> price, vector<string> name)
{
    // Executes following code if no options were chosen
    if (selectedPrices.empty())
    {
        cout << "No options chosen" << endl; // Tells user no options were chosen
        return;                              // Exits out of function
    }

    string option; // Stores user input for choosing an option to remove
    cout << endl;
    cout << "Enter what option you want to remove: "; // Ask user to remove an option
    getline(cin, option);                             // Gets the entire user's input, including whitespaces

    // Counts through the vector selectedOptions to find a match for the inputted string
    for (size_t i = 0; i < selectedOptions.size(); i++)
    {
        // Executes following code if match was found
        if (option == selectedOptions[i])
        {
            cout << option << " removed" << endl;                 // Tells user option was removed
            selectedOptions.erase(selectedOptions.begin() + i);   // Erases the specific element in the vector selectedOptions
            selectedPrices.erase(selectedPrices.begin() + i + 1); // Erases the specific element in the vector selectedPrices
            return;                                               // Exits out of function
        }
    }

void selectOption(vector<int> &selectedPrices, vector<string> &selectedOptions, vector<int> price, vector<string> name)
{
    // Checks if the user has added more than five options
    if (selectedOptions.size() > 5)
    {
        cout << "Maxed out number of options." << endl;
        return; // Exits out of function
    }

    string option;                  // Stores user input for choosing an option to add
    cout << "Enter in an option: "; // Ask user to enter in an option
    getline(cin, option);           // Gets the entire user's input, including whitespaces

    // Counts through the vector selectedOptions to make sure user is not adding an already added option
    for (size_t i = 0; i < selectedOptions.size(); i++)
    {
        // Executes the following code if user's inputted string is the same as one in the vector selectedOptions
        if (option == selectedOptions[i])
        {
            cout << option << " already added" << endl; // Tells user the option was already added
            return;                                     // Exits out of function
        }
    }

    // Counts through the vector name to find a match for the inputted string
    for (size_t i = 0; i < name.size(); i++)
    {
        // Executes following code if match was found
        if (option == name[i])
        {
            cout << option << " added" << endl; // Tells user the option was added

            selectedOptions.push_back(name[i]); // Stores the value of the specific element in the vector name into the vector selectedOptions
            selectedPrices.push_back(price[i]); // Stores the value of the specific element in the vector price into the vector selectedPrices
            return;                             // Exits the function
        }
    }

    // Executes following code if no option was found
    if (option != name.back())
    {
        cout << "Option not found, try again." << endl;             // Tells user no option was found
        selectOption(selectedPrices, selectedOptions, price, name); // Display model, total price, and added options
    }
}

void displayOption2(vector<int> &price, vector<string> &name)
{
    // Show all available options
    cout << "Prices for model E, L, & X: $10000.00, $12000.00, $18000.00" << endl;
    cout << "Available Options:" << endl
         << endl;

    // Prints out three options then inserts a new line
    for (size_t i = 0; i < price.size(); i++)
    {
        if (i % 3 == 2)
        {
            cout << name[i] << " ($" << price[i] << ") " << endl; // Prints out name of option and price of option, then inserts a newline
        }
        else
        {
            cout << name[i] << " ($" << price[i] << ") "; // Prints out name of option and price of option,
        }
    }
    cout << endl;
}


Declarations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>
using namespace std;

void menu();
void fillUp(vector<int> &price, vector<string> &name);
void showModel(vector<int> &selectedPrices, vector<string> selectedOptions, vector<char> &selectedModel);
void selectModel(vector<int> &selectedPrices, vector<char> &selectedModel);
void selectOption(vector<int> &selectedPrices, vector<string> &selectedOptions, vector<int> price, vector<string> name);
void removeOption(vector<int> &selectedPrices, vector<string> &selectedOptions, vector<int> price, vector<string> name);
void displayOption2(vector<int> &price, vector<string> &name);
void cancelOrder(vector<int> &selectedPrices, vector<string> &selectedOptions, vector<char> &selectedModel);
bool check(int &choice, vector<int> &checkNumber);


Output Inital:
Leather Seats ($5000) DVD System ($1000) 10 Speakers ($800)
Navigation System ($1400) CarPlay ($500) Android Auto ($500)
Lane Monitoring ($2000) 3/36 Warranty ($800) 6/72 Warranty ($999)
Dual Climate ($1500) Body Side Molding ($225) Cargo Net ($49)
Cargo Organizer ($87) 450W Audio ($700) Heated Seats ($1000)


Desired Output:


Leather Seats($5000)     DVD System($1000)        10 Speakers($800)
Navigation System($1400) CarPlay($500)            Android Auto($500)
Lane Monitoring($2000)   3/36 Warranty($800)      6/72 Warranty($999)
Dual Climate($1500)      Body Side Molding($225)  Cargo Net($49)
Cargo Organizer($87)     450W Audio($700)         Heated Seats($1000)


Also whenever I want to add an option to the car I want the input to not be case sensitive.

Example:

1. Select a model (E, L, X)
2. Display available options and prices.
3. Add an option.
4. Remove an option.
5. Cancel order.
6. Quit.
Enter a choice: 3
Enter in an option: 450w aUdiO 


Rather than having to type in "450W Audio" I could type "450w AuDiO" or "450w audio"
Last edited on
I could include extra context if needed
Rather than having to type in "450W Audio" I could type "450w AuDiO" or "450w audio"
Write a helper function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
#include <iostream>
#include <string>
#include <cctype>

std::string str_tolower(std::string str)
{
    using std::tolower;

    for (char& ch : str)
    {
        ch = tolower(ch);
    }
    return str;
}

int main()
{
  std::cout << str_tolower("GoOdbYe") << '\n';
}


Desired Output:

Look up the functionality found within the <iomanip> header. I think it might also be easiest to use something like a stringstream, to concatenate the name and price together into one string. This makes it easier to work with the <iomanip> operations like std::left (left justify) std::setw (set width when printing).

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
// Example program
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;

struct Item {
    string name;
    int price;
};

std::string to_string(const Item& item)
{
    std::ostringstream oss;
    oss << item.name << "(" << item.price << ")";
    return oss.str();
}

int main()
{
    Item items[] = {
        {"Leather Seats", 5000}, {"DVD System", 1000},  {"Speakers", 800},
        {"Navigation", 1400},    {"Android", 500},     {"etc", 0}
    };
 
    int i = 0;
    for (const auto& item : items)
    {
        std::cout << std::left << std::setw(30) << to_string(item) << ' ';
        
        if (i++ % 3 == 2)
            cout << '\n';
    }
}

Leather Seats(5000)            DVD System(1000)               Speakers(800)                  
Navigation(1400)               Android(500)                   etc(0)   


Edit: I keep forgetting std::to_string exists now. You don't need stringstreams.
1
2
3
4
std::string to_string(const Item& item)
{
    return item.name + "(" + std::to_string(item.price) + ")";
}
Last edited on
Error message being thrown with the operator+

error: no match for 'operator+' (operand types are 'std::vector<std::__cxx11::basic_string<char> >' and 'const char [2]')
return name + "(" + to_string(price) + ")";


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
void displayOption2(vector<int> &price, vector<string> &name)
{
    // Show all available options
    cout << "Prices for model E, L, & X: $10000.00, $12000.00, $18000.00" << endl;
    cout << "Available Options:" << endl
         << endl;

    // Prints out three options then inserts a new line
    for (size_t i = 0; i < price.size(); i++)
    {
        if (i % 3 == 2)
        {
            cout << left << setw(30) << to_string(price, name) << endl; // Prints out name of option and price of option, then inserts a newline
        }
        else
        {
            cout << left << setw(30) << to_string(price, name) << endl; // Prints out name of option and price of option,
        }
    }
    cout << endl;
}


string to_string(vector<int> &price, vector<string> &name)
{
    return name + "(" + to_string(price) + ")";
}
Trying to create a string out of an entire vector of prices ain't gonna work. A vector isn't a numeric value no matter what it contains.
Well all I'm trying to do is format the options so it displays like this:

Leather Seats($5000)     DVD System($1000)        10 Speakers($800)
Navigation System($1400) CarPlay($500)            Android Auto($500)
Lane Monitoring($2000)   3/36 Warranty($800)      6/72 Warranty($999)
Dual Climate($1500)      Body Side Molding($225)  Cargo Net($49)
Cargo Organizer($87)     450W Audio($700)         Heated Seats($1000)


Rather than this:
Leather Seats ($5000) DVD System ($1000) 10 Speakers ($800)
Navigation System ($1400) CarPlay ($500) Android Auto ($500)
Lane Monitoring ($2000) 3/36 Warranty ($800) 6/72 Warranty ($999)
Dual Climate ($1500) Body Side Molding ($225) Cargo Net ($49)
Cargo Organizer ($87) 450W Audio ($700) Heated Seats ($1000)


Main:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

void menu();
void fillUp(vector<int> &price, vector<string> &name);
void showModel(vector<int> &selectedPrices, vector<string> selectedOptions, vector<char> &selectedModel);
void selectModel(vector<int> &selectedPrices, vector<char> &selectedModel);
void selectOption(vector<int> &selectedPrices, vector<string> &selectedOptions, vector<int> price, vector<string> name);
void removeOption(vector<int> &selectedPrices, vector<string> &selectedOptions, vector<int> price, vector<string> name);
void displayOption2(vector<int> &price, vector<string> &name);
void cancelOrder(vector<int> &selectedPrices, vector<string> &selectedOptions, vector<char> &selectedModel);
bool check(int &choice, vector<int> &checkNumber);
int main()
{
    int choice; // Stores in user choice

    vector<int> price;              // Stores in the prices of the options/model from the options.txt file
    vector<int> selectedPrices;     // Stores in the prices from the added options the user has selected
    vector<string> name;            // Stores in the names  of the options from the options.txt file
    vector<string> selectedOptions; // Stores in the options' names from the added options the user has selected
    vector<char> selectedModel;     // Stores the chosen model name from the user
    vector<int> checkNumber;        // Stores in user inputs for selecting choices in menu

    fillUp(price, name);                                       // Load objects from options.txt file
    showModel(selectedPrices, selectedOptions, selectedModel); // Display model, total price, and added options
    menu();                                                    // Display menu

    // Have user input a choice
    while (cin >> choice)
    {
        cin.ignore();

        // First choice
        if (choice == 1)
        {
            // Checks to make sure options were already displayed. Result if user did print out options
            if (check(choice, checkNumber) == 1)
            {
                selectModel(selectedPrices, selectedModel); // Have user select model
                cout << endl;
                showModel(selectedPrices, selectedOptions, selectedModel); // Display model, total price, and added options
                menu();                                                    // Display menu
            }

            // Result if user did not print out options
            else
            {
                cout << "Please Display options first" << endl
                     << endl;                                              // Prompts user to display options first
                showModel(selectedPrices, selectedOptions, selectedModel); // Display model, total price, and added options
                menu();                                                    // Display menu
            }
        }

        // Choice 2
        else if (choice == 2)
        {
            check(choice, checkNumber); // Reads in the user has selected choice 2
            cout << endl;
            displayOption2(price, name);                               // Display options and prices
            showModel(selectedPrices, selectedOptions, selectedModel); // Display model, total price, and added options
            menu();                                                    // Display menu
        }

        // Choice 3
        else if (choice == 3)
        {
            // Executes following code if no model is selected
            if (selectedModel.empty())
            {
                cout << "Please select a Model first" << endl
                     << endl;                                              // Tells user to first select model
                showModel(selectedPrices, selectedOptions, selectedModel); // Display model, total price, and added options
                menu();                                                    // Display menu
            }

            // Executes following code a model is selected
            else
            {
                selectOption(selectedPrices, selectedOptions, price, name); // Have user add an option
                cout << endl;
                showModel(selectedPrices, selectedOptions, selectedModel); // Display model, total price, and added options
                menu();                                                    // Display menu
            }
        }

        // Choice 4
        else if (choice == 4)
        {
            removeOption(selectedPrices, selectedOptions, price, name); // Have user remove option
            cout << endl;
            showModel(selectedPrices, selectedOptions, selectedModel); // Display model, total price, and added options
            menu();
        }

        // Choice 5
        else if (choice == 5)
        {
            // Executes following code if no model is selected
            if (selectedModel.empty())
            {
                cout << "Please Select a Model first" << endl
                     << endl;                                              // Tells user to first select model
                showModel(selectedPrices, selectedOptions, selectedModel); // Display model, total price, and added options
                menu();                                                    // Displays menu
            }

            // Executes following code a model is selected
            else
            {
                cancelOrder(selectedPrices, selectedOptions, selectedModel); // Cancels order
                menu();                                                      // Displays menu
            }
        }

        // Choice 6
        else if (choice == 6)
        {
            cout << "Exiting program..." << endl; // Tells user program is exiting
            break;                                // Breaks out of while loop
        }

        // Misc input
        else
        {
            cout << "Wrong input, try again" << endl
                 << endl;                                              // Tells user to enter a choice again
            showModel(selectedPrices, selectedOptions, selectedModel); // Display model, total price, and added options
            menu();                                                    // Displays menu
        }
    }
    return 0; // Exits main function, causing the program to close
}
Last edited on
Well all I'm trying to do is format the options so it displays like this

@OP Write a small and separate program to format some sample data and concentrate on that. It's much easier than going down the same rabbit hole each time.
The answer to the formatting is in bold, the other stuff in this is beside the point but would dramatically simplify your (unswitched) effort.

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 <vector>
#include <iomanip>

struct Option
{
    std::string name;
    int price{};
    Option(std::string aName, int aPrice):name(aName),price(aPrice){};
    
    std::string option_string()
    {
        return name + '(' + std::to_string(price) +')';
    }
};

int main()
{
    std::vector<Option> list_of_options;
    list_of_options.push_back( Option("Leather Seats", 5000) );
    list_of_options.push_back( Option("DVD System", 1000) );
    list_of_options.push_back( Option("Navigation System", 1400) );
    list_of_options.push_back( Option("CarPlay", 500) );
    list_of_options.push_back( Option("Android Auto", 500) );
    
    int column{0};
    for(int i = 0; i < list_of_options.size(); i++)
    {
        column++;
        std::cout
        << std::setw(21) << std::left << list_of_options[i].option_string();
        
        if(column % 3 == 0)
            std::cout << '\n';
    }
    std::cout << '\n';
    
    return 0;
}



Leather Seats(5000)  DVD System(1000)     Navigation System(1400)
CarPlay(500)         Android Auto(500)    
Program ended with exit code: 0


PS Just saw it, Gonads had already shown you this by the look of it!
Last edited on
Well all I'm trying to do is format the options so it displays like this

C++20 introduced std::format to the toolbox, making it easy to do formatted output.
https://en.cppreference.com/w/cpp/utility/format/format

Not all current C++ compilers have implemented the <format> library yet, there is the fmt library.
https://fmt.dev/latest/syntax.html
Topic archived. No new replies allowed.