why doesnt this program output anything?

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
#include <iostream>
#include <string>

using namespace std;

class Counter
{
  public:
   Counter ();        //initializes the counter value to 0.
   Counter(int new_val); // value is set according to the 
                         // incoming argument.
   void increment(int new_val); //increment counter value by 1.
   int get_value(int new_val);  //returns the value of member
                     //variable
  private:
        int value;
};
int main () {
	Counter value = 5;
}
void Counter :: increment (int new_val) {
	if (new_val < 10) {
		new_val = value + 1;
	}
}
int Counter :: get_value (int new_val) {
	return value;
	return new_val;
}
Counter :: Counter (int new_val) {
}
Last edited on
i get i dont need the string header but i will
Why should this program output anything, you don't tell it to do.
return output same thing to me
but even when i do cout << new_val << value << endl; or something similar just like cout << new_val; nothing outputs
but even when i do cout << new_val << value << endl; or something similar just like cout << new_val; nothing outputs



You are asking us to debug a program that doesn't exist. @Thomas1965 told you that the program, as given to us, never asks for anything to be printed out. I agree with him. And if you take the line of code you gave us and put it --where? in increment? or do you put it in get_value?-- it still would not get executed because neither of those functions get called either.

By the way, lines 27 and 28 are a major problem. You cannot return from a function twice. I'm not sure what you are trying to do, but this is not right.
all im trying to do is make a class which is made then suppose to create a Counter object with a value and if that value is lower than 10 im suppose to increment the number by one but i put the cout in the int counter function but yea i know you cant return 2 things i was surprised that didnt give me an error but where do i call them? i thought it was already called in the class
create a Counter object with a value and if that value is lower than 10 im suppose to increment the number by one

So it sounds like the constructor that takes in a number should set the value and if it's less than 10, call the increment function.

Is 10 the minimum - so if the starting value were 5, would you need to bump it up 1 number at a time to 10?

Should increment just add one to the value and the calling function call increment as many times as necessary to get the counter value to the minimum of 10? Or should increment do all the work of making sure the counter value is the minimum so the calling function just has to call increment once?

If increment is just adding 1 to the data member, does it need to take an integer argument? Similarly, if the get_value function is just returning the current value of the data member value, does it need to take an integer argument?

What's happening below now is that increment is taking whatever number it is sent, and if that number is less than 10, setting it to the current value of the counter plus 1. The actual data member called 'value' isn't getting changed here, just a local variable in the function.
1
2
3
4
5
void Counter :: increment (int new_val) {
	if (new_val < 10) {
		new_val = value + 1;
	}
}
i thought it was already called in the class


You keep saying things that are perfectly obvious to you, but are without context to those of us who are trying to help you. I have not idea what the above statement means.

Why don't you post the code that has the cout << new_val << value << endl; statements in it? Then there will be no ambiguity about what you mean, and we can show you exactly why your code is not doing what you expect it to. We can't tell you why your program doesn't output anything if all of the output statements are removed from the program that you posted.

Edit:

return output same thing to me

I just figured out what you meant by this statement. I know I sound like an old fart, but capitalization and punctuation were invented to help us comprehend what is written. Without any, this collection of words (I would not call it a sentence) has a number of meanings.

I think you are saying that returning a value is the same thing to you as outputting a value. Not only is this not a true statement, it makes it harder for us to figure out what your question really is.

After reposting your code, please clarify what your specific question really is.
Last edited on
closed account (48T7M4Gy)
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
36
#include <iostream>

class Counter
{
public:
    Counter();
    Counter(int new_val);
    
    void increment();
    int get_value();

private:
    int value;
};

Counter::Counter (int new_val) {
    value = new_val;
}

void Counter::increment () {
    if (value < 10) {
        ++value;
    }
}

int Counter::get_value () {
    return value;
}

int main () {
    Counter some_counter(5);
    std::cout << some_counter.get_value() << '\n';
    
    some_counter.increment();
    std::cout << some_counter.get_value() << '\n';
}
5
6
Program ended with exit code: 0
"You keep saying things that are perfectly obvious to you, but are without context to those of us who are trying to help you. I have not idea what the above statement means."
doug4 that is completely in context because the comment before that you said "it still would not get executed because neither of those functions get called either."
but thanks all for the help and yea i am vague sometimes doug sorry
Topic archived. No new replies allowed.