Can't figure out what I did wrong

I started today learning C++ for the first time. I've been following a video series for beginners which has helped me a lot. But ever since it's gotten into classes and objects and stuff I've been really struggling. I finally thought I understood it so tried something on my own. I was really sure I got it right, but there is 3 errors and I can't figure out why.

The video in case anyone was wondering: http://thenewboston.org/watch.php?cat=16&number=13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
using namespace std;



class Q{
    public:

       void number(int a, int b){
           sum = a + b;

       }

        int emergen(){
         return sum;

        }


    private:
        int sum;

};

int main()
{

    Q inwin;
    inwin.number(60,10);
    cout << inwin.sum();


    return 0;
}
Last edited on
cout << inwin.sum();
sum is a private variable and thus can only be accessed from within the class.
What you probably wanted to do is this:

cout << inwin.emergen();
Your class has no such a method as sum(). Instead of it you should use method emergen().
cout << inwin.sum();

should be

cout << inwin.emergen(); ?

Last edited on
Can't believe I made such a simple mistake, even I knew that was wrong T_T
thanks for the help
Another question, How can I make it so the user can input the numbers instead of me having to set certain numbers in the code to add together? I can do it if its all in one function, but I don't know how to do it this way.
1
2
3
4
5
6
7
8
9
void number()
{
           cout << "Enter a sequence of numbers (Ctrl+z - exit): "

           sum = 0;
           int x;

           while ( cin >> x ) sum += x;     
}
With that while loop will it repeat until you do not enter in a value that is an int?
Last edited on
It will repeat until std::cin is placed in an error state - this is usually for failed input, so yes - if you input something that isn't a number or if the input ends (e.g. Ctrl+Z in a console).
ah ty =)
Topic archived. No new replies allowed.