keep getting error

keep getting an error, not sure what im doing wrong. I'm running it on visual studio Microsoft. The main errors I'm getting is "argument of type "const char *" is incompatible with parameter of type "char *"

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 <stdio.h>

//define constants

#define LITRES_PER_GALLON 3.785
#define KM_PER_MILE 1.609

//prototypes

float getInput(char*, float, float);
void main()
{

	//declare variables

	float distance, price, efficiency, cost;

	//GET user input

	distance = getInput("Enter the distance in miles: ", 0, 99999);
	price = getInput("Enter the price of gas in $ per gallon: ", 0, 99999);
	efficiency = getInput("Enter the efficiency rating in miles per gallon: ", 0, 99999);

	//CALCULATE the cost based on the user data
	cost = (distance / efficiency) * price * (LITRES_PER_GALLON / KM_PER_MILE);

	//DISPLAY calculated cost
	printf("The cost of the trip is $%.2f\n", cost);

	//WAIT for user confirmation before ending program
	system("pause");
}

float getInput(char* prompt, float lowerLim, float upperLim)
{
	float input;
	int check = 0;

	while (check == 0)
	{

		//PROMPT the user
		printf("%s", prompt);

		//READ the input value
		scanf_s("%f", &input);

		//CHECK the input value if
		if (input < lowerLim || input > upperLim)
		{
			printf("Error: Input is out of range. Please enter a value between %.2f and %.2f\n", lowerLim, upperLim);
		}
		else
		{

			//modify data check variable to indicate value is OK
			check = 1;
		}
	}
	return input;
}
You have more errors than just that.

Line 12: Return type of main MUST be int.

Line 32: System() requires the <cstdlib> header.

Is this meant to be C or C++?
If C++, line 2 should be <cstdio>

Lines 21,22,23: You've declared the first argument of getInput() to be char *.
Your're attempting to pass quoted literals. Quoted liters are be definition const char *. You can't pass a const char * to a char * argument. Change getInput() to accept a const char * as it's first argument.

Line 26: You have an implicit from double to float.
As AbstractionAnon points out your defines are creating two double type numbers, not floats. You can easily smash that implicit conversion warning by defining as floats.

Using system() is not considered good practice.
https://stackoverflow.com/questions/509743/is-it-bad-practice-to-use-the-system-function-when-library-functions-could-be

MSVC has a custom function, _getch()
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=msvc-170

Your C code rewritten, works with VS 2022 Community:
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 <stdio.h>
#include <stdlib.h>	// for system()

#include <conio.h>	// for getch()

//define constants
#define LITRES_PER_GALLON 3.785f
#define KM_PER_MILE       1.609f

//prototypes
float getInput(const char*, float, float);

int main()
{
   //declare variables
   float distance, price, efficiency, cost;

   //GET user input

   distance   = getInput("Enter the distance in miles: ", 0, 99999);
   price      = getInput("Enter the price of gas in $ per gallon: ", 0, 99999);
   efficiency = getInput("Enter the efficiency rating in miles per gallon: ", 0, 99999);

   //CALCULATE the cost based on the user data
   cost = (distance / efficiency) * price * (LITRES_PER_GALLON / KM_PER_MILE);

   //DISPLAY calculated cost
   printf("The cost of the trip is $%.2f\n", cost);

   // WAIT for user confirmation before ending program
   // system("pause");

   _getch();
}

float getInput(const char* prompt, float lowerLim, float upperLim)
{
   float input;
   int   check = 0;

   while ( check == 0 )
   {
      //PROMPT the user
      printf("%s", prompt);

      //READ the input value
      scanf_s("%f", &input);

      //CHECK the input value if
      if ( input < lowerLim || input > upperLim )
      {
         printf("Error: Input is out of range. Please enter a value between %.2f and %.2f\n", lowerLim, upperLim);
      }
      else
      {
         //modify data check variable to indicate value is OK
         check = 1;
      }
   }
   return input;
}
Enter the distance in miles: 125
Enter the price of gas in $ per gallon: 3.75
Enter the efficiency rating in miles per gallon: 22
The cost of the trip is $50.12
@elon dusk

the scanf function return a value, one should always check this value to see that the scanf function worked, otherwise there is considerable risk of having garbage data.

I hope this helps :+)
distance, price, efficiency and cost should be const and initialised when defined. Without dealing with non-numeric input, then perhaps:

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 <stdio.h>
#include <conio.h>	// for getch()

//define constants
#define LITRES_PER_GALLON 3.785f
#define KM_PER_MILE       1.609f

//prototypes
float getInput(const char*, float, float);

int main() {
    //GET user input
    const float distance = getInput("Enter the distance in miles: ", 0, 99999);
    const float price = getInput("Enter the price of gas in $ per gallon: ", 0, 99999);
    const float efficiency = getInput("Enter the efficiency rating in miles per gallon: ", 0, 99999);

    //CALCULATE the cost based on the user data
    const float cost = price * (LITRES_PER_GALLON / KM_PER_MILE) * (distance / efficiency);

    //DISPLAY calculated cost
    printf("The cost of the trip is $%.2f\n", cost);
    printf("Press a key to continue:");

    // WAIT for user confirmation before ending program
    _getch();
}

