Drill Chapter 4 Programming Principles and Practice

Hello, I am an extreme beginner at C++ and am trying to learn this language through one of the creators books. I'm working on the drill in Chapter 4 and am having trouble with number 9 on the drill list.

it asks me to keep track of the sum of values entered(as well as the smallest and the largest) and the number of values entered. When the loop ends, print the smallest, the largest, the number of values, and the sum of values. Note that to keep the sum, you have to decide on a unit to use for that sum; use meters.

I thought i figured out how to find the largest and smallest number so far, but now every time i run the program it totally skips over the second number i input. the sum is also off and im not sure how to fix it. i have been working on this code for over a day and a half now and it seems like i keep managing to make it worse. If someone could kindly give me hints or direct me to the best solution, i would be very grateful. like i said, im extremely new to this and would rather have a solid explanation rather than code thrown at me. thank you!
feel free to run this code yourself!


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
  #include <iostream>
#include <cmath>
#include <vector>
#include <stdlib.h>
#include <algorithm>

using namespace std;

int main()
{
        double sum = 0;
        string unit;
        double conversion = 0;
        const double cm = 100;
        const double m = 1;
        const double in = 39.3701;
        const double ft = 3.28084;
        double num;
        double min;
        double max;
        bool minfound = false;
        bool maxfound = false;

        while (cin >> num >> unit) {

          sum += num;


          if ((!maxfound) || (num >= max)) {
            max = num;
            maxfound = true;
            cout << max << " the largest number so far" << endl;
          }
          if ((!minfound) || (num <= min)){
            min = num;
            minfound = true;
            cout << min << " the smallest number so far" << endl;
          }

          if (unit == "cm"){
                conversion = cm * num;
                cout << num << " conversion to meters: " << conversion << endl;
          }
          if(unit == "m"){
                conversion = m * num;
                cout << num << " conversion to meters: " << conversion << endl;
          }
          if (unit == "in"){
                conversion = in * num;
                cout << num << " conversion to meters: " << conversion << endl;
          }
          if (unit == "ft"){
                conversion = ft * num;
                cout << num << " conversion to meters: " << conversion << endl;
          }

        cout << "The sum is: " << sum << "\n";

        }
}
Your booleans do not really do anything, so you do not need them.

The key to finding minimum and maximum values is to initialize the variables so that the first value entered will be both the minimum and the maximum. In other words, if I enter 5 first, it will be the largest and smallest number, since no other number was entered yet. The same should hold true if I entered 500 first.
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
cout << "Enter some numbers: " << endl;

double num = 0;

double smallest = 1.0/0.0; //setting smallest to infinity
double largest = -1.0/0.0; //setting largest to -infinity

while (cin>>num){ //while you take in user input


if (num == smallest || num < smallest){
smallest = num;
cout<<num<<" is smallest so far."<<endl;
}
else if(num>largest){
largest = num;
cout<<num<<" is largest so far."<<endl;
}
}





Thanks for the tip! okay! so i've deleted alot of things as you can see and now am solely focusing on finding the max and min values. I have gotten it to work pretty darn well except for the fact that the first number is always the least and greatest value so far. however, it only outputs that it is the smallest number so far.
I have figured it out!! turns out the "else" in the "else if" statement was totally unneeded! success!! for now..

thanks for the help again!
I am surprised you do not get a divide by zero error with:

1
2
double smallest = 1.0/0.0; //setting smallest to infinity
double largest = -1.0/0.0; //setting largest to -infinity 


Unless you are accepting negative measurements, initializing largest to 0 is sufficient. Any large value, as long as it is more than the input, will also work for smallest.

1
2
3
4
#include <limits>

const double INFINITY = numeric_limits<double>::max();
double smallest = INFINITY


Will ensure that you have smallest initialized to the highest value the variable can hold.

I have gotten it to work pretty darn well except for the fact that the first number is always the least and greatest value so far

