Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointers + structures problem.

Status
Not open for further replies.

cubics

Programmer
Jan 21, 2004
5
SI
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

struct oddaja
{
char ime[30];
char tip[30];
char datum[30];
int gledanost;
int trajanje;

oddaja * naslednji;
};

oddaja *vozlisce=NULL;


void vpis(oddaja *vozlisce, int& stodd)
{
// oddaja * vmesnik;
int n;
system(&quot;cls&quot;);
cout << &quot;Koliko oddaj zelis vpisati?: &quot;;
cin >> n;
for(int i=0; i<n ; i++)
{
oddaja * vmesnik;
vmesnik=new oddaja;
system(&quot;cls&quot;);

cout << &quot;Vpisi ime &quot; << i+1 << &quot;. oddaje: &quot; << endl;
cin >> vmesnik -> oddaje[stodd].ime;

stodd++;
vmesnik -> naslednji=vozlisce;
vozlisce=vmesnik;
}
}


void main()
{
oddaja oddaje[30];

vpis(vozlisce,stodd); break;

}
//////////////////////////////////
The program produces this error:
error C2039: 'oddaje' : is not a member of 'oddaja'
see declaration of 'oddaja'
error C2228: left of '.ime' must have class/struct/union type

Please help, what am i doing wrong?
 
> [tt]void vpis(oddaja *vozlisce, int& stodd)[/tt]
You have a parameter and a global variable with the same name. Whilst this is not your problem, it can cause all sorts of confusion in larger programs.
In this example, there is no reason why you would want a global variable.

> [tt]cin >> vmesnik -> oddaje[stodd].ime;[/tt]
oddaje is the name of the type
The nearest I can think of would be
[tt] cin >> vmesnik->ime[stodd];[/tt]

It looks to me like you're trying to create a linked list.
In which case, your vpis() function needs to return the modified list.

Something like this in main() say
Code:
void vpis(oddaja& *vozlisce, int& stodd) {
  // your code
}

int main ( ) {
  oddaja *vozlisce=NULL;  // an empty list
  int stodd;
  vpis(vozlisce,stodd);
  return 0;
}

--
 
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

struct oddaja
{
char ime[30];
char tip[30];
char datum[30];
int gledanost;
int trajanje;

oddaja * naslednji;
};


void vpis(oddaja *vozlisce, int& stodd)
{
int n;
system(&quot;cls&quot;);
cout << &quot;Koliko oddaj zelis vpisati?: &quot;;
cin >> n;
for(int i=0; i<n ; i++)
{
oddaja * vmesnik;
vmesnik=new oddaja;
system(&quot;cls&quot;);
cout << &quot;Vpisi ime &quot; << i+1 << &quot;. oddaje: &quot; << endl;
cin >> vmesnik -> ime[stodd];
stodd++;
vmesnik -> naslednji=vozlisce;
vozlisce=vmesnik;
}
}

void izpis(oddaja *vozlisce, int stodd)
{
oddaja *vmesnik=vozlisce;
system(&quot;cls&quot;);
int i=1;
while(vmesnik!=NULL)
{
cout << vmesnik -> ime << endl;
i++;
}
getch();
}

void main()
{
oddaja oddaje[30];
oddaja *vozlisce=NULL;
int stodd=1;

vpis(vozlisce,stodd);
izpis(vozlisce,stodd);

}
///////////////////////////////////////////
Ok, that got me a little closer. I've added a new procedure to output the data. But i need it in array type so i can input/output as many as i want(and for easy access). Now when i output the data(izpis(vozlisce,stodd);) the program goes crazy.

oddaja oddaje[30]; // this creates the new array, but how do i make it a pointer
 
oops a little mistake in copy&paste
the second procedure is like this:

void izpis(oddaja *vozlisce, int stodd)
{
oddaja *vmesnik=vozlisce;
system(&quot;cls&quot;);
int i=1;
while(vmesnik!=NULL)
{
cout << vmesnik -> ime << endl;
vmesnik=vmesnik -> naslednji;
i++;
}
getch();
}
 
