Reputation: 41
I'm trying to write a program with which you can control the data in that array.
But for some reason every time I'm trying to use arr.size();
the program just crashes.
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
int main() {
while (true) {
int choice;
cout << "Make new Array with custom size...(1)" << endl;
cout << "Add amount at the end.............(2)" << endl;
cout << "Delete amount at the end..........(3)" << endl;
cout << "Add amount at location i..........(4)" << endl;
cout << "Delete amount at location i.......(5)" << endl;
cout << "Delete Array......................(6)" << endl;
cout << "Show Array........................(7)" << endl;
cout << "END...............................(0)" << endl;
cout << "Choice: ";
cin >> choice;
vector<double> arr;
switch (choice) {
case 1: {
int i;
cout << "Enter array size: ";
cin >> i;
arr.resize(i);
cout << "Set array size to " << i << "!" << endl;
cout << "Success!" << endl;
system("pause");
system("cls");
break;
}
case 2: {
double amount;
cout << "Enter amount: ";
cin >> amount;
int location = arr.size();
cout << location << " " << amount << endl;
arr[location] = amount;
cout << arr[location] << endl;
cout << "Successfully saved!" << endl;
system("pause");
system("cls");
break;
}
case 3:
arr[arr.size()] = 0;
cout << "Success: " << arr[arr.size] << endl;
system("pause");
system("cls");
break;
case 4: {
int ite;
float numb;
cout << "Please enter amount: ";
cin >> numb;
cout << "Please enter amount for the i'th location: ";
cin >> ite;
cout << "Success!" << endl;
system("pause");
system("cls");
break;
}
case 5:
int ites;
cout << "Please enter amount for the i'th location: ";
cin >> ites;
arr[ites] = 0;
cout << "Success!" << endl;
system("pause");
system("cls");
break;
case 6:
int o;
for (o = 0; o < arr.size(); o++) {
arr[o] = 0;
}
cout << "Success!" << endl;
system("pause");
system("cls");
break;
case 7:
int j;
for (j = 0; j < ARRAYSIZE(arr); j++) {
cout << "Array[" << j << "] = " << arr[j] << endl;
}
system("pause");
system("cls");
break;
case 0: {
cout << "Exit Program....";
return 0;
}
}
}
return 0;
}
Upvotes: 1
Views: 236
Reputation: 21
On line no 83 (inside case 3)
You forgot the parenthesis
You have written
cout << "Success: " << arr[arr.size] << endl;
If you want to call the function size() which will return the size, it should be :
cout << "Success: " << arr[arr.size()] << endl;
Upvotes: 1
Reputation: 16876
Here on this line
for(j = 0; j < ARRAYSIZE(arr); j++){
What is ARRAYSIZE
? It's not defined anywhere in your code. You probably meant this instead:
for(j = 0; j < arr.size(); j++){
Also, note how size
is a function, so you need to call it by putting ()
at the end. This here won't work:
cout << "Success: " << arr[arr.size] << endl;
You need arr.size()
to make it compile, and even then, that would be out of bounds. To print the last element, do arr[arr.size()-1]
, or better yet, arr.back()
(and make sure that the array isn't empty either way, using arr.empty()
). To just print the size of the array, do:
cout << "Success: " << arr.size() << endl;
A little side note (don't worry, it didn't cause any problems): in this loop
int o;
for(o = 0; o < arr.size(); o++){
arr[o] = 0;
}
Since you're not using o
outside of the loop, you can just move the declaration into the loop initialization, like this:
for(int o = 0; o < arr.size(); o++){
arr[o] = 0;
}
And if you're getting a warning about signed/unsigned mismatch here, you can get rid of it by changing int
to unsigned int
.
Upvotes: 2