I am confused as to why that would be wrong? The first number will be both, and should be expected output for the first number.

You also do not need the or:

1
2
3
4
5
if (num == smallest || num < smallest)
// Can be replaced with
if(num <= smallest)
// and there is nothing wrong with 
if(num >= largest)


Unless you repeatedly enter the same number, they will only be equal for the first number.

And thank you for working it out on your own.

My bad, I was stating that the first number entered should be the greatest and least value because it is the only one. My program, however, was only outputting that it was the least value so far. When I deleted the "else" from the "else if" statement, it fixed the problem.

I have struck another problem, unfortunately. I have gotten my program to allow input of numbers and units: centimeters, meters, feet, inches. Next, I convert every number to meters and also state the least and greatest number so far, along with the sum after I enter each number. The problem is, is that I now need to create a vector that holds all the converted numbers in order to find the greatest value, least value, number of elements in the vector and to print out all those elements. I know how to find all of this once I create the vector, but I am unsure on where to put it. Hints in the right direction would be amazing! thank you!

here's my code, once again feel free to run it!



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
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
        cout << "Enter some numbers: " << endl;

        double num = 0;

        const double cm = 100;
        const double in = 39.3701;
        const double ft = 3.28084;
        const double m = 1;

        string unit = "";

        double smallest = 1.0/0.0;   //setting smallest to infinity
        double largest = -1.0/0.0;   //setting largest to -infinity

        double sum = 0;


        while (cin>>num>>unit){ //while you take in user input

                if (unit == " "){ //if number does not have unit, but " "
                        double meter = num;
                        cout<<num<<" = "<<num<<"m"<<endl;
                        num = meter;
                        sum+=num; //add converted number to sum
                }
                if (unit == "cm"){ //centimeter
                        double meter = num/cm; //convert cm to m
                        cout<<num<<"cm = "<<meter<<"m"<<endl;
                        num = meter; //num equals converted number
                        sum+=num; //add to sum
                }
                else if (unit == "in"){ //inch
                        double meter = num/in; //convert in to m
                        cout<<num<<"in = "<<meter<<"m"<<endl;
                        num = meter; //num equals convert number
                        sum+=num; //add to sum
                }
                else if (unit == "ft"){ //feet
                        double meter = num/ft; //convert to m
                        cout<<num<<"ft = "<<meter<<"m"<<endl;
                        num = meter; //num equals converted num
                        sum+=num; //add to sum
                }
                else if (unit == "m"){ //meter
                        double meter = num/m; //convert meter to meter
                        cout<<num<<"m = "<<meter<<"m"<<endl;
                        num = meter;
                        sum+=num; //add to sum
                }
                else{
                        cout<<"Not a valid unit."<<endl;
                }
        cout<<"Sum: "<<sum<<endl; //print out sum

                if (num == smallest || num < smallest){
                        smallest = num;
                        cout<<num<<" is smallest so far."<<endl;

                }
                if(num>largest){
                        largest = num;
                        cout<<num<<" is largest so far."<<endl;
                }[code]

}

vector<double>numbers; //vector to hold converted numbers
for(double meter;cin>>meter;){
numbers.push_back(meter);
}
cout<<"Number of numbers: "<<numbers.size()<<"\n"; //size


}



