Programs

What is wrong with following code? (isn't compile)


#include<string>
#include<iostream>

using namespace std;

struct osoba
{

string imie, nazwisko, plec;
int wiek;

};

osoba dodaj_osobe()
{
osoba dodaj;

cout << "Podaj imie \n";
cin >> dodaj.imie;

cout << "Podaj nazwisko \n";
cin >> dodaj.nazwisko;

cout << "Podaj wiek \n";
cin >> dodaj.wiek;

cout << "Podaj plec \n";
cin >> dodaj.plec;

return dodaj;
}

void wyswietl(osoba view)
{

cout << view.imie << " " << view.nazwisko << " " << view.plec << " " << view.wiek <<endl;

}




int main()
{
int nr_zadania;
cout << "podaj nr zadania\n";
cin >> nr_zadania;

switch(nr_zadania)
{
case 1:
{
osoba* first = new osoba();
first->imie = "Jan";
first->nazwisko = "Kowalski";
first->wiek = 20;
first->plec = "M";

osoba* second = new osoba();
second->imie="Martia";
second->nazwisko="Szczepanska";
second->wiek = 25;
second->plec="K";

osoba* third = new osoba();
third->imie="Czesio";
third->nazwisko="Mozil";
third->wiek= 30;
third->plec="M";

cout << first->imie << " " << first->nazwisko << " " << first->wiek << " " << first->plec << endl;
cout << second->imie << " " << second->nazwisko << " " << second->wiek << " " << second->plec << endl;
cout << third->imie << " " << third->nazwisko << " " << third->wiek << " " << third->plec << endl;

break;
}
case 2:
{

osoba dodaj = dodaj_osobe();
wyswietl(dodaj);

break;
}

case 3:
{
int n;
cout << " Podaj n \n";
cin >>n;

osoba *tab = new osoba[n];

for (int i = 0; i < n; i++)
{
tab[i] = dodaj_osobe();

}

for (int i = 0; i < n; i++)
{
wyswietl(tab[i]);
}

delete [] tab;
break;
}

case 4:
{



break;
}

case 5:
{


break;
}
case 6:
{


break;
}



}
system("pause");
return 0;
}
#include <cstdlib> for system.

Although it's not recommended to use that function in the first place:
http://www.cplusplus.com/articles/j3wTURfi/
Please use the code tags in the future.

I think the problem with the code is you never initialized the osaba struct's in case 1. You set the pointers for them but you never initialized osaba first, osaba second, or osaba third.

I think something like this would fix the problem:

change

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
osoba* first = new osoba();
first->imie = "Jan";
first->nazwisko = "Kowalski";
first->wiek = 20;
first->plec = "M";

osoba* second = new osoba();
second->imie="Martia";
second->nazwisko="Szczepanska";
second->wiek = 25;
second->plec="K";

osoba* third = new osoba();
third->imie="Czesio";
third->nazwisko="Mozil";
third->wiek= 30;
third->plec="M";


to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
osoba* first;
first = new osoba();
first->imie = "Jan";
first->nazwisko = "Kowalski";
first->wiek = 20;
first->plec = "M";

osoba* second;
second = new osoba();
second->imie="Martia";
second->nazwisko="Szczepanska";
second->wiek = 25;
second->plec="K";

osoba* third;
third = new osoba();
third->imie="Czesio";
third->nazwisko="Mozil";
third->wiek= 30;
third->plec="M";


I've never used structs as a return type but it looks interesting either way. Hope this fixes it for you!
Topic archived. No new replies allowed.