String with a space using structers

i want to make the user to type a string with a space. my programm works but without a struct
so what and where is my fault ?

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
  
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

struct Wgwagner
{
	char fullname[100];
	unsigned int ZimNum;
	string Stdgg;
};

int main( )
{

	struct Wgwagner persone1;
	struct Wgwagner persone2;    // Declarationen der personen der Type-                    
	struct Wgwagner persone3;    // Wgwagner

	// Eingabe der Daten ( Persone 1 )
	//char fullname[100];   // it works, when i use  this line !!
	
         cin.getline(persone1.fullname,sizeof(fullname));
        
         cout<<fullname;

   return 0;
}
put persone1. in front of each fullname.

By the way: Better use std::string
@malmsteen

A better way to input the values in your struct, would just be create as many of them as you're going to need, at the start.

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
#include <iostream>
#include <string>

using namespace std;

struct Wgwagner
{
	string fullname;
	unsigned int ZimNum;
	string Stdgg;
};

int main()
{
	Wgwagner personel[3];// Adjust the 3, to how many you need
	
	for (int x = 0; x < 3; x++) // Adjust the 3, to same as struct number
	{
		cout << "Enter name #" << x + 1 << " : ";
		getline(cin, personel[x].fullname);
		
		cout << "Enter ZimNum " << x + 1 << " : ";
		cin >> personel[x].ZimNum;
		cin.ignore(10, '\n');
		cout << "Enter Stdgg #" << x + 1 << " : ";
		getline(cin, personel[x].Stdgg);
		cout << endl;
	}
	cout << endl << endl;
	for (int x = 0; x < 3; x++) // Adjust the 3, to how many you need to print out
	{
		cout << personel[x].fullname << "  " << personel[x].ZimNum << "  " << personel[x].Stdgg << endl;
	}
	return 0;
}
thank you very much :-)

new i have another question
if i change number 3 [ for(int x=0; x< 3); x++) ] with a variable , the programe won't work.

-------------------------------------------------------------------------------------------------------------------



#include <iostream>
#include <string>

using namespace std;

struct Wgwagner
{
string fullname;
unsigned int ZimNum;
string Stdgg;
};

int main()
{

int n; // if i use this 2.lines ,the programe won't work
cin>> n; //
Wgwagner personel[10];// Adjust the 3, to how many you need

for (int x = 0; x < n; x++) // Adjust the 3, to same as struct number
{
cout << "Enter name #" << x + 1 << " : ";
getline(cin, personel[x].fullname);

cout << "Enter ZimNum " << x + 1 << " : ";
cin >> personel[x].ZimNum;
cin.ignore(10, '\n');
cout << "Enter Stdgg #" << x + 1 << " : ";
getline(cin, personel[x].Stdgg);
cout << endl;
}
cout << endl << endl;
for (int x = 0; x < n; x++) // Adjust the 3, to how many you need to print out
{
cout << personel[x].fullname << " " << personel[x].ZimNum << " " << personel[x].Stdgg << endl;
}
return 0;
}
Last edited on
@malmsteen

Add cin.ignore(10, '\n'); after the cin >> n input to clear the input buffer. That should let it work. Did for me.
Topic archived. No new replies allowed.