[/code]
oh geez I'm horrible at this.. let me try to put my code up again



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

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
        cout << "Enter some numbers: " << endl;

        double num = 0;

        const double cm = 100;
        const double in = 39.3701;
        const double ft = 3.28084;
        const double m = 1;

        string unit = "";

        double smallest = 1.0/0.0;   //setting smallest to infinity
        double largest = -1.0/0.0;   //setting largest to -infinity

        double sum = 0;


        while (cin>>num>>unit){ //while you take in user input

                if (unit == " "){ //if number does not have unit, but " "
                        double meter = num;
                        cout<<num<<" = "<<num<<"m"<<endl;
                        num = meter;
                        sum+=num; //add converted number to sum
                }
                if (unit == "cm"){ //centimeter
                        double meter = num/cm; //convert cm to m
                        cout<<num<<"cm = "<<meter<<"m"<<endl;
                        num = meter; //num equals converted number
                        sum+=num; //add to sum
                }
                else if (unit == "in"){ //inch
                        double meter = num/in; //convert in to m
                        cout<<num<<"in = "<<meter<<"m"<<endl;
                        num = meter; //num equals convert number
                        sum+=num; //add to sum
                }
                else if (unit == "ft"){ //feet
                        double meter = num/ft; //convert to m
                        cout<<num<<"ft = "<<meter<<"m"<<endl;
                        num = meter; //num equals converted num
                        sum+=num; //add to sum
                }
                else if (unit == "m"){ //meter
                        double meter = num/m; //convert meter to meter
                        cout<<num<<"m = "<<meter<<"m"<<endl;
                        num = meter;
                        sum+=num; //add to sum
                }
                else{
                        cout<<"Not a valid unit."<<endl;
                }
        cout<<"Sum: "<<sum<<endl; //print out sum

                if (num == smallest || num < smallest){
                        smallest = num;
                        cout<<num<<" is smallest so far."<<endl;

                }
                if(num>largest){
                        largest = num;
                        cout<<num<<" is largest so far."<<endl;
                }
        }

        vector<double>numbers; //vector to hold converted numbers
        for(double meter;cin>>meter;){
                numbers.push_back(meter);
        }
        cout<<"Number of numbers: "<<numbers.size()<<"\n"; //size


}
Unless I am not understanding you, you had that covered originally:

sum += num;

Unless you actually need the values, there is no reason to store them.
Last edited on
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
#include <iostream>
#include <string> 

int main()
{
    const double cms_per_metre = 100;
    const double inches_per_metre = 39.3701;
    const double ft_per_metre = 3.28084;
    // const double m = 1;

    double smallest = 1.0 / 0.0; //setting smallest to infinity
    double largest = -1.0 / 0.0; //setting largest to -infinity
    double sum = 0;
    int cnt = 0 ; // count of valid values entered

    double number = 0; // the entered value
    std::string unit = "";
    double value_in_metres = 0 ; // the entered value converted to metres

    while ( std::cin >> number >> unit ) //while you take in user input
    {
        if( unit == "cm" ) value_in_metres = number / cms_per_metre ;
        else if( unit == "in" ) value_in_metres = number / inches_per_metre ;
        else if( unit == "ft" ) value_in_metres = number / ft_per_metre ;
        else if( unit == "m" ) value_in_metres = number ; // metres, no conversion is required
        else
        {
            std::cout << "Not a valid unit.\n" ;
            continue ; // ignore this input and go back to the beginning of the loop
        }

        std::cout << number << ' ' << unit << " == " << value_in_metres << " metres\n" ;
        ++cnt ;
        sum += value_in_metres ;
        if( value_in_metres < smallest ) smallest = value_in_metres ;
        if( value_in_metres > largest ) largest = value_in_metres ;
     }

     if( cnt > 0 )
     {
         std::cout << "   Count: " << cnt << '\n'
                   << "     Sum: " << sum << " metres\n"
                   << "Smallest: " << smallest << " metres\n"
                   << " Largest: " << largest << " metres\n"
                   << " Average: " << sum / cnt << " metres\n" ;
     }
}
Last edited on
i do need the values in order to print them all out in order in the end. im just wondering where i would put the vector, or how i'd go about it. I keep trying to figure out different ways but its not picking up the converted numbers, which i want.
Do not over think it! Just create an other vector that stores the values as they are input.

1
2
3
4
5
6
7
8
vector<double> all_entries;

while (cin >> num >> unit) {

      sum += num;
      all_entries.push_back(num);
// ...
}


