Arrays

I have a program that I need to get to read an array. I need it to say that if the number is less than 0 cout<< error message other wise just keep going. How do I get that from this code? I know it is wrong, I just don't know how. Thanks
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
#include <iostream>      // input/output declarations
#include <iomanip>       // i/o manupulator declarations

using namespace std;

void searchWArray (int []);

int main ()
{
    
    
    double pweight, length1, length2, length3, pgirth;
    int weight[15] = {1,2,3,5,7,10,13,16,20,25,30,35,40,45,50};
    double shipcharge [15] = {1.50, 2.10, 4.00, 6.75, 9.90, 14.95, 19.40, 24.20,27.30, 31.90, 38.50, 43.50, 44.80, 47.40, 55.20};
    
    
    cout << "For each transaction, enter package weight and 3 dimensions.  Enter -1 to quit.\n";
    
    do        
    {
    cout << "Enter package weight and 3 sides : \n";               
    cin  >> pweight >> length1 >> length2 >> length3;
    
         if (pweight < 0);
            cout << "Error - package weight and dimensions must be larger than 0 \n Please re-enter transaction";
         if (length1 < 0);
            cout << "Error - package weight and dimensions must be larger than 0 \n Please re-enter transaction";
         if (length2 < 0);
            cout << "Error - package weight and dimensions must be larger than 0 \n Please re-enter transaction";
         if (length3 < 0);
            cout << "Error - package weight and dimensions must be larger than 0 \n Please re-enter transaction";
              
    
    
    } while (pweight != -1);
    
system ("PAUSE");
return 0;
}   
    
    
    
    
    
    
    
    
void searchWArray (int [])
{
     int count = 0;
     int searchFor = 0;
     int weight[15] = {1,2,3,5,7,10,13,16,20,25,30,35,40,45,50};    

         for (int x = 0; x > 1; x = x + 1)
         if (weight[x] > searchFor)
            count = count + 1;
}
You had semicolons after ifs. Also logic must be changed to handle both validation and canceling. One advice: do not forget about endl for each cout, cerr. Without that you cant control when string is printed on screen.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cout << "For each transaction, enter package weight and 3 dimensions.  Enter -1 to quit." << endl;
do		
{
	cout << "Enter package weight and 3 sides :" << endl;
	cin  >> pweight >> length1 >> length2 >> length3;
	
		if (pweight > 0 && length1 > 0  && length2 > 0 && length3 > 0)
		{
			break;
		}

		if(pweight == -1 || length1 == -1 || length2 == -1 || length3 == -1 )
		{
			cout << "Canceling... " << endl;
			return 1;
		}

		cerr << "Error - package weight and dimensions must be larger than 0 \nPlease re-enter transaction" << endl;
	
} while (true);
Last edited on
Hi,i'm sorry for posting my question here but the title is gonna be basicly the same.
Do I have to declare always the exact number for arrays and vectors or I can declare them for indefinite number i.e. vector <int> vec( i ) or at least if I can declare it for only one member and add more later i.e. vector <int> vec(1) ...what I mean is that sometimes you dont know how many members you need for a vector or array and you'll get the number later probably after interface input.And is it gonna be the same for both vector and array or there's difference?Thanks for the help in advance.
The answer is, unfortunately, not quite that simple:-)

Arrays need to be declared with a constant number of members
EG
 
int myArray[5];

but you cad also have dynamic arrays via the new keyword
EG
1
2
3
4
5
int size;                                         
cin >> size;                       //user inputs number of elements required
int *myArrray = new int[size];     //allocate memory for dynamic array
...                                //do stuff!
delete [] myArray;                 //remember to free the memory 


The vector class is a type of container class - that is a class designed to contain other objects. An instance can be created in a number of ways, but the simplest is just
 
vector <int> vec;

which creates an empty vector.
you then use
 
vec.push_back()

and / or
 
vec.insert()

to add elements to the class.
The [] operator is overloaed for the vector class to allow you to access elements as if it were an array.
IE, if you have added at least 5 elements to vec, then vec[4] will return the 5th element (as with arrays, remember the index is starts at zero).

See http://www.cplusplus.com/reference/stl/vector/
for more ocumentaion on vectors
and
http://www.cplusplus.com/reference/stl/
for liks to this and the other container classes.

BTW, to add to the confusion, arrays are also refered to as vectors in some books!
Last edited on
Thanks a lot buddy,creating empty vector works perfect-I didn't know I can do that.Now I came to another question-I know that I can assign an array to a pointer and the pointer goes in a function as argument but is this the only way...can I give the array directly to the function somehow i.e.
void func(int m,int n,int arr[10])
or I always use pointers and references to get arrays and vectors to functions?
With vectors, you should use references:
void func (int m, int n, vector<int>& vec) //the '&' after a type says that the variable is a reference to a variable of that type
You would use the reference variable like if it wasn't a reference.


For arrays, arrays are basically contiguous values in memory with a single pointer pointing to the first element. However, you can use something like this to do it:
void func (int m, int n, int arr[]) //pass an array with an unknown number of elements

Even if you know the length of the array, the function can't possibly know how many elements there are. You are pretty much passing a pointer, but the function understands that it is an array instead of a simple pointer.
Last edited on
Topic archived. No new replies allowed.