Have a new problem:( I want to sort the array for gledanost. Here is the whole code so there is no confusion:

/////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

struct oddaja
{
char ime[30];
char tip[30];
char datum[30];
int gledanost;
int trajanje;

oddaja * naslednji;
};

void vpis(oddaja *&vozlisce, int& stodd)
{
int n;
system(&quot;cls&quot;);
cout << &quot;Koliko oddaj zelis vpisati?: &quot;;
cin >> n;
for(int i=0; i<n ; i++)
{
oddaja * vmesnik;
vmesnik=new oddaja;
system(&quot;cls&quot;);
/* cout << &quot;Vpisi ime &quot; << i+1 << &quot;. oddaje: &quot; << endl;
cin >> vmesnik -> ime;
cout << &quot;Vpisi tip &quot; << i+1 << &quot;. oddaje: &quot; << endl;
cin >> vmesnik -> tip;
cout << &quot;Vpisi datum &quot; << i+1 << &quot;. oddaje: &quot; << endl;
cin >> vmesnik -> datum;*/
cout << &quot;Vpisi gledanost &quot; << i+1 << &quot;. oddaje: &quot; << endl;
cin >> vmesnik -> gledanost;
// cout << &quot;Vpisi trajanje &quot; << i+1 << &quot;. oddaje: &quot; << endl;
// cin >> vmesnik -> trajanje;
stodd++;
vmesnik -> naslednji=vozlisce;
vozlisce=vmesnik;
}
}


void izpis(oddaja *vozlisce, int stodd)
{
oddaja *vmesnik=vozlisce;
system(&quot;cls&quot;);
while(vmesnik!=NULL)
{
/* cout << &quot;ime: &quot; << vmesnik -> ime << endl;
cout << &quot;tip: &quot; << vmesnik -> tip << endl;
cout << &quot;datum: &quot; << vmesnik -> datum << endl;*/
cout << &quot;gledanost: &quot; << vmesnik -> gledanost << endl;
// cout << &quot;trajanje: &quot; << vmesnik -> trajanje << endl;
cout << &quot;-----------------------&quot; << endl;
vmesnik=vmesnik -> naslednji;
}
getch();
}


oddaja*najvecji(oddaja*vozlisce) //find the biggest gledanost in oddaja
{

oddaja*max=vozlisce;
vozlisce=vozlisce->naslednji;
while(vozlisce->naslednji!=NULL)
{
if(max->naslednji->gledanost > vozlisce->naslednji->gledanost)
{
max=vozlisce;
vozlisce=vozlisce->naslednji;
}

}
return max;
}


void sortgledanost(oddaja*&vozlisce, int stodd) //sort oddaja by gledanost
{
system(&quot;cls&quot;);
oddaja*trenutni =new oddaja;
oddaja*prvi=trenutni;
prvi->naslednji=vozlisce;
while(trenutni->naslednji!=NULL)
{
oddaja*max=najvecji(trenutni);
if(max->naslednji != trenutni->naslednji)
{
oddaja*cut=max->naslednji;
max->naslednji=max->naslednji->naslednji;
cut->naslednji=trenutni->naslednji;
}
max = NULL;
trenutni = trenutni->naslednji;
}
vozlisce=prvi->naslednji;

trenutni = NULL;
prvi->naslednji = NULL;
delete prvi;

izpis(vozlisce,stodd);
}


void menu()
{
system(&quot;cls&quot;);
cout << &quot;1: Vpis nove oddaje.&quot; << endl; //input new oddaja
cout << &quot;2: Izpis oddaj.&quot; << endl; //output oddaja
cout << &quot;3: Sortiranje oddaj po tipu.&quot; << endl; // not imlemented yet
cout << &quot;4: Sortiranje oddaj po stevilu gledalcev.&quot; << endl; //sort oddaja by gledanost
cout << &quot;5: Izhod.&quot; << endl; // exit the program
cout << &quot;--) &quot;;
}

void main()
{
int izbira;
int stodd = 1;
oddaja *vozlisce=NULL;

while(izbira!=5)
{
menu();

cin >> izbira;
switch(izbira)
{
case 1: vpis(vozlisce,stodd); break;
case 2: izpis(vozlisce,stodd); break;
// case 3: sorttip(oddaje,stodd); break;
case 4: sortgledanost(vozlisce,stodd); break;
case 5: exit(1);
}
}
}
///////////////////////////////////

It doesn't really sort it the way it should. Please somebody help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top