float getInput(const char* prompt, float lowerLim, float upperLim) {
    float input = 0;

    for (int check = 0; check == 0; ) {
       //PROMPT the user
        printf("%s", prompt);

        //READ the input value
        scanf_s("%f", &input);
        //CHECK the input value if
        if (input < lowerLim || input > upperLim)
            printf("Error: Input is out of range. Please enter a value between %.2f and %.2f\n", lowerLim, upperLim);
        else
            //modify data check variable to indicate value is OK
            check = 1;
    }

    return input;
}

Hello there. Nice little functionnal code...
However you get a warning using _getch() -> 6031: return value ignored (_getch()).
Not a big problem, but if you like clean code, you should add (void). It specifies that the function does not wait any return.

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
#include <stdio.h>
#include <conio.h>

#define LITRES_PER_GALLON 3.785f
#define KM_PER_MILE       1.609f

float getInput(const char*, float, float);

int main() {
    const float distance = getInput("Enter the distance in miles: ", 0, 99999);
    const float price = getInput("Enter the price of gas in $ per gallon: ", 0, 99999);
    const float efficiency = getInput("Enter the efficiency rating in miles per gallon: ", 0, 99999);
    
    const float cost = price * (LITRES_PER_GALLON / KM_PER_MILE) * (distance / efficiency);
  
    printf("The cost of the trip is $%.2f\n", cost);
    printf("Press a key to continue:");
   
    (void)_getch(); // <- to avoid warning 6031

    return 0;
}

float getInput(const char* prompt, float lowerLim, float upperLim) {

    float input = 0;

    for (int check = 0; check == 0; ) 
    {
        printf("%s", prompt);
        scanf_s("%f", &input);

        if (input < lowerLim || input > upperLim)
            printf("Error: Input is out of range. Please enter a value between %.2f and %.2f\n", lowerLim, upperLim);
        else
            check = 1;
    }

    return input;
}
Last edited on
Compiles clean with VS2022 when compiling as c as C17 with level 4 warnings...
Yes, that is a warning, "return value ignored", that IMO can be ignored. Since you don't care what the character retrieved is. Casting a return value on this 'throw away use' is a bit of over-kill IMO. YMMV.

If that warning is so annoying use <iostream> functionality to pause program execution. You could even create a Program Pauser Toolkit encapsulated in a custom namespace and C++ class (admittedly a bit of 'elephant gun to kill a gnat):
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
/* user created functions and classes for flushing the input stream and pausing the program
 *
 * version 1.0 (not likely to change or be modularized)
 *
 * C++ Header: program_pauser_toolkit.hpp */

#ifndef PROGRAM_PAUSER_TOOLKIT
#define PROGRAM_PAUSER_TOLLKIT

#include <iostream>
#include <limits>

inline void _pause()
{
   std::cout << "\nPress ENTER to continue...";

   // clear any previous errors in the input stream
   std::cin.clear();

   // synchronize the input buffer stream
   std::cin.sync();

   // extract and discard the max number of characters
   // until an endline character is reached
   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

class ProgramPauser
{
public:
     ProgramPauser() { }
    ~ProgramPauser() { _Pause(); }

public:
   inline void _Pause() const;
};

using PP = ProgramPauser;

inline void ProgramPauser::_Pause() const
{
   _pause();
}

#endif 

Darn, where did I see this before?

https://github.com/GeorgePimpleton/misc_files/tree/main/Program%20Pauser%20Toolkit

(Oh, yeah, ^^^^^ there)
> inline void _pause() { ...

some identifiers are reserved for use by C++ implementations and shall not be used otherwise; no diagnostic is required.
...
Each identifier that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
https://eel.is/c++draft/lex.name#3.2
Oh, darn, ya caught me. I was being lazy, knowingly violating a suggested guideline when I created the toolkit.

*BUSTED!*

That toolkit was created for my own personal use, and for experimentation. It was never meant to be even remotely production code quality.

With that all said, the point made is well taken. :)

Not that I will bother making it more "code correctness friendly." I put it up on github "as is," no suitability warranty provided.
It's actually funny, since you use "underscore + lowercase" outside the class, which is reserved in the global scope, and "underscore + uppercase" inside the class, which is reserved everywhere.
Weirdly enough I checked my module interface file of the toolkit and it doesn't have the underscore. *shrug*

I decided to update the pre-C++ header file to be more like the module interface, removing the underscore. Inside and outside the class.

As I said, I was lazy with the header. Though not with the module interface. Go figure.

Consistency is the hobgoblin of small minds, etc. And lazy/tired self-taught programming hobbyists who need more coffee.
> violating a suggested guideline

It is a rule; not a mere guideline ("shall not be used" as per the standard).

If the programmer uses such identifiers, the behavior is undefined.
https://en.cppreference.com/w/cpp/language/identifiers
Topic archived. No new replies allowed.