How to output message if user inputs nothing.

Hi all,

In my program I want the user to input a value 0 or greater. I know how to output an error message if user input is negative:

1
2
  if ( input < 0)
 // then cout error message 


But what if the user puts in nothing at all. I want the program to spit out a message for that also and not just terminate abruptly.

I was wondering if this would work, but i think it won't

1
2
if (input == NULL)
// then cout error message 

^^^ can NULL be used like this?

or

1
2
if (input == " ")
// then cout error message 

^^^ dont know about that one, input would be a int and not a string or char.

Thanks!
I guess if I wanted to do this, this might be an approach.

1
2
3
4
5
string str;
getline(cin, str);
if(str.empty()){
     //Code
}


Bad thing is if you wanted to do ints or doubles etc. you would have to parse the string data.

http://www.cplusplus.com/forum/articles/9645/



Last edited on
Try this:

1
2
if (!input)
   { // Your message here } 


This is the same as saying:

 
if (input == NULL)


Remember, a space is a character when input into the cin
NULL int is equal to 0. So if the user inputs 0 it will end it.
How long is your code, line wise?
Can you post it on here?

Is the user just going to press enter when prompted for an input and then it ends?
Off topic and not related to this post, but as a beginner what does null mean?
null is the ascii code 0 represented as '\0' it is used as a terminate key. They are used at the end of strings to signal where the string of characters end. They are also used in streams to signal the end of the stream. There is a macro in C called NULL that represents the null character which is often times a null ptr which is the same as a void *. In c++ for pointers there is nullptr.

http://www.cplusplus.com/reference/cstddef/NULL/?kw=NULL
http://www.cplusplus.com/reference/cstddef/nullptr_t/

http://en.wikipedia.org/wiki/Null_character
http://en.wikipedia.org/wiki/Null-terminated_string
http://en.wikipedia.org/wiki/Null_pointer#Null_pointer
http://en.wikipedia.org/wiki/Null
Thanks guys for the suggestions!

Here was my original code, I'm still working on it so its messy. I wanted to send out an error if user puts in value less than 0 or none at all. And if neither occurs then the program should print out the amount of results as the user entered.

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

#include <iostream>
#include <cstring>
#include <fstream>
#include <thread>
#include <vector>
#include <pthread.h>

using namespace std;

// vector used to hold fibonacci values. Shared by all.
vector<int>fibo_values;
// Thread
void * calculate_fibonacci (void * parameter);
// Prints Fibonacci values
void print_fibonacci ();


int main (int argc, char ** argv)
{
    pthread_t thread1;
    pthread_attr_t attr;

    // 
    if (argc < 0) {
	cerr << "proj2: arguement was" << argc << ", must be >= 0\n";
	return 0;
    }

//    if (argc 0) {
    
    //  }

    // intialize default Thread attributes
    pthread_attr_init(&attr);
    // Thread created
    pthread_create ( &thread1, &attr, &calculate_fibonacci, NULL);
    // Wait for Thread to exit
    pthread_join ( &thread1,   NULL  ); 

    print_fibonacci();


}

// Thread used to calculate fibonacci values
void * calculate_fibonacci (void * parameter)
{
    int fib = 0, a = 1, b = 0;

    for (int i=0; i < /*argc*/; i++)
	{
	    b = fib;
	    fib = a+b;

	    fibo_values.push_back (fib);

	    a=b;
	}
}

// fibonacci values are printed
void print_fibonacci ()
{
    for (int i=0; i < fibo_values.size(); i++)
    {
	cout << "Fibs [ " << i << " ] = " << fibo_values[i] << endl;
    }
}

Last edited on
Ok, so their input is going to be from command line correct?

Your usage statement would cover that all you would have to put is:

1
2
3
if (argc < 0 || argc == "") {
	cerr << "proj2: arguement was" << argc << ", must be >= 0\n";
	return 0;
thanks! I was able to figure it out.
Topic archived. No new replies allowed.