Anyone knows how to solve this | Sololearn: Learn to code for FREE!


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

1/15/2021 5:01:01 AM

Decoder 👩‍💻

3 Answers

New Answer


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;


Your loop is running 3 times only (for "i" = 0 ,1 and 2) What is the problem you are facing here ?


Btw, if i may ask do u know how to stop a file from over writing?