Oct 30, 2013 at 2:20am Oct 30, 2013 at 2:20am UTC
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 Oct 30, 2013 at 2:21am Oct 30, 2013 at 2:21am UTC
Oct 30, 2013 at 2:41am Oct 30, 2013 at 2:41am UTC
NULL int is equal to 0. So if the user inputs 0 it will end it.
Oct 30, 2013 at 2:49am Oct 30, 2013 at 2:49am UTC
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?
Oct 30, 2013 at 4:25am Oct 30, 2013 at 4:25am UTC
Off topic and not related to this post, but as a beginner what does null mean?
Oct 30, 2013 at 4:12pm Oct 30, 2013 at 4:12pm UTC
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 Oct 30, 2013 at 4:15pm Oct 30, 2013 at 4:15pm UTC
Nov 2, 2013 at 6:40am Nov 2, 2013 at 6:40am UTC
thanks! I was able to figure it out.