Corrections (1st assignment)?

I did my assignment and lost four points. This is what my professor said.
// -1 indenting – condtionalAndLoopCalcuiations(), WHOLE while() LOOP NOT INDENTED
// -1 reference – VALUES NOT RETURNED AND USED BY CALLER

-1 THERE IS NO INITIAL MESSAGE TO LET THE USER KNOW WHAT TO DO

* ON EXIT, HIGHEST/LOWEST ARE REPEATED – GOODBYE WOULD HAVE BEEN A GOOD MESSAGE

Enter -99 to quit entering.

45

The highest number you entered was 45

The lowest number you entered was 45

Enter -99 to quit entering.

67

The highest number you entered was 67

The lowest number you entered was 45

Enter -99 to quit entering.

23

The highest number you entered was 67

The lowest number you entered was 23

Enter -99 to quit entering.

-99

The highest number you entered was 67

The lowest number you entered was 23

Press any key to continue . . .

-1 KEEP OUT OF USER MESSAGES AS WELL

cout << "Enter -99 to quit entering." << endl;

I'm not quite sure what he's saying or how to correct them in my code, so I was hoping someone could me with this and my other two assignment mistakes.

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
//Ashton Dreiling
//Largest and Smallest exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

//module prototype
void condtionalAndLoopCalcuiations (double &number, double &high, double &low, double &couner);


//global constants
const double ZERO_FOR_CALCULATION = 0;
const double NEGATIVE_NINTY_NINE_FOR_CALCULATION = -99;

int main()
{
    double number = ZERO_FOR_CALCULATION;
    double high;
    double low;
    double counter = ZERO_FOR_CALCULATION;

    condtionalAndLoopCalcuiations(number, high, low, counter);

    system("Pause");

    return 0;


}//end main


    void condtionalAndLoopCalcuiations(double &number, double &high, double &low, double &counter)
    {

    while (number != NEGATIVE_NINTY_NINE_FOR_CALCULATION)
    {
        cout << "Enter -99 to quit entering." << endl;
        cin >> number;
        if (counter == ZERO_FOR_CALCULATION)
        {
            high = number;
            low = number;
            counter++;

        }//end of conditional statement
        else
        { if (number > high && number != NEGATIVE_NINTY_NINE_FOR_CALCULATION)
        high = number;
        else if (number < low && number != NEGATIVE_NINTY_NINE_FOR_CALCULATION)
                       low = number;

        }//end of conditional statement

    cout << "The highest number you entered was " << high << endl;
    cout << "The lowest number you entered was " << low << endl;

    }//end of which loop statement
    }//end of conditionalAndLoopCalculations module
// -1 indenting – condtionalAndLoopCalcuiations(), WHOLE while() LOOP NOT INDENTED


Your indentation was off a bit. Check lines 47-51.

-1 THERE IS NO INITIAL MESSAGE TO LET THE USER KNOW WHAT TO DO


You didn't provide a message on entering to let the user know what to do. It just sort of opens a console window telling the user to enter -99 to quit entering. Just telling someone the purpose of the program on entry would probably be sufficient.

// -1 reference – VALUES NOT RETURNED AND USED BY CALLER


I assume he wants you to pass values from the function, and not just have the function perform the entire task. Or in other words, your function serves multiple purposes rather than completely specialize. It's not the worst setup, but it could be split into different functions.

* ON EXIT, HIGHEST/LOWEST ARE REPEATED – GOODBYE WOULD HAVE BEEN A GOOD MESSAGE


