Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[C++] Program działa przy zwykłej tablicy i sypie się po odwolaniu do struktury

Ostatnio zmodyfikowano 2013-09-29 16:49
Autor Wiadomość
zamir4
Temat założony przez niniejszego użytkownika
[C++] Program działa przy zwykłej tablicy i sypie się po odwolaniu do struktury
» 2013-09-29 15:25:20
Witam.
Na wstepie przepraszam ze znów zwracam sie do was z prośbą, ale siedzę już kolejną bitą godzinę i nie umiem sobie samemu poradzić...
Bardzo proszę o pomoc, jest to moje jutrzejsze zadanie na informatykę. 
Otóż program dobrze działa przy użyciu zwykłej tablicy , wklejam kod:
C/C++
#include <iostream>
#include <string>
using namespace std;
int tab[ 5 ];
int n = 5;
void zamien( int & a, int & b )
{
    int temp = a; a = b; b = temp;
}

void sort( int tab[], int n )
{
    int temp;
    for( int j = n - 1; j > 0; j-- )
    for( int i = 0; i < j; i++ )
    if( tab[ i ] > tab[ i + 1 ] )
         zamien( tab[ i ], tab[ i + 1 ] );
   
}

int suma, wymysl;
string klops;
struct studia
{
    int nazwa_uczelni;
    string kierunek_uczelni;
   
   
} uczelnia;
struct student
{
    string imie;
    string nazwisko;
    int rok;
    int wiek;
    int stypN;
    int tab;
    studia odw;
} cos;
int main()
{
    student cos[ 5 ];
    for( int i = 0; i < 5; i++ )
    {
       
       
       
        cout << " Podaj ile wynosi stypendium socjalne " << endl;
        cin >> tab[ i ];
        cout << " ______________________________" << endl;
    }
    sort( tab, n );
   
   
    cout << "to twoja suma" << tab[ 0 ] + tab[ 1 ] + tab[ 2 ];
   
}
Jednakże gdy zamieniam tab  na odwołanie się do struktury tj. cos[].stypS to program nie działa. Wywala mnóstwo błędów.
tak wyglada kod po zamianie wszystkiego :
C/C++
##include <iostream>
#include <string>
using namespace std;

int tab[ 5 ];
int n = 5;
void zamien( int & a, int & b )
{
    int temp = a; a = b; b = temp;
}

void sort( int cos[].stypS, int n )
{
    int temp;
    for( int j = n - 1; j > 0; j-- )
    for( int i = 0; i < j; i++ )
    if( cos[ i ].stypS > cos[ i + 1 ].stypS )
         zamien( cos[ i ].stypS, cos[ i + 1 ].stypS );
   
}