<edit>
If you want to store the converted numbers, the easiest way is to put them in different vectors, depending on the unit.
Last edited on
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>
#include <string>
#include <vector>

int main()
{
    const double cms_per_metre = 100;
    const double inches_per_metre = 39.3701;
    const double ft_per_metre = 3.28084;

    std::vector<double> converted_values ; // vector to hold the converted values

    double number = 0; // the entered value
    std::string unit = "";
    double value_in_metres = 0 ; // the entered value converted to metres

    while ( std::cin >> number >> unit ) //while you take in user input
    {
        if( unit == "cm" ) value_in_metres = number / cms_per_metre ;
        else if( unit == "in" ) value_in_metres = number / inches_per_metre ;
        else if( unit == "ft" ) value_in_metres = number / ft_per_metre ;
        else if( unit == "m" ) value_in_metres = number ; // metres, no conversion is required
        else
        {
            std::cout << "Not a valid unit.\n" ;
            continue ; // ignore this input and go back to the beginning of the loop
        }

        std::cout << number << ' ' << unit << " == " << value_in_metres << " metres\n" ;
        converted_values.push_back(value_in_metres) ; // add the value to the vector
     }

     // print all the values in the vector
     // print sum, smallest, largest etc.

}
So, i understood the tips given and used them in my code. But, my vector is still not printing out all the converted values- only some of them. Can someone please tell me why this is happening?

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

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
        cout << "Enter some numbers with the units: in, m, ft, cm: " << endl;


        const double cm = 100;
        const double in = 39.3701;
        const double ft = 3.28084;
        const double m = 1;

        string unit = "";

        double smallest = 1.0/0.0;   //setting smallest to infinity
        double largest = -1.0/0.0;   //setting largest to -infinity

        int count = 0;

        double sum = 0;

        double num = 0;

        vector<double>converted;//vector to hold converted values
        double meter  = 0;

        while (cin>>num>>unit){ //while you take in user input

                if (unit == " "){ //if number does not have unit, but " "
                        double meter = num;
                        cout<<num<<" = "<<num<<"m"<<endl;
                        sum+=meter;
                        ++count;
                        converted.push_back(meter);
                }
                else if (unit == "cm"){ //centimeter
                        double meter = num/cm; //convert cm to m
                        cout<<num<<"cm = "<<meter<<"m"<<endl;
                        sum+=meter;
                        ++count;
                        converted.push_back(meter);
                }
                else if (unit == "in"){ //inch
                        double meter = num/in; //convert in to m
                        cout<<num<<"in = "<<meter<<"m"<<endl;
                        sum+=meter;
                        ++count;
                        converted.push_back(meter);
                }
                else if (unit == "ft"){ //feet
                        double meter = num/ft; //convert to m
                        cout<<num<<"ft = "<<meter<<"m"<<endl;
                        sum+=meter;
                        ++count;
                        converted.push_back(meter);
                }
                else if (unit == "m"){ //meter
                        double meter = num/m; //convert meter to meter
                        cout<<num<<"m = "<<meter<<"m"<<endl;
                        sum+=meter;
                        ++count;
                        converted.push_back(meter);
                }
                else{
                        cout<<"Not a valid unit."<<endl;
                }
        cout<<"Sum: "<<sum<<endl; //print out sum
        cout<<"Count: "<<count<<endl; //print out counter


        sort(converted.begin(),converted.end());

        for(int i = 0; i < converted.size(); ++i){
                cout<<converted[i]<<" "<<endl;
                ++i;

        }
1
2
3
4
        for(int i = 0; i < converted.size(); ++i){
                cout<<converted[i]<<" "<<endl;
                ++i; // **** get rid of this; we are already incrementing i after each iteration
        }

Or better, use a range-based loop http://www.stroustrup.com/C++11FAQ.html#for
1
2
for( int v : converted ) std::cout << v << ' ' ;
std::cout << '\n' ;
Topic archived. No new replies allowed.