You didn't provide a message indicating the program was complete and would be terminating. You just sort of paused the system (which I'm to understand is a bad idea for reasons I don't understand) and it will abruptly exit.
Last edited on
This is my edited code. I believe I fixed the indenting and text issues (tell me if I didn't), but I'm not sure how to fix the reference one since I did pass values by reference (I even used the &).

Mind explaining more?

//Ashton Dreiling
//Largest and Smallest exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

//module prototype
void condtionalAndLoopCalcuiations (double &number, double &high, double &low, double &couner);


//global constants
const double ZERO_FOR_CALCULATION = 0;
const double NEGATIVE_NINTY_NINE_FOR_CALCULATION = -99;

int main()
{
double number = ZERO_FOR_CALCULATION;
double high;
double low;
double counter = ZERO_FOR_CALCULATION;
cout << "You're going to enter a series of numbers and we're going to display the largest and smallest numbers entered." << endl;

condtionalAndLoopCalcuiations(number, high, low, counter);

system("Pause");

return 0;


}//end main


void condtionalAndLoopCalcuiations(double &number, double &high, double &low, double &counter)
{

while (number != NEGATIVE_NINTY_NINE_FOR_CALCULATION)
{
cout << "Enter -99 to quit entering." << endl;
cin >> number;
if (counter == ZERO_FOR_CALCULATION)
{
high = number;
low = number;
counter++;

}//end of conditional statement
else
{ if (number > high && number != NEGATIVE_NINTY_NINE_FOR_CALCULATION)
high = number;
else if (number < low && number != NEGATIVE_NINTY_NINE_FOR_CALCULATION)
low = number;

}//end of conditional statement

cout << "The highest number you entered was " << high << endl;
cout << "The lowest number you entered was " << low << endl;
cout << "Have a nice day!" << endl;
}//end of which loop statement
}//end of conditionalAndLoopCalculations module
The message looks good.

It's hard to tell about your indentation because you didn't put code tags in your last post. In addition to what Yawzheek said, you need to fix the indentation to line 33 and lines 53-58.

Regarding references, you should only pass a parameter to a function by reference if you intend to have the function modify the parameter, and have the caller use the modified value. Otherwise just pass the parameter by value (or const reference if the data is large, but that's a more advanced issue). So function conditionalAndLoopCalculations could be:
void condtionalAndLoopCalcuiations(double number, double high, double low, double counter);
But in this case, even that is bad. Since none of these values is set or used inside the main program, there's no reason to declare them there. They should ALL be declared inside conditionalAndLoopCalculations:

1
2
3
4
5
6
7
void condtionalAndLoopCalculations()
{
    double number = ZERO_FOR_CALCULATION;
    double high;
    double low;
    double counter = ZERO_FOR_CALCULATION;
    ...
1
2
3
4
5
6
7
8
9
10
11
12
while (std::cin >> number and number not_eq EXIT_VALUE) {
  minMax(number, high, low);
  std::cout << "Highest " << high << '\n';
  std::cout << "Lowest " << low << '\n';
}

void minMax(double value, double &highest, double &lowest) {
  if (value > highest)
    highest = value;
  if (value < lowest)
    lowest = value;
}


> const double NEGATIVE_NINTY_NINE_FOR_CALCULATION = -99;
that's still a magic number
Last edited on
Can you explain how the indentation is supposed to be like with some examples perhaps? I suppose I don't fully understand indentation yet.

As for the reference, are you saying that I missed points because I used none in my main program? If so, I understand, and I won't make the mistake again. : )

I'm glad I fixed the message.
ne555, is it? I got points for it. Perhaps my professor didn't count it as a magic number because the assignment had one specific purpose?
If you don't understand indentation, let your IDE resolve it.
If you do understand it, you'll realize that it's better to let your IDE resolve it.


Suppose that you need to change your program so it ends at -1 instead of -99.
¿Will you change line 14 to const double NEGATIVE_NINTY_NINE_FOR_CALCULATION = -1;? ¿will you leave your loop condition and the ifs as they are?
But my IDE doesn't resolve it. I'm using codeblocks, but I do need to understand it regardless. It may be because of how I've installed it. Indentation is on my exams. I was doing okay, but nested loops have me in a bit of a pickle. If you'd just give me an example or tell me the proper way to align it, that'd be great!

I'd change line 14, right?

Last edited on
What ne555 means is (I assume) that there's usually an option in your preferences you can set yourself to make these changes automatically. I use Visual Studio, so I don't know CodeBlocks options, but in VS, when you create nested loops, provided your braces are OK, it will do the work for you. For example, what I suspect your instructor wanted, VS does for me automatically:

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
void condtionalAndLoopCalcuiations(double &number, double &high, double &low, double &counter)
{

	while (number != NEGATIVE_NINTY_NINE_FOR_CALCULATION)
	{
		cout << "Enter -99 to quit entering." << endl;
		cin >> number;
		if (counter == ZERO_FOR_CALCULATION)
		{
			high = number;
			low = number;
			counter++;

		}//end of conditional statement
		else
		{
			if (number > high && number != NEGATIVE_NINTY_NINE_FOR_CALCULATION)
				high = number;
			else if (number < low && number != NEGATIVE_NINTY_NINE_FOR_CALCULATION)
				low = number;

		}//end of conditional statement

		cout << "The highest number you entered was " << high << endl;
		cout << "The lowest number you entered was " << low << endl;

	}//end of which loop statement
}//end of conditionalAndLoopCalculations module 


I literally copy/pasted that into Visual Studio, and it properly indented it for me, and I have made ZERO changes to the defaults with regards to indentation.

EDIT: Note the indentation is exaggerated for C++.com. I'll provide a screenshot.

EDIT: http://i.imgur.com/Fm7yIaQ.png

That's what VS provided when I copy/pasted your code, and that's what your instructor/teacher meant.
Last edited on
Thanks!

So, based off of what I'm seeing, in a nested loop, after each loop, the next loop should be indented (tabed twice) from the loop before that?

Also, was I correct about my hypothesis for the reference? Since I didn't use them I lost points?
Code::Blocks has a tool for reformatting your code when you screw up the indentation, look for the Source Code Formatting (Astyle) button in the Plugins menu.

That was a big help. Thanks so much!

I see the convention, now.

Finally, for the reference one, it was because I didn't use it in main correct, and what does this mean -1 KEEP OUT OF USER MESSAGES AS WELL?

The whole thing was actually


-1 KEEP OUT OF USER MESSAGES AS WELL

cout << "Enter -99 to quit entering." << endl;
Last edited on
So, based off of what I'm seeing, in a nested loop, after each loop, the next loop should be indented (tabed twice) from the loop before that?


One tab is sufficient, and kind of?

The way you nest loops and how they interact is important. I'm not particularly good at explaining things tersely, but:

http://i.imgur.com/tlgzpjT.png

Kind of gives the basic idea. Sometimes "nesting" statements are unavoidable, but I'd suggest you look up switch statements. In the case where you need to go beyond if/else multiple times, it's an easier to read format. It has drawbacks (you can't use strings, for example) but it's usually much simpler. A brief introduction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void my_nesting_for_FBHSIE()
{
	cout << "Enter a number between 1 and 3: ";
	int sample{ 0 };
	cin >> sample;
	switch (sample) {
	case 1:
		cout << "Well done!\n";
		break;
	case 2:
		cout << "Well done!\n";
		break;
	case 3:
		cout << "Well done!n";
		break;
	default:
		cout << "Sorry, " << sample << " wasn't between 1 and 3. Try again... ";
		my_nesting_for_FBHSIE();
	}
}


EDIT: As far as your reference issue is concerned, I'm not certain what it is he wanted, so I apologize. I assume he wanted you to create a function that passed values to another function, based on what you provided.

// -1 reference – VALUES NOT RETURNED AND USED BY CALLER


It's a bit ambiguous to me, and you should probably contact your instructor to clarify.
Last edited on
" // -1 reference – VALUES NOT RETURNED AND USED BY CALLER"


It's a bit ambiguous to me, and you should probably contact your instructor to clarify.


Actually it's not really all that ambiguous. You are passing variables by reference into your function but the calling function never really uses these variables. Therefore it would probably be better if you declared the variables local to the function.

Another option would be to move the printing of the variables into main(), and just pass those two variables by reference to the function. The other two variables should still be declared inside the function.
Last edited on
That's what I thought as well, jib.

Thanks for the advice, Yaw. Well, that solves everything here!
Actually it's not really all that ambiguous.


To be fair, I never claimed to be a good programmer :)

I read so much of his code that I saw the indentation problems and saw references passed to a function that used them. For all the beginner I am, it looked fine, if not convoluted. Again, I never claimed to be a good programmer, so as I said, the instructions were ambiguous, at least to me ;)
You're fine boys! I'm a poorer programmer than both of you combined at the moment.

Any advice is good advice. <3
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
37
38
39
40
41
42
43
44
45
46
#include <iostream>

void condtionalAndLoopCalcuiationsDespiteTheTypo(double &, double &, double &, double &);

int main()
{
    double number = 0;
    double high = 0;
    double low = 0;
    double counter = 0;

    condtionalAndLoopCalcuiationsDespiteTheTypo(number, high, low, counter);
    
    return 0;
}

void condtionalAndLoopCalcuiationsDespiteTheTypo(double &number, double &high, double &low, double &counter)
{
    const int QUIT_NO = -99;
    const int LOWEST_POSSIBLE_NUMBER = 0;
    const int ANY_HIGHER_THAN_LOWEST_NUMBER = 1000;
        
    while (number != QUIT_NO)
    {
        std::cout << "Enter -99 to quit entering.\n";
                    
        if ( counter == 0 )
        {
            high = LOWEST_POSSIBLE_NUMBER;
            low = ANY_HIGHER_THAN_LOWEST_NUMBER;
            counter++;
        }
        else
        {
            if ( number > high )
                high = number;
            if ( number < low )
                low = number;
        }
            
        std::cin >> number;
    }//end of conditional statement
    
    std::cout << "The highest number you entered was " << high << std::endl;
    std::cout << "The lowest number you entered was " << low << std::endl;
}
Enter -99 to quit entering.
1
Enter -99 to quit entering.
2
Enter -99 to quit entering.
44
Enter -99 to quit entering.
-99
The highest number you entered was 44
The lowest number you entered was 1


Topic archived. No new replies allowed.