A Problem about a Sentinel

So, I have been working on this problem for quite some time and I know I am making mistakes but I don't know what those mistakes are. First of all I would like to know how to get my sentinel to work, second in importance how to get my first function to work...the rest of the functions are not quite as important because I have a suspicion that if I can just understand the two I mentioned then the rest will probably fall into place.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  //Prog04.cpp
//my name
//my class 
// Program 04
//February 19 2016
//A program that performs one of three operations based on the input values.
// One: If the user inputs a value from 10 to 50 (inclusive), then you will compute the
//sum of the numbers between 1 and the input value.
//Two: If the user inputs a non-negative value less than 10, then you will compute the
//product of the values between 1 and the number.
//Three If the user inputs a value greater than 50, then you will compute the remainder
//of that value divided by 13.
//The program also prompts the user to input the required value and then prints the answer 

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int myPrompt(void);
int mySum(int value);
int myProd(int value);
int myMod(int value);
int myPrint(int problem, int value);

int main() {

	int value;
	int total; 
	const int SENTINEL = -9999; 
    //can't figure out how to make sentinel work without having an initial prompt outside the myPrompt function. 
	//cout << "Enter a non-negative integer (-9999 to end):";
	//cin >> value;
    
   myPrompt();

   while (value != SENTINEL ) {
		

	if (value >= 10 || value <= 50){
		int problem = 1;
	   mySum(value);
	   myPrint(problem, value);
	   cout << total << endl; 
		  }                              
	else if (value < 10) {
		int problem = 2;
		myProd(value);            
		myPrint(problem, value);
		cout << total << endl;         
	}
	else if (value > 50) {
		int problem = 3;
		myMod(value); 
		myPrint(problem, value);
		cout << total << endl; 
	}
		

}
	return 0; 
}



//my functions 

int myPrompt(void){
	int value;
const int SENTINEL = -9999; 

	cout << "Enter a non-negative integer (-9999 to end):";
	cin >> value;
	if (SENTINEL != value) {
	while (value < 0  ) {    
		cout << "Enter a non-negative integer (-9999 to end):";
		cin >> value;
		if (value == SENTINEL) {
			value = SENTINEL; break; 
		}
	}
	}

	  
	return value;                    
	}

int mySum(int value){ 
	int total; 

	for (int i = 1; i <= value; ++i) {


		total += i;

	}
	return total;

}

int myProd(int value){
	int total; 

	for (int i = 1; i <= value; ++i) {


		total = total*i;

	}
	return total;
}

int myMod(int value){
	int total; 

	total = value%13;

	return total; 

}

int myPrint(int problem, int value){ 

	switch (problem)
	{
	case  1:
		cout << "The sum from 1 to "<< value << " is " ;
		break; 
	case  2:
		cout << "The product from 1 to " << value << " is " ; 
			
		break;
	
	default:
		cout << " The remainder of " << value << " divided by 13" << " is " ;
	}
    return 0; 
}
First off, and there will probably be plenty of people that disagree with me, but creating a function like myPrompt() is just wrong in the first place. A function that is only called once serves no purpose. All that aside, you give it a void argument, which is another useless thing. You also give it a return value of an integer, which is also meaningless, as your code is written.

The logic of the function is completely wrong, and would be probably be more noticeable if it was not a function. You set const int SENTINEL = -9999; , then ask for a positive number. Your if statement will only ever be true if the user enters -9999, which is all but guaranteed not to happen. If they enter a positive value, as would be expected, your while loop will never be entered, and the only way to get out of that is to enter -9999 or a positive number.

Since you do not do anything with that returned value, like value = myprompt(); , or initialize the value of value in the main function, it is a good guess that
while (value != SENTINEL ) will always by true.
The value variables declared at lines 28 and 69 are different variables. So when you check value at line 37, you're checking the uninitialized value declared at line 28. To fix this, just change line 35 to:
value = myPrompt();
You have a similar problem with the total variable. Change line 42 to
total = mySum(value);

