Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Class Help

Name: Anonymous 2009-03-30 22:42

Halp /prog/
I'm supposed to make a program that asks how many integers to sums up, then using a for loop, allows the user to input the integers. I posted earlier about it, but now I'm trying to use a class, and failing horribly. This is my first programming language class, so if you could provide as many tips as possible that would be appreciated (other than the obvious 'GTFO' or 'change your major lol randumb xD')


//////////////////////////////////////////////////////////////////
//    Lab 5 - For loop Sum
//    Header.h
//////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

class Sum
{
    public:
        Sum();    //constuctor
        void calcSum(int);    //adds to the total
        int setSum();    //displays total
    private:
        int m_total;
}

Sum::Sum ()
{
    calcSum(m_total);
    setSum();
}

void Sum::calcSum (int a_temp)
{
    int m_total = m_total + a_temp;
}

int Sum::setSum (void)
{
    return m_total;
}


//////////////////////////////////////////////////////////////////
//    Lab 5 - For loop Sum
//    Test.cpp
//////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

#include "Header.h"

int main()
{
    int x = 0;    // x = the number of times the for loop will run
    int i = 0;
    int num = 0;
    Sum total;

    cout << "Number of integers to sum: ";
    cin >> x;

    for(i = 0; i >= x; i++)
    {
        cout << "Enter number " << (i+1) << ": ";
        cin >> num;
        cout << endl;
        total.calcSum(num);
    }

    cout << "\n\nTotal sum: " << total.setSum() << endl;

    return 0;
}

Name: dude 2009-03-31 15:37

>>1
In addition to the missing semi colon, you should set m_total to 0 in the constructor(no need for prefixes on your variables here either for that matter) also the two functions you call in the constructor have no effect.  Also yes, there is many sucky things about C++, it will help you learn about low level languages though, and it will make it easy to see how everything fits together when you learn about assembly and computer architecture.  IMO colleges should teach vanilla C instead of C++. (or at least if they use C++, stay away from classes, templates etc. and save that stuff for the java or C# class).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List