Question:
Question in C++ how can I input infinitely many number without having to cite them in the begining?
D
2012-11-15 10:10:05 UTC
Hello I'm new in C++. I'm creating a program to calculate determinants of matrices but I'm having a problem:
when I want to declare a variable so I can give it a value later I start with int a; for an example, but what should I do if I have so many variables it's impossible to write int for every single one of them! thank you.
Six answers:
?
2012-11-15 10:51:15 UTC
If you have a large but fixed number of entries then you can use an array as detailed by others.



If you want to be able to cope with a completely arbitrary number of entries limited only by the available memory on the computer then you need to use some form of dynamic memory allocation.

Fortunately c++ makes this fairly easy using the standard container library, for what you want a vector is probably the best option.





#include

using std::vector



main {



// define myIntVector as being a vector of int's, it could be floats, doubles, pointers,

// of a class you have defined or even a vector of vectors if you wanted.



vector myIntVector;





// reserve space for 100 entries. Never needed, it will add space as required but it's

// faster to allocate the space in large blocks if you know you're going to need it.



myIntVector.resize(100);





// add an arbitary number of values

while (more values to add) {

myIntVector.pushBack(newValue);

}



int numberOfEntries = myIntVector.size();

int valueAtLocationSix = myIntVector.at(6); // myIntVector[6] would also work.



myIntVector.clear(); // delete them all

}





See http://www.cplusplus.com/reference/stl/vector/ for more things you can do.
gail
2016-08-03 07:55:28 UTC
Genuinely Charles, of the Allies the Soviet Union misplaced essentially the most lives. I believe that this kind of debate is pointless. Countries strengthen opinions about their struggle efforts. Mostly these opinions are open to question in other nations. The united states entry into the war certianly helped the allies battle efforts and were a welcome addition. I havn't heard too many brag about Vietnam. With the aid of all accounts it was once a humiliating defeat. That's not to remove from the significant efforts and sacrifice made with the aid of the infantrymen who went there. The reception these humans recieved upon their return home was appalling. The U.S. & UK are both unbiased countries. They take moves in their own satisfactory pursuits. At the time of the struggle there was once so much debate in the us about whether the us must or will have to not be concerned. The bombing of Pearl Harbour made it clear to the USA executive what it needed to do.
green meklar
2012-11-16 13:29:30 UTC
That is what arrays and dynamic memory are for.
madmac79
2012-11-15 10:26:51 UTC
use arrays

int a[10];/* array size of 10 or11 variables it starts from 0 i,e a[0]*/
No Name
2012-11-15 10:17:30 UTC
That's a question for stack overflow
Korey Badgley
2012-11-15 10:16:53 UTC
I would suggest to create an array of int then reference each one later. However if you need to add them on as you go a vector would be better rather than having a set number of indexes via an array. This would fix your problem of having a million and a half values. The only other thing is, if you know how many variables you are going to use, then use an array with a fixed amount. If you need more help, feel free to add me and message me as well and I will help as I can.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...