Program 18

Pages: 12
Now I moved on to the next exercise, which is as follows:

Now change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Each time through the loop write out the value entered. If it`s the smallest so far, write the smallest so far after the number. If it`s the largest so far, write the largest so far after the number.

Now I`m having hard time understanding how to define two variables to keep track of the values, so why not to store all the values in a vector and then decide if the value entered are smaller/larger than the previous ones. Something along these lines?

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

namespace program {

int main()

{

   
   double a=0.0;
   
   
   
   
cout<<"Enter a double value";

vector<double>v= {a};
for(double value;cin>>a;)
sort(words);                                   // Sort the words 

for (double a=0.0;++a)
if (double[a-1]<double a)              // Is the value larger than the previous ones?

cout>>" "<<a<<" is the largest so far";


....

 
vectors are overkill here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int smaller;
int larger;
int input;

while(/*getting input*/)
{

if(//input greater than larger)
{
   larger = input;
   cout << input <<" is the largest so far" << endl;
}
else if(//input smaller...)
{//...
}
else if(//input smaller than larger AND input greater than smaller)
{
cout << input << endl;
}

}//end while 
Last edited on
Now it seems readable but does not compile:

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

#include <iostream>
#include <cmath> 
#include <cstdio>


namespace program {

int main() 
{

double smaller=0.0;
double larger=0.0;
double input=0.0;

cout<< "Enter a floating point value";

while(cin>>input;)
{

    if(input>larger;) {

         larger = input;
         cout << input <<" is the largest so far" << endl;
      }    
         
    else if(input<smaller) {
       
          cout << input <<" is the smallest so far" << endl;
       }


    else if(input < larger and input > smaller) {
 
            cout << input << endl;
     }
         
   }

}  

1) main() can not be a part of namespace. It shall be global function. Delete namespace from your program
2) line 18, 21: extra unneeded semicolons
3) cout, cin, endl should be qualified: std::cout, etc.
4) You forgot to save current smallest number (like you did with largest)
5) no need to chain ifs. First entered value would be both largest and smallest
6) if you will enter onlty negative number, larger will never be set. If you enter only positives, smaller will never be set. You need to assign smallest possible and largest possible values to larger and smaller respecttively.
You can do this by using implementation defined behavior which is true for all x86 PC:
1
2
double smaller = 1 / 0.0;
double larger = -1 / 0.0;

Alternatively you can use <limits> header:
1
2
double smaller = std::numeric_limits<double>::infinity();
double larger = -smaller;
Nice, now I got it to work and I moved on to the next exercise.

Add a unit to each double entered; that is, enter values such as 10cm, 2.5in, 5ft or 3.33m. Accept four units:

cm, m, in, ft. Assume conversion factors 1m == 100cm, 1in == 2.54cm, 1ft == 12in. Read the unit indicator

into a string . You may consider 12m (with a space between the number and the unit) equilavent to 12m

(without the space). Reject values withot units or with illegal representations of units, such as y, yard,meter,

km and gallons.

Here is the code I have so far:

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 <cstdio>



int main() 
{

double a=0.0;
double b=0.0;
double smaller=1/0.0;
double larger=-1/0.0;
double input=0.0;
string unit;
string smallest_unit;
string largest_unit;
bool unit;

bool unit= true;

if (unit== cm or m or in or ft)

else 

bool unit = !true;



std::cout<< "Enter a floating point value followed by a unit(cm,m,in,ft)";

while(std::cin>>input>>string unit) {


if (unit=="m")				         //convert meters to centimeters
			a=b*100;
					
		else if (strcmp(...) == 0)		//convert inches to centimiters
			a=b*2.54;		
		
		else if (strcmp(...) == 0)		//convert feet to centimeters
			a=b*30.48;
					
		else if (strcmp(...) == 0) 
			a=b;




    if(input>larger) {

         larger = input;
         std::cout << input << unit" is the largest so far" << std::endl;
      }    
         
    else if(input<smaller) {
       
         std::cout << input << unit" is the smallest so far" << std::endl;
       }


    else if(input < larger and input > smaller) {
 
            std::cout << input unit << std::endl;
     }
         
   }

}
  

Here is the code I have so far
But it doesn't even compile. I think you'd be better asking more specific questions to be honest.

This is bit a wacky too:
1
2
double smaller=1/0.0;
double larger=-1/0.0;

dividing something by zero is bad.

and this:
bool unit = !true;
surely
bool unit = false;

is clearer?

and this test:
if (unit=="m")
.. you've declared 'unit' as boolean.

It's all very confusing.
Last edited on
mutexe wrote:
This is bit a wacky too:

1
2
double smaller=1/0.0;
double larger=-1/0.0;


dividing something by zero is bad.
It is defined for any implementation using IEEE 754 standard (which is pretty much any implementation on PC)
Also I provided alternative way to do it.

@pacman169 my previous points 4 and 5 still not adressed. And you get a bunch of errors in the new code. Better to finalize current project before moving on.

Your code looks like you just copied pseudocode from somwhere and do not bothered to actually implement it.
" My previous points 4 and 5 still not adressed."

I don`t really understand them but anyway the purpose of the program is to accept only four unit`s that`s why I used the boolean statement.

