Witam, mam problem z zadaniem:
"Zmodyfikuj program tak, aby nie używał klasy array. Wykonaj dwie wersje programu:
a) Ze zwyczajną tablicą elementów typu const char * reprezentujących łańcuchy znakowe z nazwami pór roku u ze zwyczajną tablicą elementów typu double reprezentującą wydatki.
b) Ze zwyczajną tablicą elementów typu const char * reprezentujących łańcuchy znakowe z nazwami pór roku i ze strukturą, której jedyną składową jest zwyczajna tablica elementów typu double (na wydatki; ten wariant będzie bardziej przypominał pierwowzór z klasą array)."
Oryginalny kod:
#include <iostream>
#include <array>
#include <string>
using namespace std;
const int Seasons = 4;
const array < string, Seasons > Snames =
{ "Wiosna", "Lato", "Jesien", "Zima" };
void fill( array < double, Seasons > * pa );
void show( array < double, Seasons > da );
int main()
{
array < double, 4 > expenses;
fill( & expenses );
show( expenses );
return 0;
}
void fill( array < double, Seasons > * pa )
{
for( int i = 0; i < Seasons; i++ )
{
cout << "Podaj wydatki na okres >> " << Snames[ i ] << " <<: ";
cin >>( * pa )[ i ];
}
}
void show( array < double, Seasons > da )
{
double total = 0.0;
cout << "\nWYDATKI\n";
for( int i = 0; i < Seasons; i++ )
{
cout << Snames[ i ] << ": $" << da[ i ] << '\n';
total += da[ i ];
}
cout << "Lacznie wydatki roczne: " << total << " zl" << endl;
}
Mój kod na podpunkt a, działa:
#include <iostream>
void fill(const char*[], double[] );
void show(const char*[], double[] );
const char* season[] = {"Wiosna", "Lato", "Jesien", "Zima"};
int main(int argc, char **argv)
{
double cost[4];
fill(season, cost);
show(season, cost);
return 0;
}
void fill(const char* seasons[], double costs[] )
{
std::cout << "Podaj wydatki\n";
for(int i = 0; i < 4; i++)
{
std::cout << seasons[i] << ": ";
std::cin >> costs[i];
}
}
void show(const char* seasons[], double costs[] )
{
std::cout << "WYDATKI\n";
for(int i = 0; i < 4; i++)
{
std::cout << seasons[i] << ": ";
std::cout << costs[i] << std::endl;
}
}
Ale nie mam głowy o do podpunktu b:
#include <iostream>
const char *seasons[] = {"Wiosna", "Lato", "Jesien", "Zima"};
struct season_expenses
{
double expenses[4];
};
season_expenses *sea_exp;
void fill(const char*[], season_expenses);
int main(int argc, char **argv)
{
fill(seasons, sea_exp.expenses);
return 0;
}
void fill(const char *season[], season_expenses sea_exp)
{
for(int i = 0; i < 4; i++)
{
std::cout << season[i] <<": ";
std::cin >> sea_exp.expenses[i];
}
}
Wyskakuje mi błąd:
"7.8b.cxx:16:25: error: request for member ‘expenses’ in ‘sea_exp’, which is of pointer type ‘season_expenses*’ (maybe you meant to use ‘->’ ?)".
Podpowie ktoś?