what codes can I use?

what code can I use to replace an EMPTY array when an input or string is entered?
'everytime I will enter a string "EMPTY" should be replaced with the string entered.'
ex;

---------------------------------------
#                              STRINGS

0                               EMPTY
1                               EMPTY
2                               EMPTY
3                               EMPTY
4                               EMPTY
          totat 0 out of 5
----------------------------------------
enter a string: 


the "total 0 out of 5" is somewhat a counter for the STRINGS if how many is entered.
Last edited on
I currently have these, but I don't know how I will be displaying EMPTY 5 times or positioning it there.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
int main(){
    string productnames;
    int x[5];
    do{
    cout<< "------------------------------\n";
    cout<< "# \t  STRINGS" << endl;
        for (int cnt = 0; cnt < 5; cnt++){ //COUNTER OF 0-5 in #
        cout<< cnt << setw(15) << right << "EMPTY" << endl;
    }
    cout<< "Total #: " << " out of 5";
    cout<< "\n------------------------------\n";
    cout<<"\nEnter the strings: ";
    cin>>productnames;
    } while (1);
    return 0;
}
// EMPTY IS ONLY A TEXT. 
Last edited on
Maybe start with
1
2
3
4
5
6
7
string arr[5] = {
    "EMPTY",
    "EMPTY",
    "EMPTY",
    "EMPTY",
    "EMPTY",
};

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
#include <iostream>
#include <string>

// print a quoted string (indicate if the string is empty)
void print_string( const std::string& str )
{
    if( str.empty() ) std::cout << "\"\" (EMPTY)\n" ; // if it is an empty string
    else std::cout << '"' << str << "\"\n" ;
}

// print an array of N strings
template < std::size_t N >
void print_array( const std::string (&array) [N] ) // the array is passed by reference
{
    for( std::size_t i = 0 ; i < N ; ++i )
    {
        std::cout << i << ". " ;
        print_string( array[i] ) ;
    }
}

// accept strings from the user for an array of N strings
template < std::size_t N >
void get_array_elements( std::string (&array) [N] ) // the array is passed by reference
{
    std::cout << "\nEnter " << N << " strings\n" ;

    for( std::size_t i = 0 ; i < N ; ++i )
    {
        std::cout << '\n' << i << ". " ;
        print_string( array[i] ) ;
        std::cout << "enter new value: " ;
        std::cin >> array[i] ;
        // or std::getline( std::cin, array[i] ) ; // if the input may contain white space
    }
}


int main()
{
    constexpr std::size_t ARRAY_SZ = 5 ;
    std::string arr[ARRAY_SZ] ;
    print_array(arr) ;

    get_array_elements(arr) ;
    std::cout << '\n' ;
    print_array(arr) ;
}
@salem c, Hey can you explain further?

@JLBorges, thank you I'll try it out but it's kinda complicated for me.
Last edited on
> it's kinda complicated for me.

If you are unfamiliar with functions, here's the same code without 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
#include <iostream>
#include <string>

int main()
{
    constexpr std::size_t ARRAY_SZ = 5 ;
    std::string arr[ARRAY_SZ] ;

    // print_array
    for( std::size_t i = 0 ; i < ARRAY_SZ ; ++i )
    {
        std::cout << i << ". " ;

        // print_string: we print the string with quotes around it 
        // and also print (EMPTY) if the string is empty
        // note that the escape sequence \" represents a single " character
        // see: https://en.cppreference.com/w/cpp/language/escape
        if( arr[i].empty() ) std::cout << "\"\" (EMPTY)\n" ; // if it is an empty string
        else std::cout << '"' << arr[i] << "\"\n" ;
    }

    std::cout << "\nEnter " << ARRAY_SZ << " strings\n" ;

    // get_array_elements
    for( std::size_t i = 0 ; i < ARRAY_SZ ; ++i )
    {
        std::cout << '\n' << i << ". " ;
        if( arr[i].empty() ) std::cout << "\"\" (EMPTY)\n" ; // if it is an empty string
        else std::cout << '"' << arr[i] << "\"\n" ;
        std::cout << "enter new value: " ;
        std::cin >> arr[i] ;
        // or std::getline( std::cin, array[i] ) ; // if the input may contain white space
    }

    std::cout << '\n' ;
    // print_array
    for( std::size_t i = 0 ; i < ARRAY_SZ ; ++i )
    {
        std::cout << i << ". " ;
        if( arr[i].empty() ) std::cout << "\"\" (EMPTY)\n" ; // if it is an empty string
        else std::cout << '"' << arr[i] << "\"\n" ;
    }
}
@JLBorges, thank you for making it without functions I tried tweaking it to how I want it to display. Only line that changing is the line in # 0.

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
#include <iostream>
#include <string>
using namespace std;
int main(){
    constexpr size_t ARRAY_SZ = 5 ;
    string arr[ARRAY_SZ] ;
    // print_array
    do{
        cout<< "------------------------------\n";
        cout<< "# \t  STRINGS" << endl;
    for( size_t i = 0 ; i < ARRAY_SZ ; ++i ){
        cout << i ;
        if( arr[i].empty() ) cout << "         EMPTY\n" ;
        else cout << "         " << arr[i] << "\n" ;
    }
    // get_array_elements
    cout<< "Total #: " << " out of 5\n";
    cout<< "------------------------------\n";
    for( size_t i = 0 ; i < 1 ; ++i ){
        cout << "enter new value: " ;
        cin >> arr[i] ;
    }
        } while(1);
    return 0;
}
Last edited on
I got it fixed, thank you all.
Topic archived. No new replies allowed.