Monday, May 24, 2010

Why my C++ program is not working?

i am doing functions and when i build my solution i get a 0. why? this is my code:


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::setprecision;


using std::fixed;





//funtions prototypes


double getTestScore(double, double, double);


double calcAverage(double);





int main()


{


//declare variables


double firstScore = 0.0;


double secondScore = 0.0;


double thirdScore = 0.0;


double average = 0.0;


double score = 0.0;





//function calls


score = getTestScore(firstScore, secondScore, thirdScore);


average = calcAverage(score);





//display information





cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);


cout %26lt;%26lt; "Your average is: " %26lt;%26lt; average %26lt;%26lt; endl;


return 0;


}





//*****************************PROGRAM DEFINED FUCTION********************************


double getTestScore(double firstScore, double secondScore, double thirdScore)


{

Why my C++ program is not working?
After reviewing your getTestScore function this is what i am seeing...When you are returning the double totalScore, your are returning a value of 0.0





double totalScore = 0.0;


input a


input b


input c


return totalScore;





I do not see you using the actual input that your getting. So before you return the value, make sure you set totalScore before passing it back like so:





double totalScore = 0.0;


input a


input b


input c





totalScore = input a + input b + input c;


return totalScore;
Reply:In the function getTestScore, the totalScore variable is assigned a value of 0. It is not modified in any way in the function, you need to add the three inputs together and assign that value to total score inside the function before you return totalScore. As in:





totalScore = (Score1 + Score2 + Score3...etc);





Then your function will return a value other than 0.





oh, and for the top, you can just use using namespace std; instead of all the std:: stuff.
Reply:The 3 parameters to getTestScore are passed by value, not by reference. They don't update back in main() when you do an assignment in the subroutine.





Try passing them by reference, or restructuring in some other way.
Reply:Your function getTestScore always returns 0.0. You initialize totalScore to 0.0, never change it, and return totalScore.





Also, the arguments firstScore, secondScore, and thirdScore are serving no purpose, you don't need them. I would say the function should be called getTotalScore. The only thing you're missing is to set totalScore to the sum of the entered scores.

marguerite

1 comment:

  1. #include
    class House;
    class Room
    {
    public: Room() { };
    static void createRoom_v(Room* (&room), House* hse, char* name)
    {
    room = new Room(hse, name);
    }
    Room(House* hse, char* myName)
    {
    cout<<"PakistaniCitizens\n";
    myHse_p = hse;
    if(NULL != myHse_p)
    {
    name_p = new char(sizeof(strlen(myName)));
    name_p = myName;3
    }
    else
    {
    cout<<"Please enter the age of the citizen\n";
    }
    }

    ~Room()
    {
    cout<<"PakistaniCitizens\n";
    myHse_p = NULL;
    delete (name_p);
    }
    void disp()
    {
    cout<< name_p;
    cout<<"\n";
    }

    static void initList_v(Room *(& roomsList_p)[3])
    {
    roomsList_p[3] = new Room[3];
    }
    private: House * myHse_p;
    char * name_p;

    }

    class House
    {
    public: House(char *myName)
    {
    cout<<"PakistaniCitizens\n";
    name_p = new char(sizeof(strlen(myName)));;
    name_p = myName;
    Room::initList_v(roomsList_p);
    Room* myRoom;
    Room::createRoom_v(myRoom, this, "name");
    roomsList_p[0] = myRoom;
    Room::createRoom_v(myRoom, this, "age");
    roomsList_p[1] = myRoom;
    Room::createRoom_v(myRoom, this, "CNIC No");
    roomsList_p[2] = myRoom;
    }
    ~House()
    {
    cout<<"PakistaniCitizens\n";
    unsigned int i;
    cout<<"Eligible for vote cast...\n";
    for(i=0; i<3; ++i)
    {
    if(roomsList_p[i] != NULL)
    { delete (roomsList_p[i]);
    }
    }
    delete [] roomsList_p;
    delete (name_p);
    }
    void disp()
    {
    cout<<"\n\nPakistaniCitizens :"name_p;
    if(roomsList_p != NULL)
    {
    unsigned int i;
    cout<<"\n\nVotersList...\n";
    for(i=0; i<3; ++i)
    {
    if(NULL != roomsList_p[i])
    {
    roomsList_p[i]->disp();
    }
    }
    cout<<"\n\n";
    }
    }
    private:
    char* name_p;
    Room* roomsList_p[3];
    }
    int main()
    {
    cout<<"\nExample of Composition Relationship\n";
    cout<<"-----------------------------------------\n\n";
    House hse("data members");
    cout<<"\n\nPakistaniCitizens...\n";
    hse.disp();
    cout<<"Age is less than 18...\n";
    return(0);

    }

    ReplyDelete