Program 18

Pages: 12
That is better, but what does the compiler say about the cm?
Does not work:

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

int main() 
{

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



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


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

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

					
		
     }


}
  


Compilers do not say: "Does not work". They give (more or less detailed) error messages.

We cannot possibly know what you mean by "does not work". It could be anything. You have to tell the details.


What are you trying to achieve on line 21?
If the user enters 1m then there should be 100cm, or that`s how I understood it. Now there is only 100. For the original exercise:

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.

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 12 m (with a space between the number and the unit) equivalent to 12m (without a space).

Reject values without units or with illegal representations of units, such as y, yard, meter, km and gallons.
Let's change the format of the code slightly to reflect what is actually happening:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
                if (unit == "m")  
                {
                    std::cout<< a * 100;
                    "cm";
                }
               
					
		else if (unit == "in")
                {
                    std::cout<< a * 2.54;
                    "cm";
                }
          		
		else if      (unit == "ft")
               {		
                    std::cout<<  a * 30.48; 
                    "cm"; 
               }  

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


Note that the statement "cm"; is essentially a no-op.

Perhaps you meant: std::cout << a*30.48 << "cm"; ?
No it`s working but I don`t know how to continue implementing the second part
I'm confused. You say both "does not work" and "it's working". Both cannot be true simultaneously.


Second part:
Define two variables to keep track of which is the smallest and which is the largest value you have seen so far.

Didn't you already had that in http://www.cplusplus.com/forum/beginner/142965/#msg754796
Last edited on
Sorry, I meant now it`s working. I would write:

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

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


int main() 
{

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



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


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

                else  std::cout<< "Not a unit I know!"; 
              
					
    if(a>larger) {

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


    else if(a < larger && a > smaller) {
 
            std::cout << a << "unit" << std::endl;
     }
         
   }
}





Getting strange error:
1
2
3
4
5
6
7
8

In function ‘int main()’:
Documents/Program14.cpp:46:35: error: expected ‘;’ before '\x9'
          std::cout << a << "unit" '\t' "is the largest so far" << std::endl;
                                   ^
Documents/Program14.cpp:52:35: error: expected ‘;’ before '\x9'
          std::cout << a << "unit" '\t' "is the smallest so far" << std::endl;
Last edited on
It is not strange at all. The error message even points to the singlequote character before the \t.

If you want to print separate string literals, then do so:
std::cout << "unit" << '\t' << "is the largest so far"

If you want to print one string literal, then do so:
std::cout << "unit\tis the largest so far"
Ok, my current program behaves like this:

Enter a floating point value followed by a unit(cm,m,in,ft)

3.4in
8.636cm3.4unit is the largest so far



I don`t know if it is what the writer intended but it doesn`t seem right...
Last edited on
Hello guys

now I decided to move to the next exercise:

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.

Before connecting the above to my current program, I would write a prototype like this:

double a; // Random number
int b= number of values entered
double i= sum of units;
string sum of units;
sum of units= "Meters";

while (cin>>a) {

for (double a=0; a<i; ++a) // Increment a up to sum which is i

for (int x=0; x<b ; ++x)

// x = value entered (1)

My problem is that I don`t know how to initialize x: How to assign x to number of values entered.

Thanks
Topic archived. No new replies allowed.
Pages: 12