Alignment in an Array Problem

I have a problem in picking the show item list. How can I align the inputs on the table and what kind of condition can I do to only out out "no records found" when there is no input. Can someone lend me a hand with this? Thank you.

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const int rowlimit = 10; //expand easily our array

string ar_id [rowlimit] = {};//array limit of id is 100
string ar_name [rowlimit] = {};// array limit of name is 100
string ar_price [rowlimit] = {}; //array limit of price is 100
string ar_quantity [rowlimit] = {}; //array limit of quantity is 100

void ADDITEM ()
{
 char id [100]; //character limit of id is 100
 char name [100]; //character limit of name is 100
 char price [100]; //character limit of price is 100
 char quantity [100]; //character limit of quantity is 100
 cin.ignore ();
 char pick; //(y/n)
 cout << "\t\t\t\t\t\t\b\b\b\b\b\b\b\bID: ";
 cin.getline(id, 100);
 cout << "\t\t\t\t\t\t\b\b\b\b\b\b\b\bNAME: ";
 cin.getline(name, 100);
 cout << "\t\t\t\t\t\t\b\b\b\b\b\b\b\bPRICE: ";
 cin.getline(price, 100);
 cout << "\t\t\t\t\t\t\b\b\b\b\b\b\b\bQUANTITY: ";
 cin.getline(quantity, 100);
 cout << "\v\t\t\t\t\t\t\b\b\b\b\b\b\b\bSave the Record (y/n): "; 
 cin >> pick; 


for (int x = 0; x < rowlimit; x++)
 {

{       if (pick == 'y') { 
        if (ar_id[x] == "\0")
        {
          ar_id [x] = id;
          ar_name [x] = name;
          ar_price [x] = price;
          ar_quantity [x] = quantity;
          break; } //TOL PACHECK NITO
        }
    }
}

}

void SHOWITEM ()
{
    system ("clear");
    cout << "ITEM LIST" << endl;
    cout << "==============================================================" << endl;

    int counter = 0;
    cout << "       ID.      |        Name      |   Price   |   Quantity |" << endl << "==============================================================\n";
    for (int x =0; x <rowlimit; x++)
    {

        counter++;
        cout << ar_id [x]<< "\t\t\t" << ar_name [x]<<"\t\t" << ar_price[x] <<"\t\t"<< ar_quantity[x] << endl;
    }


if (counter ++)0;
{
    cout << "no record found!" << endl;

}
cout << "==============================================================" << endl;

}

int main(){ // our function needs to return some integer at the end of the execution
std :: cout  << "MENU\n";
int optionnum; //store the number that wil lbe used as our corresponding command to our menu
system ("clear"); //clear the screen

do { // do-while looping statement
cout << "\n     ---------------------------------------------------------------------------------------------" << endl; //print the output
cout << "\t\t\t\t\t\t\b\b\b\b\bMAIN MENU" << endl; //print the output
cout << "     ---------------------------------------------------------------------------------------------" << endl; //print the output
cout << "\v\t\t\t\t\t\t\b\b\b\b\b\b\b\b[1] - ADD ITEM" << endl; //print the output
cout << "\t\t\t\t\t\t\b\b\b\b\b\b\b\b[2] - SEARCH ITEM" << endl; //print the output
cout << "\t\t\t\t\t\t\b\b\b\b\b\b\b\b[3] - DELETE ITEM" << endl; //print the output
cout << "\t\t\t\t\t\t\b\b\b\b\b\b\b\b[4] - SHOW ITEM LIST" << endl; //print the output
cout << "\t\t\t\t\t\t\b\b\b\b\b\b\b\b[5] - BACK TO MENU" << endl; //print the output
cout << "     ---------------------------------------------------------------------------------------------" << endl; //print the output
cout << "\n\v\tPick a Number from the Items Above: "; //print the output
cin >> optionnum; //store and integer "optionnum" from the printed outputs below

switch  (optionnum) //provides an easy way to dispatch execution to different parts of code
{
    case 1: ADDITEM (); //function of ADDITEM
    system ("clear"); //clear the screen
    break; //terminate the case


    case 4: SHOWITEM ();
    system ("clear");
    break;

}





} while (optionnum != 6); //do-while looping statement if pressed X should back to menu (not yet sure, not included in the instructions, PLEASE FIX IT)

return 0; //return statement "exit"
}
Last edited on
have a look at cout's specifiers. setw specifically may help here.
http://faculty.cs.niu.edu/~hutchins/csci241/output.htm

not sure what you meant on the no records, but you may need to track the # of records the user inputs in a variable. say your array is 100 long and the user puts in 42 records. Then you have counted and stored 42 somewhere (Ill call it numberused), so you can say for(i = 0; i < numberused; i++) cout << array[i]
maybe that is what you are asking. If numberused is zero (it should start at 0) you can print a special no-data-at-all message.
> How can I align the inputs on the table?

C++20 (eg. Visual Studio 2022 with /std:c++20): use std::format

For example:
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
#include <iostream>
#include <string>
#include <format> // C++20

void show_items( const std::string id[], const std::string name[],
                 const double price[], const int qty[], std::size_t num_items )
{
    if( num_items == 0 )
    {
        std::cout << "there are no records\n" ;
        return ;
    }

    // https://en.cppreference.com/w/cpp/utility/format/format
    // https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specification
    static const std::string_view hdr_fmt = "{: ^10}{: ^40}{: >10}{: >5}" ;
    static const std::string_view item_fmt = "{: ^10}{: ^40}{: >10.2f}{: >5}" ;

    std::cout << std::format( hdr_fmt, "id", "name", "price", "qty" ) << '\n' // print header
              << std::string( 65, '-' ) << '\n' ;

    for( std::size_t i = 0 ; i < num_items ; ++i ) // print each item
        std::cout << std::format( item_fmt, id[i], name[i], price[i], qty[i] ) << '\n' ;
}

int main()
{

    const std::string id[100] { "abcd", "efghij" } ;
    const std::string name[100] { "ABcdefgh12345", "IJjklmnopqrst6789" } ;
    const double price[100] { 12.345, 678.98765 } ;
    const int qty[100] { 22, 56 } ;

    std::size_t n_items = 2 ;
    show_items( id, name, price, qty, n_items ) ;
}
JlBorges, just so you know std::format works with VS 2019 set to /std:c++latest.

ForgottenLaw, If your compiler doesn't support C++20, and you can use a 3rd party library, Boost has a format library:

https://www.boost.org/doc/libs/1_77_0/libs/format/
Okay bro, thanks for the information you gave to me. I hope I'll do this program successfully. I'll ask in the forum next time when I'm having difficulties in my future programs.
Last edited on
Topic archived. No new replies allowed.