int main() {
int numrec;
int* records;
cout << "How many records would you like to enter?" << endl;
cin >> numrec;
records = newint[numrec];
for(int i=0;i<numrec;i++)
{
cout << "Enter ID" << i + 1 << ":";
cin >> records[i];
}
cout << "The ID's I got were as follows:" << endl;
for(int i=0;i<numrec;i++)
{
cout << records[i] << endl;
}
system("pause");
}
But what I want to do is I can keep typing records until one of the records I type 0 and then it will post all the records that I have typed before hand. I've tried using strings but it doesn't seem to work. Any help will be appreciated!
First of all, please place your code within [code] tags, which can easily be done by using the <> button under the formatting options.
I'm a little confused on what you're trying to do though. Your code implies that you want the user to dictate how many user IDs are entered, but then in your post you say that you want to be able to enter IDs in until you type '0'. Which is it? Your code will be different depending on what you want to do.
Dynamic arrays won't help you much, I suggest you use vectors. What I've gathered is you use a #include <vector> , and declare a vector as follows: vector</*Int or something*/>myVector(/*Insert some integer >0, I haven't found it matters what*/), and then just use myVector[i] to access the i-th member.
Kazekan, I think (s)he's figured out how to do it with a fixed number of ID's, but now (s)he wants to know how to do so with a dynamic size.
And why are you using system("pause")? I've heard bad things about it. cin.get() and cin.ignore() are both alternatives.
Well its just a snipit of the program i wrote in two minutes to try and demonstrate what I wanted to show instead of being a comformed person asking for help and not showing exactly what they want to do.
The thing I need help with is that, at the moment it asks for numrec, which then adds the value to the array so it can create the amount of "records".
Example : I enter 5 on numrec
so it will ask for 5 records.
and then output the records to me.
but what I want to be able to do, is to keep entering records and records until I enter a invalid input which could be an IF statement that says if ID < 1 then break the loop/ break the dynamic array and then it will output all the records i enter previously and then end.
I'm having trouble doing this and at the moment i'm using a DO/WHILE statement.
while (true)
{
cout << "give me a number" << endl;
cin >> thatNumberIAskedFor;
if (thatNumberIAskedFor == 0)
{
break;
}
// do some stuff with the num
}
...
i wish I could use that one but unfortunately, that will only store one number. I need to store multiple values in just one variable. So I need an array
so for instance I need x to be
3
5
7
8
all in one time. and referring it to using the arrays i.e. x[1], x[2] so on so on.
But I kept getting errors and the program wouldn't function properly. The STL Vector does seem useful but it just keeps bugging up for me either im programming wrong or im programming wrong :P haha lol
More importantly, you need to learn to look up problems on your own. Most errors can simply be solved with a google search and ~5 minutes of reading tutorial pages (like the one for the STL vector class).
viliml, that is not a safe use of placement new, nor is that really a concept for this level of question. The memory you are 'allocating' with records=new (records) int[numrec+1]; may already be in use by another object. Example:
int main()
{
int numrec=0;
int* records=newint[numrec+1];
int * randomArray = newint[2];
randomArray[0] = 101;
randomArray[1] = 101;
cout << randomArray << endl << records << endl;
do
{
cout << "Enter ID" << ++numrec << ":";
cin >> records[numrec-1];
records=new (records) int[numrec+1];
} while(records[numrec-1]!=0);
cout << "The ID's I got were as follows:" << endl;
for(int i=0; i<numrec-1; i++)
{
cout << records[i] << endl;
}
cout << "randomArray[0]: " << randomArray[0] << endl;
cout << "randomArray[1]: " << randomArray[1] << endl;
cout << randomArray << endl << records << endl;
return 0;
}
00345D60
00345D20
Enter ID1:1
Enter ID2:2
Enter ID3:3
Enter ID4:4
Enter ID5:5
Enter ID6:6
Enter ID7:7
Enter ID8:8
Enter ID9:9
Enter ID10:1
Enter ID11:2
Enter ID12:3
Enter ID13:4
Enter ID14:5
Enter ID15:6
Enter ID16:7
Enter ID17:8
Enter ID18:9
Enter ID19:0
The ID's I got were as follows:
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
randomArray[0]: 8
randomArray[1]: 9
00345D60
00345D20