J
John Harrison
Protoman said:How do I access the induvidual elements of a vector if I have no idea
how many there are?
You can use the size() method to find out how big a vector is.
john
Protoman said:How do I access the induvidual elements of a vector if I have no idea
how many there are?
Protoman said:OK here's the input function that doesn't abort on bad data:
template<class T>
void input(vector<T>& v)
{
start:
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
if(num==0)
{
cout << "Try again, with a number this time ";
goto start;
}
else if(!cin)
{
cout << "Try again, with a number this time ";
goto start;
}
else
for(int i=0;i<num;++i)
{
start2:
cout << "Enter a number: ";
T temp;
cin >> temp;
if(!cin)
{
cout << "Try again, with a number this time ";
goto start2;
}
else
v.push_back(temp);
}
}
But it keeps displaying the same message over and over again; what's
wrong? Thanks for the help!!! BTW, I use Learn C++ in 21 Days and
Standard C++ Bible. And my teacher told me there's a book that tells
you everything you can and can't do in C++. Could you tell me the name
of that book please? Thanks!!!
John Harrison said:It works, but the idiots aren't going to be very happy when the program
aborts. I would have suggested some sort of error recovery, 'Hey idiot!
Please enter the number again', that kind of thing.
John Harrison said:There is nothing that cannot be done in C++, it is a general purpose
language. Of course there are somethings that are easy to do and some
that are difficult, compared to other languages.
Protoman said:OK, here we go, inputting's a seperate function now:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;
void input(vector<long double>& v)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
long double temp;
cin >> temp;
v.push_back(temp);
}
}
long double mean(vector<long double>& v)
{
input(v);
. And how to I access
induvidual items from a vector when I have no idea how many there are?
I've used those books, and my teacher, whose is (really; I'm not lying)
a top programmer at Bell Labs, says those two are great books for a
beginner; I've got others. And thanks for the tip. And how to I access
induvidual items from a vector when I have no idea how many there are?
You are thinking of 'exit()', BUT, don't use that for normal program
termination. It's intended as an 'emergency exit' (it's possible to shoot
yourself in the foot - or worse!).
I don't see any other orderly way to exit in a case like that.
As I understand it, exit() does basic clean-up (destructs
objects, closes files, etc.), unlike abort() and terminate(),
which die much more messily.
Alf P. Steinbach said:* Robbie Hatley:
Orderly -- with stack unwinding -- throw an exception of a type only
caught by a catch in 'main'.
§3.6.1/4, the standard guarantees that you do not get stack unwinding.
And out of curiousity, what if I'm a bad boy and exit() with
100MB of stuff on the stack? Does the OS (eg, Win2k) reclaim
that? Or is that 100MB off-line until I shut down or push "reset"?
Or is this OS dependent?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.