Even if the code contains a few errors why can`t it be fixed a step by step?

I want to compare strings with http://www.cplusplus.com/reference/cstring/strcmp/

like if (unit=="m") using strcmp should it be something like

(strcmp ((unit,m) = 0) to know if unit is in meters?
You wrote:
1
2
3
string unit;
bool unit;
bool unit= true;

What is the "unit"?

Your wrote:
if (unit== cm or m or in or ft)
Some compiler extension uses "or" as shorthand for "||", so you wrote
if ( unit == cm || m || in || ft )
Which, considering the operator precedence is same as:
if ( (unit == cm) || m || in || ft )
In other words, this condition is true if
units==cm
OR
m is true
OR
in is true
OR
ft is true

However, you have not declared identifiers cm, m, in, ft nor given them any value, so the compiler simply does not know what it should do.

No, you do not want to use strcmp. You do have std::string, and that has operator==. The if (unit=="m") is perfectly correct syntax IF unit is a std::string.
Last edited on
Some compiler extension uses "or" as shorthand for "||",
It is actually part of Standard: http://en.cppreference.com/w/cpp/language/operator_alternative

@pacman169
You have: multiple redefinition of unit: as bool and as string. Remove everything related to bool one, it does not do useful anyway (Lines 19-29)

b is never set so any multiplication of it is 0 anyway. Also a is not used after that too.

Smaller input is not saved: any number you enter would be "smallest".
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

#include <iostream>
#include <cmath> 
#include <cstdio>



int main() 
{

double a=0.0;
double smaller=1/0.0;
double larger=-1/0.0;
double input=0.0;
string unit;
unit= " ";






std::cout<< "Enter a floating point value followed by a unit(cm,m,in,ft)";

while(std::cin>>input>>unit) {

if ( <<"unit"<< != cm || m || in || ft)

{ cout>>" Not a unit I know!"'\n' ; }


while (std::cin>>unit) {				         
		
                if (unit == m) cout<< "<< a <<" * 100 cm;
					
		else if (unit == in)		
			cout<< "<< a <<" * 2.54 cm;		
		
		else if (unit == ft)		
			cout<< "<< a <<" * 30.48 cm;
					
		
     }



Something like this first, I`m not sure how to continue though...
How about asking your compiler whether it has any comments about your syntax?
Line 27 should be if ( <<"unit"<< != "cm" || "m" || "in" || "ft")
Operator precedence:
1
2
3
4
5
if ( a != b || c )

// is same as
const bool D = (a != b);
if ( D || c )

You seem to mean:
if ( a != b || a != c )
Last edited on
Yes, I think these lines does not still compile:

