Count and Display a value>0 but <1

Question is in the title

1
2
3
4
5
6
for(int index = 0; index < a.capacity( ); index++)
            if(a[index] > 0)
            {
                indexCount++;
                cout << index << " => " << a[index] << endl;
            }


pretty regular for loop with an array that can store double. My problem is that it won't count and print values that are less than 1
Last edited on
You know, runnable test cases work much better than minimal out of context code snippets.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ cat foo.cpp
#include <iostream>
#include <vector>
using namespace std;

int main ( ) {
  vector<double> a = { 0, 0.5, 1.0, 1.5 };
  int indexCount = 0;
  for(int index = 0; index < a.capacity( ); index++)
    if(a[index] > 0)
    {
      indexCount++;
      cout << index << " => " << a[index] << endl;
    }
  cout << "count=" << indexCount << endl;
}
$ g++ -std=c++11 foo.cpp
$ ./a.out 
1 => 0.5
2 => 1
3 => 1.5
count=3


Especially when your minimal code snippet placed into a minimal runnable example completely disproves your assertion.

What it means is that whatever it is you're seeing, it isn't in the code you posted.
Sorry. You're right. I'm honestly unsure what I should include so I'm just gonna post everything...

header file
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
#ifndef DYNAMICARRAYCLASS_H_INCLUDED
#define DYNAMICARRAYCLASS_H_INCLUDED


template <typename T>
class DynamicArray
{
    T* values; // T Dataype CAP capacity
    int cap;
    T dummy = T( );

public:
    DynamicArray(int = 2); //constructor // default = 2
    int capacity( ) const;
    void capacity(int); //setter
    T operator[ ] (int) const; //getter
    T& operator[ ] (int) ; //setter
};

////////Member Functions/////

template <typename T> // Dynamic Array Constructor
DynamicArray<T>::DynamicArray(int cap)
{
	this->cap = cap;
	values = new T[cap];
	for(int i = 0; i < cap; i++)
		values[i] = T( );
}


template <typename T>
DynamicArray<T>::capacity( ) const
{
    return cap;
}

template <typename T>
void DynamicArray<T>::capacity(int cap)
{
    T* temp = new T[cap];                                  //allocate a new array with new cap
    int limit = (cap < this->cap ? cap : this->cap);       // get the lesser of the new and old capacities
    for(int i = 0; i < limit; i++)                           // copy the contents
        temp[i] = values[i];

    for(int i = limit; i < cap; i++)                         //set the added values to their defaults
        temp[i] = T();

    delete [ ] values;                                     // de allocate original array
    values = temp;                                         // switch newly allocated array into the object
    this->cap = cap;                                       // uppdate the cap
}

template <typename T> // SETTER Function
T& DynamicArray<T> :: operator [](int index)
{
    if (index < 0)
        return dummy;
    if (index >= cap)
        capacity(index * 2);
    return values[index];
}

template <typename T> // GETTER Function
T DynamicArray<T>::operator[](int index) const
{
    if(index < 0 || index >= cap)
        return T( );
    return values[index];
}



#endif // DYNAMICARRAYCLASS_H_INCLUDED 


cpp file

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
#include <iostream>
#include <cstdlib>
#include <string>
#include "DynamicArray.h"

using namespace std;

int main()
{
    DynamicArray<double> a(100);
    DynamicArray<double> b(100);
    string str1 = "";
    string str2 = "";
    double indexCount = 0;

    while(true)
    {
        cout << "Enter an index and a value. [q to quit]: ";
        cin >> str1;
        if(str1 == "Q" || str1 == "q")
            break;
        cin >> str2;
        int index = atoi(str1.c_str());
        int value = atoi(str2.c_str());
        a[index] = value;
        b[value] = index;
    }

        for(int index = 0; index < a.capacity( ); index++)
            if(a[index] > 0)
            {
                indexCount++;
                cout << index << " => " << a[index] << endl;
            }


        cout << "You stored this many values: " << indexCount << endl;



    return 0;
}



I only posted such a small snippet because I thought there was going to be a simple solution that I just couldn't see
Last edited on
if ( a[index] > 0.0 && a[index] < 1.0 ) in the test.

Not sure if that's what you wanted.
Last edited on
Ok I see what I was doing wrong. Atoi converts string to int not double. So that was the problem. Changed it to atof and it works fine
Topic archived. No new replies allowed.