How to tell when no data has been entered for an array?

I have to do a C++ project where I create an array that can take a maximum of 140 characters from the user. However, I also have a menu of multiple things I am able to do with the matrix. In that menu, if the user has yet to enter any text into the array, the user is to get an error message notifying them that they must enter text using option 1 first. I will put the code I have so far below, but I am not sure how to go about setting up a way to determine if any information has been entered for the array yet? I am thinking it has something to do with pre-defining the values in the array and seeing if they are still those values, but arrays are new to me and I'm at a loss on where to start here.

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
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<fstream>

using namespace std;

void MainMenu ();

string options = "-1";
char text[140];

int main()
{
    MainMenu();
    while (options!="0"){ // keeps loop running until user enters 0
        if ((options=="2" || options=="3" || options=="4") && NEED RESTRICTION FOR NO DATA HAVING BEEN ENTERED INTO ARRAY "TEXT" HERE){  // need text first error
            cout << "Please enter text using option 1 first.\n\n";
            MainMenu();
        }
        if (options != "0" && options != "1" && options != "2" && options != "3" && options != "4"){  // invalid options error
            cout << "Wrong choice.\n\n";
            MainMenu();
        }
        if (options=="1"){ // option 1 - enter text
            cout << "Please enter text (up to 140 characters): ";
            cin.ignore();
            cin.getline(text,140);
            MainMenu();
        }

    }
}

void MainMenu (){
    cout << "Main Menu\n" << "1. Enter text.\n" << "2. Search for text.\n" << "3. Append text.\n";
    cout << "4. Analysis of text.\n" << "Press 0 to quit.\n" << "Please make a choice: \n";
    cin >> options;
    cout << endl << endl;
}
Last edited on
1. The getline stores only 139 characters, because it needs the last for terminating null.
See http://www.cplusplus.com/reference/istream/istream/getline/

2. The terminating null. What you will have in your character array is a proper C-string. The string functions in C library operate on null-terminated C-strings.

You will thus need to do two things:
1. Make sure that the array is initialized to look like an empty C-string.
2. Test for empty C-string.


PS. Plain arrays and global variables are both rather challenging things to play with.
Last edited on
NEED RESTRICTION FOR NO DATA HAVING BEEN ENTERED INTO ARRAY "TEXT" HERE
text[0] == 0; will check whether the string is empty.

Important is (like keskiverto said) initialize text as an empty string:

text[0] = 0;
Ok, took what you guys said into consideration. The program wouldn't let me input it exactly how you suggested, but here is the closest I got:

char text[140]="0";

since it is char, it wanted the quotes. Hopefully this is still defining it as an empty set.

if ((options=="2" || options=="3" || options=="4") && text != NULL)

The bold above was the only thing it would seem to accept there. When I tried using your suggestion, it was giving me some type of error message. Alternatively, it also allowed me to use "text != "0" and both seem to be doing the same thing. So far, it seems to be doing what I want, so thanks.
since it is char, it wanted the quotes. Hopefully this is still defining it as an empty set.
No. "0" a string

text != NULL compares the pointer which is never NULL

char text[140]={0}; set the entire array to 0

When I tried using your suggestion, it was giving me some type of error message.
What error? text[0] == 0 is a correct comparison (without the ; of course)
Last edited on
some type of error message

Be more specific. The compiler tries to tell you something very important. Learn to use that feedback.


This is not what you think: char text[140] = "0";
It is equivalent to:
1
2
3
char text[140];
text[0] = '0';
text[1] = '\0';

The first character in the array becomes what looks zero when printed.
The second character in the array becomes null.
Therefore, the array does not have empty string "". It has "0".

The array is global.
If you want to initialize it in the declaration to empty string, then use char text[140] = "";
The other way is to assign null to the first array element in the main() function.
1
2
3
4
char text[140];
int main()
{
  text[0] = 0;

Topic archived. No new replies allowed.