Structures and for loop

Hello everyone, I am new member here.
I made this:

struct structure
{
int number;
string name;
};


int main()
{
structure a1,a2,a3,a4,a5
cin>>a1.number;
cin>>a1.name;
cin>>a2.number;
cin>>a2.name
.
.
.
}

So my question is , how do I make for loop to automatize input of data in different structures, so I dont have to write cin>>a1 cin >>a2..... large amount of times?

Thanks in advance!
Last edited on
The same way as you'd do it for any other type of variable: create an array vector of them, and use a loop, incrementing the array vector index each time.
Hmm, do you have any example where I can see how to declare that properly...
Like i know how to make for loop, but how do i make etc. 5 times repeatedly inputs for 5 different structures with for loop
Last edited on
1) Make an array of structures.

2) Create a for loop that iterates 5 times, incrementing an index each time

3) On each iteration, use the index to access the element of your array that you want to set.

Presumably you could do this if it was just integer variables you were modifying, yes?

If you've learned how to use vectors, use a vector instead of an array.

If you haven't learned how to use vectors:
- learn how to use vectors
- use a vector instead of an array :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct Thing
{
   int number;
   string name;

   void input();
};


void Thing::input()
{
   cout << "Enter a number: ";   cin >> number;   cin.ignore( 1000, '\n' );
   cout << "Enter a name: ";     getline( cin, name );
}


int main()
{
   int N;
   cout << "How many things do you want? ";   cin >> N;
   vector<Thing> things( N );

   for ( int i = 0; i < N; i++ ) things[i].input();

   cout << "Your entries are:\n";
   for ( Thing t : things ) cout << t.number << "   " << t.name << '\n';
}


How many things do you want? 3
Enter a number: 123
Enter a name: Donald Trump
Enter a number: 456
Enter a name: Vladimir Putin
Enter a number: 789
Enter a name: Uncle Tom Cobley and all
Your entries are:
123   Donald Trump
456   Vladimir Putin
789   Uncle Tom Cobley and all



And here's the version for those who insist on std:: and const-correctness and initialising all variables, etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
#include <string>

struct Thing
{
   int number{};
   std::string name;

   void input();
};


void Thing::input()
{
   std::cout << "Enter a number: ";   std::cin >> number;   std::cin.ignore( 1000, '\n' );
   std::cout << "Enter a name: ";     std::getline( std::cin, name );
}


int main()
{
   unsigned int N{};
   std::cout << "How many things do you want? ";   std::cin >> N;
   std::vector<Thing> things( N );

   for ( std::size_t i = 0; i < N; i++ ) things[i].input();

   std::cout << "Your entries are:\n";
   for ( const Thing &t : things ) std::cout << t.number << "   " << t.name << '\n';
}

Last edited on
Thanks guys, I did it <3
Edit:
When i add more int than one in "struct" I get this error:
Warning C26495 Variable 'Thing::number3' is uninitialized. Always initialize a member variable (type.6)
(i get this error for every integer, like number2,number3,number4...
@nniks19
Perhaps you do ... but we can't see it because we can't see your screen.

Please post your code ... BELOW ... IN CODE TAGS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Thing 
{
	int number1;
	string name;
	string description;
	int number2;

	void input();
};
void Thing::input()
{
	cout << "enter id of the variable: ";
	cin >> number1;
	cout << "enter name of the variable: ";
	getline(cin, name);
	cin.ignore(1000, '\n');
	cout << "enter description of the variable: ";
	getline(cin, description);
	cout << "enter price of the variable: ";
	cin >> number2;
}

int main()
{
	int N;
	int i;
	N = 2;
	vector<Thing> things(N);
	for (i = 0; i <= N; i++)
	{
		things[i].input();
	}
	things.clear();
	return 0;
}
Last edited on
for (i = 0; i <= N; i++)
That will loop N+1 times ... you have only allowed for N. Change <= to <.


cin.ignore(1000, '\n');
This should go after cin >> lines, NOT after getline() input. Its purpose is to clear the newline character from the input buffer, which cin>> alone would not do.
Thank you... problem was <=
Topic archived. No new replies allowed.