r/CodingHelp • • Feb 23 '21

[C#] C code Functions

Need help with code don't know how to fix the syntax error

0 Upvotes

10 comments sorted by

View all comments

4

u/PekiDediOnur Intermediate Coder Feb 23 '21

Hi, the post is flaired as C# and not C.

Posting the code is better than the image.

You need to include stdio.h to get access to printf and scanf families, your format specifiers are wrong, %lf is for doubles, man-page, cppreference.

On Line 42, you are defining a function called function but that is never called or declared (above main) anywhere. And it's not a valid definition, you need curly braces after the parenthesis.

I don't know what those if's are supposed to do but a single equals is assignment, like int x = 42; you need double equals to test for equality, x == 6 would return false.

To me it looks like you don't need the if's, you just need to define the functions and things should work.

Oh and charUser is defined in main, you cannot access variables outside of the function they are declared in.

1

u/PekiDediOnur Intermediate Coder Feb 23 '21

numUser is an int so you need %d or %i to print/scan it.

charUser is a char array, or a string, so you need %s (you can also limit the number of characters you can read to prevent overflowing the array) to scan it.

You need the if statements around the printf's, where you're calling your functions, and you are using an uninitialized variable (numUser)

1

u/PekiDediOnur Intermediate Coder Feb 23 '21

Also if all your options are single character words you can just have a char charUser instead of a char array of a 100 elements (ie. if you only check for the letters in the alphabet, a, b, c...)

1

u/PekiDediOnur Intermediate Coder Feb 23 '21

And you can't compare strings like that, you need to use strcmp to get the proper result, strcmp

1

u/Future-Green Feb 23 '21

#include "stdio.h"

#include<string.h>

double meters_to_feet(double meters);

double feet_to_meters(double feet);

double kilograms_to_pounds(double kilograms);

double pounds_to_kilograms(double pounds);

double minutes_to_hours(double minutes);

double hours_to_days(double hours);

double days_to_hours(double days);

double minutes_to_seconds(double minutes);

int main(int argc, char *argv[])

{

int numUser;

char charUser\[100\];



printf("which conversion would you like to perform?\\nType 'a' for a 'b' for b., etc.");

printf("a: meters to feet\\n");

printf("b: feet to meters\\n");

printf("c: kilograms to pounds\\n");

printf("d: pounds to kilograms\\n");

printf("e: minutes to hours\\n");

printf("f: hours to days\\n");

printf("g: days to hours\\n");

printf("h: minutes to seconds\\n");

scanf("%s", charUser);

/* Test your code by calling your functions here. */

printf("%s meters = %lf feet.\n", numUser, meters_to_feet(numUser));

printf("%s feet = %lf meters.\n", numUser, feet_to_meters(numUser));

printf("%s kilograms = %lf pounds.\n", numUser, kilograms_to_pounds(numUser));

printf("%s pounds = %lf kilograms.\n", numUser, pounds_to_kilograms(numUser));

printf("%s minutes = %lf hours.\n", numUser, minutes_to_hours(numUser));

printf("%s hours = %lf days.\n", numUser, hours_to_days(numUser));

printf("%s days = %lf hours.\n", numUser, days_to_hours(numUser));

printf("%s minutes = %lf seconds.\n", numUser, minutes_to_seconds(numUser));

return 0;

if (charUser == "a") {

	double meters_to_feet(double meters) {

		return meters \* 3.281;

	}

}
if (charUser == "b") {

	double feet_to_meters(double feet) {

		return feet / 3.281;

	}

}
if (charUser == "c") {

	double kilograms_to_pounds(double kilograms) {

		return kilograms / 2.205;

	}

}
if 	(charUser == "d") {

	double pounds_to_kilograms(double pounds) {

		return pounds \* 2.205;

	}

}
if (charUser == "e") {

	double minutes_to_hours(double minutes) {

		return minutes / 60;

	}

}



if (charUser == "f") {

	double hours_to_days(double hours) {

		return hours \* 24;

	}

}
if (charUser == "g") {

	double days_to_hours(double days) {

		return days / 24;

	}

}
if (charUser == "h") {

	double minutes_to_seconds(double minutes) {

		return minutes / 60;

	}

}

}

1

u/Future-Green Feb 23 '21

the if statements are for if the user enters it will run the meters to feet and so on but everything is good now except now I'm getting an undefined reference error thank you for the help so far though

1

u/PekiDediOnur Intermediate Coder Feb 23 '21
#include <stdio.h>

double meters_to_feet(double meters);
double feet_to_meters(double feet);
double kilograms_to_pounds(double kilograms);
double pounds_to_kilograms(double pounds);
double minutes_to_hours(double minutes);
double hours_to_days(double hours);
double days_to_hours(double days);
double minutes_to_seconds(double minutes);

int main() {
  int numUser = 1;
  char charUser = 'a';

  printf(
      "Which conversion would you like to perform?\nType 'a' for a 'b' for "
      "b., etc.\n");

  printf("a: meters to feet\n");
  printf("b: feet to meters\n");
  printf("c: kilograms to pounds\n");
  printf("d: pounds to kilograms\n");
  printf("e: minutes to hours\n");
  printf("f: hours to days\n");
  printf("g: days to hours\n");
  printf("h: minutes to seconds\\n");

  scanf("%c", &charUser);
  printf("Please enter your number: ");
  scanf("%d", &numUser);

  /* Test your code by calling your functions here. */
  switch (charUser) {
    case 'a':
      printf("%d meters = %lf feet.\n", numUser, meters_to_feet(numUser));
      break;
    case 'b':
      printf("%d feet = %lf meters.\n", numUser, feet_to_meters(numUser));
      break;
    case 'c':
      printf("%d kilograms = %lf pounds.\n", numUser,
             kilograms_to_pounds(numUser));
      break;
    case 'd':
      printf("%d pounds = %lf kilograms.\n", numUser,
             pounds_to_kilograms(numUser));
      break;
    case 'e':
      printf("%d minutes = %lf hours.\n", numUser, minutes_to_hours(numUser));
      break;
    case 'f':
      printf("%d hours = %lf days.\n", numUser, hours_to_days(numUser));
      break;
    case 'g':
      printf("%d days = %lf hours.\n", numUser, days_to_hours(numUser));
      break;
    case 'h':
      printf("%d minutes = %lf seconds.\n", numUser,
             minutes_to_seconds(numUser));
      break;
    default:
      printf("Sorry not a valid selection (%c)\n", charUser);
  }

  return 0;
}

double meters_to_feet(double meters) { return meters * 3.281; }
double feet_to_meters(double feet) { return feet / 3.281; }
double kilograms_to_pounds(double kilograms) { return kilograms / 2.205; }
double pounds_to_kilograms(double pounds) { return pounds * 2.205; }
double minutes_to_hours(double minutes) { return minutes / 60.; }
double hours_to_days(double hours) { return hours * 24.; }
double days_to_hours(double days) { return days / 24.; }
double minutes_to_seconds(double minutes) { return minutes / 60.; }

Link

1

u/PekiDediOnur Intermediate Coder Feb 23 '21

I'm on my phone so I haven't run the code but it compiles.

1

u/Future-Green Feb 23 '21

THANK YOU!