If the user doesn't enter the sentinel, then the loop at line 37 will continue forever. I would change lines 35-37 to:
1
2
3
4
5
    while (true) {
        value = myPrompt();
        if (value == SENTINEL) {
            break;
        }

In mySum() and myProd(), you need to initialize total to something. Be careful here: the right initial value is not the same in the two functions.

The logic at line 40 is wrong. Every number is either greater than 10 or less than 50.

I would get rid of myPrint() completely and just print the appropriate strings where you currently call myPrint(). It does something different at each call site anyway so there is no code savings by making it a function.

I respectfully disagree with some of admkrk's comments:
A function that is only called once serves no purpose.
It makes the code easier to read, write and maintain, which are all major reasons for any high level language.

you give it a void argument, which is another useless thing.
I'm not sure if he's objecting to the declaraion (int myPrompt(void) vs int myPrompt()) or the fact that it takes no arguments. I think it's fine to have functions that take no arguments but I agree that they should be declared as func() instead of func(void).

You also give it a return value of an integer, which is also meaningless, as your code is written.
The problem isn't the return value, the problem is that you were ignoring it.

You set const int SENTINEL = -9999; , then ask for a positive number.
The prompt is cout << "Enter a non-negative integer (-9999 to end):"; This seems crystal clear to me.
The logic of [myPrompt()] is completely wrong
I would code it differently, but other than the unnecessary value = SENTINEL at line 79, the logic is fine.
If they enter a positive value, as would be expected, your while loop will never be entered,
That's the point. If they enter a positive number then myPrompt() returns it.
and the only way to get out of the while loop is to enter -9999 or a positive number.
That's the point of the while loop.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int myPrompt(void);
int mySum(int value);
int myProd(int value);
int myMod(int value);
int myPrint(int problem, int value);

int main() {

	int value = 1;
	int total = 0; 
	const int SENTINEL = -9999; 
 


myPrompt(); 

   while (true ) {

	value =	 myPrompt();
	if (value = SENTINEL) {
	break; 
	}


   if (value >= 10 || value <= 50){
		int problem = 1;
	 total = mySum(value);
	   myPrint(problem, value);
	   cout << total << endl; 
		  }                              
	else if (value < 10) {
		int problem = 2;
		total = myProd(value);            
		myPrint(problem, value);
		cout << total << endl;         
	}
	else if (value > 50) {
		int problem = 3;
		total = myMod(value); 
		myPrint(problem, value);
		cout << total << endl; 
	}
		

}
	return 0; 
}



//my functions 

int myPrompt(){
	const int SENTINEL = -9999;
	int value;
	
 cout << "Enter a non-negative integer enter -9999 to end:";

 cin >> value;
	
	while (value < 0  ) {    
	if (value != SENTINEL) {	
		cout << "Enter a non-negative integer (-9999 to end):";
		cin >> value;
	}
		break; 
	}
	

	  
	return value;                    
	}

int mySum(int value){ 
	int total = 0; 

	for (int i = 1; i <= value; ++i) {


		total += i;

	}
	return total;

}

int myProd(int value){
int total = 1; 

	for (int i = 1; i <= value; ++i) {


		total = total*i;

	}
	return total;
}

int myMod(int value){
	int total = 1; 

	total = value%13;

	return total; 

}

int myPrint(int problem, int value){ 

	switch (problem)
	{
	case  1:
		cout << "The sum from 1 to "<< value << " is " ;
		break; 
	case  2:
		cout << "The product from 1 to " << value << " is " ; 
			
		break;
	
	default:
		cout << " The remainder of " << value << " divided by 13" << " is " ;
	}
    return 0; 
}


I changed a few things slightly but still, don't really get what to do...
Last edited on
Delete line 20.

Line 25 is wrong. = should be ==.

Fix the logic on line 30.
@dhayden As I said, I expected disagreement about a single use function, but maybe it would be better to append, "that are only a couple of lines", to my statement. In this case, it made it harder to manage, as several of those errors would have been avoided if it was just written outright. I should also apologize for my language use, I did a horrible job of choosing my words, which I think is the cause of most of your counter points.
Final version of the code:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int myPrompt(void);
int mySum(int value);
int myProd(int value);
int myMod(int value);
int myPrint(int problem, int value);

int main() {

	int value = 1;
	int total = 0; 
	const int SENTINEL = -9999; 
 

   while (true ) {
	value =	 myPrompt();
	if (value == SENTINEL) {
	break; 
	}


	if (value >= 10 && value <= 50){
		int problem = 1;
	 total = mySum(value);
	   myPrint(problem, value);
	   cout << total << endl; 
		  }                              
	else if (value >= 0 && value < 10) {
		int problem = 2;
		total = myProd(value);            
		myPrint(problem, value);
		cout << total << endl;         
	}
	else if (value > 50) {
		int problem = 3;
		total = myMod(value); 
		myPrint(problem, value);
		cout << total << endl; 
	}
		

}
	return 0; 
}



//my functions 

int myPrompt(){
	const int SENTINEL = -9999;
	int value;
	
 cout << "Please enter a non-negative integer. (enter -9999 to quit) :";

 cin >> value;
	
	while (value < 0  ) {    
	if (value != SENTINEL) {	
		cout << "Please enter a non-negative integer. (enter -9999 to quit) :";
		cin >> value;
	}
		break; 
	}
	

	  
	return value;                    
	}

int mySum(int value){ 
	int total = 0; 

	for (int i = 1; i <= value; ++i) {


		total += i;

	}
	return total;

}

int myProd(int value){
int total = 1; 

	for (int i = 1; i <= value; ++i) {


		total = total*i;

	}
	return total;
}

int myMod(int value){
	int total = 1; 

	total = value%13;

	return total; 

}

int myPrint(int problem, int value){ 

	switch (problem)
	{
	case  1:
		cout << "The sum from 1 to "<< value << " is " ;
		break; 
	case  2:
		cout << "The product from 1 to " << value << " is " ; 
			
		break;
	
	default:
		cout << " The remainder of " << value << " divided by 13" << " is " ;
	}
    return 0; 
}


It actually works!! Thanks you guys! :)
Topic archived. No new replies allowed.