What do I do... Im so confused

Hi. I cant wrap my head around this... Its been a whole day and cant figure out how to make it show only 1 of the selected measurments. It converts CM to either metres/decimeters and milimeters, but when I select one of them it shows all 3 functions. Can anyone help?

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>
using namespace std;

void izvelne() { 
    cout << " 1. CM uz M \n";
    cout << " 2. CM uz DM \n";
    cout << " 3. CM uz MM \n";
}

double M (double a, double b);
double DM (double a, double d);
double MM (double c, double b);

int main() {
    
    izvelne();
    int tavaizvele;
    int confirm;
    double x, z, y, o;
    double sum;
    
    // Izvelne
    cout << "Izvelies no 1. - 3. \n";
    cin >> tavaizvele;
        
    switch (tavaizvele) {   
        case 1: double M(); break;
        case 2: double DM(); break;
        case 3: double MM(); break;
        default: cout << "Kļūda"; break; 
    }
    
    cout << "Ievadi centimetrus : "; cin >> z;
    
    // CM UZ METRI
    sum = M(x, z);
    cout << "1. Tas/tie ir " << sum << " metri\n";
    
    // CM UZ DECIMETRI
    sum = DM(z, o);
    cout << "2. Tas/tie ir " << sum << " decimetri\n";
    
    // CM UZ MILIMETRI
    sum = MM(y, z);
    cout << "3. Tas/tie ir " << sum << " milimetri\n";
}

// Funkcijas
double M (double a, double b) {
    double rez;
    rez = a = b/100;
    return rez;
}

double DM (double b, double d) {
    double rez;
    rez = d = b*0.1;
    return rez;
}

double MM (double c, double b) {
    double rez;
    rez = c = b*10;
    return rez;
}
You call (or invoke) a function by way of assignment:

 
      sum = M(x, z);

Right?

You select (or switch) between actions using a switch statement:

1
2
3
4
5
switch (tavaizvele) {
    case 1: cout << "I should compute and print meters here\n"; break;
    case 2: cout << "I should compute and print decimeters here\n"; break;
    case 3: cout << "I should compute and print millimeters here\n"; break;
}

A function prototype looks like these:

1
2
  double M( double, double );
  double Q();

Neither actually calls any function. It is just informative to the compiler. (And should be flagged as an error by the compiler when found inside other functions.)

In order to help you find the problems, make sure to turn the warnings all the way up on your compiler. If you are using the command-line:

    MSVC: cl /EHsc /W4 /std:c++17 ... 
    GCC:   g++ -Wall -Wextra -pedantic-errors -std=c++17 ... 
    Clang: clang++ -Wall -Wextra -pedantic-errors -std=c++17 ... 

(Use a later version of the standard if you wish, but you should be using at minimum C++17.)

If you are using an IDE go to the compiler setup and make sure that the warnings are set to the highest level. You will likely see something similar to the command-line stuff above that will help you select it.

Remember, you must combine switch with the stuff that actually does things.

Good luck!
Last edited on
I am not sure if this is what you want. You should get into the habit of using meaningful English names.
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

#include <iostream>

using namespace std;

double cm_to_meter(double a);
double cm_to_decimeter(double a);
double cm_to_millimeter(double a);

void izvelne() { 
  cout << " 1. CM uz M \n";
  cout << " 2. CM uz DM \n";
  cout << " 3. CM uz MM \n";
}

int main() 
{
	int tavaizvele = 0;
	double cm = 0.0;

	izvelne();
	cout << "Izvelies no 1. - 3. \n";

	cin >> tavaizvele;
		
	switch (tavaizvele) 
	{   
		case 1: 
			std::cout << "Centimeter to Meter: ";
			std::cin >> cm;
			std::cout << cm_to_meter(cm) << "m\n";
			break;
		case 2: 
		std::cout << "Centimeter to Decimeter: ";
			std::cin >> cm;
			std::cout << cm_to_decimeter(cm) << "decimeter\n";
			break;
		case 3: 
			std::cout << "Centimeter to Millimeter: ";
			std::cin >> cm;
			std::cout << cm_to_millimeter(cm) << "mm\n";
			break;
		default: 
			cout << "invalid choice\n"; 
			break; 
	}
}

double cm_to_meter(double a)
{
	return a / 100;
}

double cm_to_decimeter(double a)
{
	return a / 10;
}
double cm_to_millimeter(double a)
{
	return a * 10;
}
Last edited on
There is nothing wrong with using names familiar to both the student and the instructor.
A multi-character name, no matter what the language, is preferred over using single letter names such as x, y, z, o, etc.
Agreed. Only very obvious throw-aways should have names like x, y, and z. Otherwise, name things for what they represent (and name functions for what they do).

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>

template <typename T>
T lugt_ievadi( const std::string & uzvedne )
{
  T vertiba;
  std::cout << uzvedne;
  std::cin >> vertiba;
  std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  return vertiba;
}

int main()
{
    double centimetru = lugt_ievadi <double> ( "Cik centimetru? " );
    int kuras = lugt_ievadi <int> ( 
        "1. CM uz M\n"
        "2. CM uz DM\n"
        "3. CM uz MM\n"
        "Izvelies? " );
    switch (kuras)
    {
        case 1:
           ...
           break;

        case 2:
           ...
           break;

        case 3:
           ...
           break;
    }
}

(Sorry if my Latvian is off.)

EDIT fixed typo
Last edited on
This is Latvian? Google Translate (my normal "it ain't 'Murrican" translation tool) insists it is Slovenian, and refuses to do anything.

Sho'nuff, force Latvian and it works. How 'bout dat.
I’ve been known to be wrong on the interwebs. I guess we’ll see if OP responds.
You, D, wrong?!? *GASP!!!* :D

Yes, I sure would like to see the OP show back up and respond to your bit o' code. :)
@hevi - you seem to need a refresher on functions and switch(). Have a look at

https://www.learncpp.com/cpp-tutorial/introduction-to-functions/ etc
Topic archived. No new replies allowed.