Anyone knows how to solve this

#include <iostream> #include<string> using namespace std; struct student { string name; int id; }; const int ENTRIES = 3; int main () { student alpha[ENTRIES] ; for(int i =0; i<ENTRIES; i++) { cout <<"Enter name and id\n"; cin >>alpha->name; cin>>alpha->id; } return 0; } My for loop isn't working because of the last statement

15th Jan 2021, 5:01 AM
Decoder 👩‍💻
3 Answers
+ 7
Decoder 👩‍💻 You need to provide details on what you expect to happen with your code. We have no idea what the mistake could be when there is not any compile error. But... Part of your problem likely has to do with the fact that you are declaring an array but you are not assigning the values in your for loop to a unique index of that array. Therefore each value gets assigned to index 0 and overwrites all previous values. student alpha[ENTRIES] ; Assignment in for loop should be cin >> alpha[i].name;
15th Jan 2021, 5:58 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
+ 4
Your loop is running 3 times only (for "i" = 0 ,1 and 2) What is the problem you are facing here ?
15th Jan 2021, 5:50 AM
Arsenic - avatar
Btw, if i may ask do u know how to stop a file from over writing?
15th Jan 2021, 6:49 AM
Decoder 👩‍💻