Inifnite sentinel controlled while loop

I'm having an issue using two nested sentinel controlled while loops. I just finished up an intro programming class and this is teh final assignment that I am stuck on.

My searching led me to this post
http://www.cplusplus.com/forum/beginner/8353/

which then led me to this post
http://www.cplusplus.com/forum/articles/6046/

This seems like it would solve my problem except that it goes beyond the scope of the class and while I spent some time sidetracked researching getline (which I learned is a built in function) I'm not entireley sure how I would apply it in the context of an array in a sentinel controlled loop. Here is my function for collecting input so far. By inserting cout statements prior to each section of code I was able to determine the hangup is at the execution of the while loops.

I'm not looking for someone to finish the code for me, I want to learn what I'm doing wrong and how to fix it. I'm using VS C++ 2010 Express on Win 7 32bit.

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
double inputNumbers()
// Write in up to 10 numbers
{
	// Declare variables
	double inputNum[10]; // Array to store up to to numbers
	double sum;          // Store the sum of the numbers entered
	int index;           // Loop counter to collect input
	char reenter;        // Sentinel value to determine whether or not to reenter data

	// Inititalize variables
	index = 0;
	sum = 0;
	reenter = 'Y';

	// Sentinel controlled loop to collect input and allow user to reenter data if they made a mistake
	while (reenter == 'Y' || 'y');
	{
		cout << "Enter a rational number (or * to quit): " << endl;
		cin >> inputNum[index];
		
		// Loop to collect input
		while (inputNum[index] != '*');
		{
			sum = sum + inputNum[index];
			index++;
			cout << "Enter another rational number (or * to quit): " << endl;
			cin >> inputNum[index];
		}
		// Confirm accuracy of entries made
		cout << "Please take a moment to review your data for accuracy." << endl;
		cout << "If you would like to reenter your numbers, enter Y, otherwise enter N: " << endl;
		cin >> reenter;
	}
	return(sum);
}
i your while (inputNum[index] != '*'); <- the semicolon after while is syntax error.. and also inputNum[index] != '*' <- instead of this put index<10 because you limited your array up to 10 numbers.. and also before this while put: index=0; because if the user put "Y" then your index will be to the end but you must remove to put "y" because when you filled your array then you will reput another numbers.. :-) i think i'm clear.. if you have any question pls ask.
Last edited on
Thanks for the fix on the two additional semicolons on lines 16 and 22. I took all of your advice and almost have my input function worked out now. Thank you! Here is my code as it stands right now. I'm going to mark this is solved as you did help me get past the issue I posted about. I truly appreciate the help! Now I just have to identify why a selection other than Y or y still loops back, but I feel like I've almost got it figured out.
Last edited on
On line 16: while(reenter=='Y' || 'y')

This should cause an infinite loop because 'y'!=0, and therefore it always evaluates to true. I see what you were trying to do, but this isn't logically correct. Replace it with this:

while(reenter=='Y' || reenter=='y')
Last edited on
I've hit my next road block. My instructor gave a whopping 4 lines of instructions on how to pass array values between functions and it isn't very clear. I've read Functions(I) and Functions(II) at http://www.cplusplus.com/doc/tutorial/ and read a few posts regarding it but I'm still confused. My function for inputting data should have no value passed to it, but should return the values input stored as an array. The values of the array should then be passed to a function that averages all the values and returns that average. This is then passed to an output function. My issue is with the passing of nothing to the input function, but returning something. Current output using 1 and 1 for my two input numbers is
-1.85119e+062-9.25596e+061
Can anyone help?

For the sake of testing I changed the array size to 2 until I figure this all out. Testing 10 repeatedly seemed excessive for now.
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
/*******************************************************/
/* File: Avg10Nums.cpp                                 */
/*                                                     */
/* Created by: Jacob W. Rennels                        */
/* Date: 2011.04.16                                    */
/*                                                     */
/* Program to compute the avergae of ten numbers       */
/*                                                     */
/* Inputs: (keyboard)                                  */
/*                                                     */
/* Output:                                             */
/* Print the average of given numbers on the screen    */
/*******************************************************/

#include <iostream> 

using namespace std ;

void displayWelcome(void)
// Display a welcome message
{
	cout << "Average Of Ten Numbers" << endl;
	cout << "Version 1.0" << endl;
	cout << "Created By: Jake Rennels" << endl;
	cout << endl;
	cout << "This program allows you to enter up to a maximum of ten numbers." << endl;
	cout << "The average of those numbers will then be calculated and displayed on screen." << endl;
	cout << endl;
}

double inputNumbers()
// Write in up to 10 numbers
{
	// Declare variables
	double inputNum[2]; // Array to store up to ten numbers
	int index;           // Loop counter to collect input
	
	// Loop to Collect 10 values
	for (index = 0; index <2; index++)
	{
		cout << "Enter a rational number: ";
		cin >> inputNum[index];
	}
	cout << endl;
	return inputNum;
}

double averageNumbers(double inputNum)
// Average the numbers collected in the inputData function
{
	// Declare variables
	int index = 0;   // Loop counter/array index
	double sum = 0;  // Sum of inputNum array values
	double avg = 0;  // Average of inputNum array values
	double arrayNum[2];

	// Loop to sum 10 values
	for (index = 0; index <2; index++)
		sum = sum + arrayNum[index];
	avg = sum / index;
	cout << sum;
	cout << avg;
	return inputNum;
}

void outputAverage(void)
// Read out the average computed in the averageNumbers function
{

}

int main()
{
	// Declare variables
	double inputArray[2]; // Array to hold the ten numbers
	double avg;
	// Declare functions
	void displayWelcome(void);
	double inputNumbers();
	double averageNumbers(double inputNum[2]);
	//double outputAverage();
	
	// Program execution
	displayWelcome();
	inputNumbers();
	avg=averageNumbers(inputArray);
	//outputAverage();
	return 0;
}


Updated code to latest revision. Still having trouble with line 45 as described below. Trying not to overpost on this topic.
Last edited on
PiMaster, thank you soooo much. I reworked the input section to ditch the chance to reenter data in favor of pressing on with the rest of it to meet my deadline of midnight tomorrow night. I should be able to go back and code that how I originally wanted to now. I'm learning more today than I did in the last two weeks of class.
A friend just pointed out that lines 45 and 62 attempt to pass a location that doesn't exist. I'm getting somewhere. I'm just not sure how to pass the values of the array. If I change them from
 
return inputNum[2];

to
 
return inputNum;

I get compiler error of return value type does not match the function type. I have everything decalred as double so far as I am seeing. Is anyone able to help me make sense of this error?

Edit: Fixed line 65 error. Updating code above to reflect changes. Just need to figure out line 45 now.
Last edited on
Have a working product. From what I understand, it's not an ideal way to deal with the parameters and functions, but I'll ask for that help in a different thread.

Thanks to those that offered their help!
Topic archived. No new replies allowed.