|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Nov 1999
Messaggi: 123
|
Aiuto ad un principiante!
Salve a tutti! Intanto mi scuso per le violenze che ho fatto verso il povero c++ nel programma che segue, ma sono davvero alle prime armi
Codice:
#include <iostream.h>
#include <cmath>
int k=8;
class star
{ double mg;
double dist;
char str[8];
public:
star (char n[k], double m, double d );
double reall();
char getstra(){ return str[8];};
};
double star::reall()
{
return ((mg+5)-(5*log10(dist/3.26)));
}
star::star (char n[k] , double m, double d )
{
str[8]= n[k] ;
mg = m;
dist = d;
}
int main()
{
int i=0;
star cost[6]=
{
star ( "sirius" , -1.4 , 8.7),
star ( "Arturus" , -0.06 , 37),
star ( "Vega" , 0.04 ,27),
star ( "Capella" , 0.06 , 45),
star ( "Sun" , -27 , 0.000015749196),
star ( "Rigel" , -0.14 , 750),
};
for (i=0;i<6;i++)
{cout<<" Rmg "<<cost[i].getstra()<<" = "<<cost[i].reall();
}
return 0;
}
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Codice:
#include <iostream.h>
#include <cmath>
#include <stdlib.h>
#define k 8 //meglio usare un define per queste cose...in teoria come
//avevi fatto te non avrebbe nemmeno dovuto compilare
class star
{
double mg;
double dist;
char str[k];
public:
star (char n[k], double m, double d );
double reall();
char* getstra(){ return str;}; //il problema era anche qui ritornavi
//str[k] che era l'ottavo carattere della stringa
};
double star::reall()
{
return ((mg+5)-(5*log10(dist/3.26)));
}
star::star (char n[k] , double m, double d )
{
strcpy(str, n); //e qui assegnavi l'ottavo carattere all'ottavo carattere
mg = m;
dist = d;
}
int main()
{
int i=0;
star cost[6]=
{
star ( "sirius" , -1.4 , 8.7),
star ( "Arturus" , -0.06 , 37),
star ( "Vega" , 0.04 ,27),
star ( "Capella" , 0.06 , 45),
star ( "Sun" , -27 , 0.000015749196),
star ( "Rigel" , -0.14 , 750),
};
for (i=0;i<6;i++)
{
cout<<" Rmg "<<cost[i].getstra()<<" = "<<cost[i].reall() << endl;
}
system("PAUSE");
return 0;
}
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Nov 1999
Messaggi: 123
|
grazie mille!
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 09:16.



