int suma, wymysl;
string klops;
struct studia
{
    string nazwa_uczelni;
    string kierunek_uczelni;
   
   
} uczelnia;
struct student
{
    string imie;
    string nazwisko;
    int rok;
    int wiek;
    int stypN;
    int stypS;
    int tab;
    studia odw;
} cos;
int main()
{
    student cos[ 5 ];
    for( int i = 0; i < 5; i++ )
    {
       
       
       
        cout << " Podaj ile wynosi stypendium socjalne " << endl;
        cin >> cos[ i ].stypS;
        cout << " ______________________________" << endl;
    }
    sort( cos.stypS, n );
   
   
    cout << "to twoja suma" << cos[ 0 ].stypS + cos[ 1 ].stypS + cos[ 2 ].stypS;
   
}
To są wywalające błędy :
12 20 C:\Users\Karol\Desktop\a\f.cpp [Error] expected ',' or '...' before '.' token
C:\Users\Karol\Desktop\a\f.cpp In function 'void sort(int*)':
17 18 C:\Users\Karol\Desktop\a\f.cpp [Error] request for member 'stypS' in '*(cos + ((sizetype)(((long long unsigned int)i) * 4ull)))', which is of non-class type 'int'
17 33 C:\Users\Karol\Desktop\a\f.cpp [Error] request for member 'stypS' in '*(cos + ((((sizetype)((long long unsigned int)i)) + 1u) * 4u))', which is of non-class type 'int'
18 21 C:\Users\Karol\Desktop\a\f.cpp [Error] request for member 'stypS' in '*(cos + ((sizetype)(((long long unsigned int)i) * 4ull)))', which is of non-class type 'int'
18 37 C:\Users\Karol\Desktop\a\f.cpp [Error] request for member 'stypS' in '*(cos + ((((sizetype)((long long unsigned int)i)) + 1u) * 4u))', which is of non-class type 'int'
C:\Users\Karol\Desktop\a\f.cpp In function 'int main()':
53 14 C:\Users\Karol\Desktop\a\f.cpp [Error] request for member 'stypS' in 'cos', which is of non-class type 'student [5]'
Jeszcze raz bardzo proszę o pomoc.
Pozdrawiam
P-92864
pekfos
» 2013-09-29 15:34:45
C/C++
void sort( int cos[].stypS, int n )
Co to?
P-92866
zamir4
Temat założony przez niniejszego użytkownika
» 2013-09-29 15:50:38
Chyba, jest to odwołanie się do struktury student i zrobienie z niej tabeli:) Jednakże skoro się pytasz, to chyba coś jest nie tak:/. Może jeszcze jakaś delikatna podpowiedź?
P-92867
pekfos
» 2013-09-29 16:18:05
P-92869
zamir4
Temat założony przez niniejszego użytkownika
» 2013-09-29 16:22:53
Dziękuję za odpowiedź. Oto poprawy, wywala ostatni błąd.
56 11 C:\Users\Karol\Desktop\a\f.cpp [Error] expected primary-expression before 'int'
A oto program:
C/C++
#include <iostream>
#include <string>
#include <conio.h>
#include <cstdlib>
using namespace std;

int tab[ 5 ];
int n = 5;
void zamien( int & a, int & b )
{
    int temp = a; a = b; b = temp;
}

void sort( int stypS[], int n )
{
    int temp;
    for( int j = n - 1; j > 0; j-- )
    for( int i = 0; i < j; i++ )
    if( stypS[ i ] > stypS[ i + 1 ] )
         zamien( stypS[ i ], stypS[ i + 1 ] );
   
}

int suma, wymysl;
string klops;
struct studia
{
    string nazwa_uczelni;
    string kierunek_uczelni;
   
   
} uczelnia;
struct student
{
    string imie;
    string nazwisko;
    int rok;
    int wiek;
    int stypN;
    int stypS;
    int tab;
    studia odw;
} cos;
int main()
{
    student cos[ 5 ];
    for( int i = 0; i < 5; i++ )
    {
       
       
       
        cout << " Podaj ile wynosi stypendium socjalne " << endl;
        cin >> cos[ i ].stypS;
        cout << " ______________________________" << endl;
    }
    sort( int stypS, n );
   
   
    cout << "to twoja suma" << cos[ 0 ].stypS + cos[ 1 ].stypS + cos[ 2 ].stypS;
    system( "PAUSE" );
    return 0;
}
P-92870
pekfos
» 2013-09-29 16:29:39
C/C++
sort( int stypS, n );
Co to ma robić?
P-92871
Monika90
» 2013-09-29 16:29:40
void sort( int stypS[], int n )
tak się przkazuje tablicę intów, a ty chyba chcesz mieć tablicę studentów

sort( int stypS, n );
tak się nie wywołuje funkcji
P-92872
zamir4
Temat założony przez niniejszego użytkownika
» 2013-09-29 16:35:20
Po zmianie na :   sort(cos.stypS, n);
Wywala błąd : 54 14 C:\Users\Karol\Desktop\a\f.cpp [Error] request for member 'stypS' in 'cos', which is of non-class type 'student [5]'

Wydaje mi się że chcę mieć tablicę wpisanych stypendiów, to co jest struct odnosi się również do innych zadań, nie wkleilem tutaj całego progrmau ponieważ jest bardzo długi i nie przejrzysty.

Dziękuję za pomoc:)
P-92873
« 1 » 2
  Strona 1 z 2 Następna strona