1
2
3
4
5

if ( "unit" != "cm" || "unit" != "m"  "unit" != "in" || "unit"!= "ft")

{ std::cout<<" Not a unit I know!" '\n' ; }


However I`m not particularly interested of the whole line, because I think I don`t need it. Something like

1
2
3
4
5
6
7
8
9
10
11

if (unit == m) cout<< "<< a <<" * 100 cm;
					
		else if (unit == in)		
			cout<< "<< a <<" * 2.54 cm;		
		
		else if (unit == ft)		
			cout<< "<< a <<" * 30.48 cm;

               else  std::cout<<" Not a unit I know!" '\n' ;


As far as the syntax is sufficient...
Last edited on
Should be something like

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

int main() 
{

double a=0.0;
double smaller=1/0.0;
double larger=-1/0.0;
double input=0.0;
std::string unit;
unit= " ";



std::cout<< "Enter a floating point value followed by a unit(cm,m,in,ft)";



while (std::cin>>input>>unit) {				         
		
                if (unit == "m") std::cout<< "<< a * 100 <<" cm\n";
					
		else if (unit == "in")		
			std::cout<< "<< a * 2.54 <<" cm\n";		
		
		else if (unit == "ft")		
			std::cout<< "<< a * 30.48 <<"  cm\n";
        
              else  std::cout<<" Not a unit I know!" '\n' ;

					
		
     }


}
 


but does not compile..
Last edited on
If it does not compile, then the compiler must tell the file, line, and the error it thinks there to be.

What does the compiler say?
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

Documents/Program14.cpp:26:17: error: stray ‘\’ in program
                 if (unit == "m") std::cout<< "<< a * 100 <<" cm\n";
                 ^
Documents/Program14.cpp:26:66: warning: missing terminating " character [enabled by default]
                 if (unit == "m") std::cout<< "<< a * 100 <<" cm\n";
                                                                  ^
Documents/Program14.cpp:26:17: error: missing terminating " character
                 if (unit == "m") std::cout<< "<< a * 100 <<" cm\n";
                 ^
Documents/Program14.cpp:29:4: error: stray ‘\’ in program
    std::cout<< "<< a * 2.54 <<" cm\n";  
    ^
Documents/Program14.cpp:29:37: warning: missing terminating " character [enabled by default]
    std::cout<< "<< a * 2.54 <<" cm\n";  
                                     ^
Documents/Program14.cpp:29:4: error: missing terminating " character
    std::cout<< "<< a * 2.54 <<" cm\n";  
    ^
Documents/Program14.cpp:32:4: error: stray ‘\’ in program
    std::cout<< "<< a * 30.48 <<"  cm\n";
    ^
Documents/Program14.cpp:32:39: warning: missing terminating " character [enabled by default]
    std::cout<< "<< a * 30.48 <<"  cm\n";
                                       ^
Documents/Program14.cpp:32:4: error: missing terminating " character
    std::cout<< "<< a * 30.48 <<"  cm\n";
    ^
Documents/Program14.cpp: In function ‘int main()’:
Documents/Program14.cpp:26:62: error: expected ‘;’ before ‘cm’
                 if (unit == "m") std::cout<< "<< a * 100 <<" cm\n";
                                                              ^
You do have doublequotes (" ) in odd places. The syntax highlight by the code tags should give you a hint.

You have single statement in each if, but you could use braces. See http://www.cplusplus.com/doc/tutorial/control/

Another issue: you read into input, but use a. Why?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

while (std::cin>>a>>unit) {				         
		
                if (unit == "m")
                          std::cout<< a * 100 cm;
					
		else if (unit == "in")		
			 std::cout<< a * 2.54  cm;
		
		if      (unit == "ft")		
			 std::cout<<  a * 30.48 cm;   

                else  std::cout<< "Not a unit I know!"; 

					
		
     }


Pages: 12