473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Program Crashs When Input Is Entered

Hi have this program which keep crashing upon entering input, i think there is somthing wrong with the get_temp function returning the variables back to main?.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>  
  3. #include <ctype.h> 
  4.  
  5. float Get_Temp (float &kelvin_temp, char &conversion_type)
  6.  
  7.      printf ("Welcome to the temperature conversion program\n"); 
  8.      printf ("Please enter the temperature in Kelvin degres:\n"); 
  9.      scanf ("%f", &kelvin_temp); 
  10.  
  11.      printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n"); 
  12.      scanf ("%c", &conversion_type); 
  13.  
  14.      return (kelvin_temp, conversion_type); 
  15.  
  16. }
  17.  
  18. void Water_Type (float &celsius_temp)
  19.      if ( celsius_temp <= 0 ) 
  20.           printf ("Your water type is a solid\n"); 
  21.      else if (( celsius_temp > 0 ) && ( celsius_temp < 100 )) 
  22.           printf ("Your water type is a liquid\n"); 
  23.      else if ( celsius_temp >= 100 ) 
  24.           printf ("Your water type is a gas\n"); 
  25.  
  26.      return; 
  27.  
  28.  
  29. void Conversion (char conversion_type, float kelvin_temp) 
  30.      float fahrenheit_temp, celsius_temp; 
  31.  
  32.      switch ( conversion_type ) 
  33.             {
  34.             case 'c': {
  35.                  celsius_temp = kelvin_temp - 273; 
  36.                  Water_Type(celsius_temp); 
  37.                  printf ("Your temperature in degrees celsius is %f\n", celsius_temp); 
  38.                  }
  39.                 break; 
  40.  
  41.             case 'f': { 
  42.                  celsius_temp = kelvin_temp - 273;  
  43.                  fahrenheit_temp = ((9 * celsius_temp) / 5) + 32; 
  44.                  Water_Type(celsius_temp); 
  45.                  printf ("Your temperature in degrees fahrenheit is %f\n", fahrenheit_temp); 
  46.                 }
  47.                 break;
  48.  
  49.             return; 
  50.             } 
  51. }
  52.  
  53. int main ()  
  54.     float kelvin_temp; 
  55.     char conversion_type;
  56.  
  57.  
  58.     Get_Temp (kelvin_temp, conversion_type); 
  59.     printf ("%c\n", conversion_type);
  60.     Conversion (conversion_type, kelvin_temp); 
  61.  
  62.  
  63.     getchar();
  64.     getchar ();
  65.     return 0; 
  66.  
  67. }  
  68.  
Sep 23 '07 #1
14 1584
Savage
1,764 Expert 1GB
Function can only return a single value:

Expand|Select|Wrap|Line Numbers
  1. float Get_Temp (float &kelvin_temp, char &conversion_type)
  2. {
  3.  
  4.      printf ("Welcome to the temperature conversion program\n");
  5.      printf ("Please enter the temperature in Kelvin degres:\n");
  6.      scanf ("%f", &kelvin_temp);
  7.  
  8.      printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n");
  9.      scanf ("%c", &conversion_type);
  10.  
  11.      return (kelvin_temp, conversion_type);<-------------------------
  12.  
  13. }
Sep 23 '07 #2
Ahh yer, how could i over
Sep 23 '07 #3
Ahh yer, how could i over come this, cause i have to return these values back to main some how?
Sep 23 '07 #4
ilikepython
844 Expert 512MB
Ahh yer, how could i over come this, cause i have to return these values back to main some how?
Since this is C, you can't use pass by reference so you could use pointers:
Expand|Select|Wrap|Line Numbers
  1. void GetTemp(float *kelvTemp, char *convers)
  2. {
  3.     scanf("%f", kelvTemp);
  4.     scanf("%c", convers);
  5. }
  6.  
Then the changes should occur in the calling function.
Sep 23 '07 #5
I now get this error when addding the pointers:

cannot convert `float' to `float*' for argument `1' to `void Get_Temp(float*, char*)'

Expand|Select|Wrap|Line Numbers
  1.  
  2. Get_Temp (kelvin_temp, conversion_type);  
  3.  
Sep 24 '07 #6
ilikepython
844 Expert 512MB
I now get this error when addding the pointers:

cannot convert `float' to `float*' for argument `1' to `void Get_Temp(float*, char*)'

Expand|Select|Wrap|Line Numbers
  1.  
  2. Get_Temp (kelvin_temp, conversion_type);  
  3.  
You have to pass the addresses of the integers:
Expand|Select|Wrap|Line Numbers
  1. Get_Temp(&kelvin_Temp, &conversion_type);
  2.  
Sep 24 '07 #7
JosAH
11,448 Expert 8TB
Ahh yer, how could i over come this, cause i have to return these values back to main some how?
How about *two* little functions: one returns the temperature value and the other
one returns the calculation method. Never let one function do two disjunct things;
your mistake is a perfect example why you shouldn't do that.

kind regards,

Jos
Sep 24 '07 #8
sicarie
4,677 Expert Mod 4TB
FYI-

I've changed the title of the thread to be a bit more descriptive.
Sep 24 '07 #9
Hi ive added a new function but the program still crashes?, i am still confused as to why it might be:( This is the program code now:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void Get_Temp (float *kelvin_temp)
  3.  
  4.      printf ("Welcome to the temperature conversion program\n"); 
  5.      printf ("Please enter the temperature in Kelvin degres:\n"); 
  6.      scanf ("%f", &kelvin_temp); 
  7.  
  8.      return; 
  9.  
  10. }
  11.  
  12. void Get_Conversion (char *conversion_type) 
  13.      printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n"); 
  14.      scanf ("%c", &conversion_type); 
  15.  
  16.      return; 
  17.  
  18. }
  19.  
  20.  
  21. void Water_Type (float &celsius_temp)
  22.      if ( celsius_temp <= 0 ) 
  23.           printf ("Your water type is a solid\n"); 
  24.      else if (( celsius_temp > 0 ) && ( celsius_temp < 100 )) 
  25.           printf ("Your water type is a liquid\n"); 
  26.      else if ( celsius_temp >= 100 ) 
  27.           printf ("Your water type is a gas\n"); 
  28.  
  29.      return; 
  30.  
  31.  
  32. void Conversion (char conversion_type, float kelvin_temp) 
  33.      float fahrenheit_temp, celsius_temp; 
  34.  
  35.      switch ( conversion_type ) 
  36.             {
  37.             case 'c': {
  38.                  celsius_temp = kelvin_temp - 273; 
  39.                  Water_Type(celsius_temp); 
  40.                  printf ("Your temperature in degrees celsius is %f\n", celsius_temp); 
  41.                  }
  42.                 break; 
  43.  
  44.             case 'f': { 
  45.                  celsius_temp = kelvin_temp - 273;  
  46.                  fahrenheit_temp = ((9 * celsius_temp) / 5) + 32; 
  47.                  Water_Type(celsius_temp); 
  48.                  printf ("Your temperature in degrees fahrenheit is %f\n", fahrenheit_temp); 
  49.                 }
  50.                 break;
  51.  
  52.             return; 
  53.             } 
  54. }
  55.  
  56. int main ()  
  57.     float kelvin_temp;
  58.     char conversion_type;
  59.  
  60.  
  61.     Get_Temp (&kelvin_temp); 
  62.     Get_Conversion (&conversion_type); 
  63.     Conversion (conversion_type, kelvin_temp); 
  64.  
  65.  
  66.     getchar();
  67.     getchar ();
  68.     return 0; 
  69.  
  70. }  
  71.  
  72.  
Sep 24 '07 #10
ilikepython
844 Expert 512MB
Hi ive added a new function but the program still crashes?, i am still confused as to why it might be:( This is the program code now:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void Get_Temp (float *kelvin_temp)
  3.  
  4.      printf ("Welcome to the temperature conversion program\n"); 
  5.      printf ("Please enter the temperature in Kelvin degres:\n"); 
  6.      scanf ("%f", &kelvin_temp); 
  7.  
  8.      return; 
  9.  
  10. }
  11.  
  12. void Get_Conversion (char *conversion_type) 
  13.      printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n"); 
  14.      scanf ("%c", &conversion_type); 
  15.  
  16.      return; 
  17.  
  18. }
  19.  
  20.  
  21. void Water_Type (float &celsius_temp)
  22.      if ( celsius_temp <= 0 ) 
  23.           printf ("Your water type is a solid\n"); 
  24.      else if (( celsius_temp > 0 ) && ( celsius_temp < 100 )) 
  25.           printf ("Your water type is a liquid\n"); 
  26.      else if ( celsius_temp >= 100 ) 
  27.           printf ("Your water type is a gas\n"); 
  28.  
  29.      return; 
  30.  
  31.  
  32. void Conversion (char conversion_type, float kelvin_temp) 
  33.      float fahrenheit_temp, celsius_temp; 
  34.  
  35.      switch ( conversion_type ) 
  36.             {
  37.             case 'c': {
  38.                  celsius_temp = kelvin_temp - 273; 
  39.                  Water_Type(celsius_temp); 
  40.                  printf ("Your temperature in degrees celsius is %f\n", celsius_temp); 
  41.                  }
  42.                 break; 
  43.  
  44.             case 'f': { 
  45.                  celsius_temp = kelvin_temp - 273;  
  46.                  fahrenheit_temp = ((9 * celsius_temp) / 5) + 32; 
  47.                  Water_Type(celsius_temp); 
  48.                  printf ("Your temperature in degrees fahrenheit is %f\n", fahrenheit_temp); 
  49.                 }
  50.                 break;
  51.  
  52.             return; 
  53.             } 
  54. }
  55.  
  56. int main ()  
  57.     float kelvin_temp;
  58.     char conversion_type;
  59.  
  60.  
  61.     Get_Temp (&kelvin_temp); 
  62.     Get_Conversion (&conversion_type); 
  63.     Conversion (conversion_type, kelvin_temp); 
  64.  
  65.  
  66.     getchar();
  67.     getchar ();
  68.     return 0; 
  69.  
  70. }  
  71.  
  72.  
Since you are already passing a pointer to the function, you don't need to get the address of the variable (pointer):
Expand|Select|Wrap|Line Numbers
  1. scanf("%c", conversion_type);
  2.  
Same thing for the float.
Sep 24 '07 #11
Ok i still removed the & from scanf funcitons, i get the program to compile but when after i enter the temp and then enter the conversion type it still crashes then?
Sep 25 '07 #12
im not sure y but i think it as somthing to do with the inputs
E.g This works when i just get the conversion type.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. void Get_Conversion (char *type) 
  4.      printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n"); 
  5.      scanf ("%c", type); 
  6.  
  7.      return; 
  8.  
  9. }
  10.  
  11. void Conversion (char type) 
  12.  
  13.      switch ( type ) 
  14.             {
  15.             case 'c': {
  16.  
  17.                  printf ("C has been selected \n"); 
  18.                  }
  19.                 break; 
  20.  
  21.             case 'f': { 
  22.  
  23.                  printf ("F has been selected \n"); 
  24.                 }
  25.                 break;
  26.  
  27.             return; 
  28.             } 
  29. }
  30.  
  31.  
  32.  
  33.  
  34. int main ()  
  35. {  
  36.     float kelvin_temp;
  37.     char type;
  38.  
  39.  
  40.     Get_Conversion (&type); 
  41.     printf("Your type is %c\n", type); 
  42.     Conversion (type);
  43.  
  44.     getchar();
  45.     getchar ();
  46.     return 0; 
  47.  
  48. }  
  49.  
  50.  
But when i add the gettemp function it will crash after i enter in the conversion type.

Expand|Select|Wrap|Line Numbers
  1.  
  2. void Get_Temp (float *kelvin_temp)
  3.  
  4.      printf ("Welcome to the temperature conversion program\n"); 
  5.      printf ("Please enter the temperature in Kelvin degres:\n"); 
  6.      scanf ("%f", kelvin_temp); 
  7.  
  8.      return; 
  9.  
  10.  
Sep 25 '07 #13
ilikepython
844 Expert 512MB
im not sure y but i think it as somthing to do with the inputs
E.g This works when i just get the conversion type.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. void Get_Conversion (char *type) 
  4.      printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n"); 
  5.      scanf ("%c", type); 
  6.  
  7.      return; 
  8.  
  9. }
  10.  
  11. void Conversion (char type) 
  12.  
  13.      switch ( type ) 
  14.             {
  15.             case 'c': {
  16.  
  17.                  printf ("C has been selected \n"); 
  18.                  }
  19.                 break; 
  20.  
  21.             case 'f': { 
  22.  
  23.                  printf ("F has been selected \n"); 
  24.                 }
  25.                 break;
  26.  
  27.             return; 
  28.             } 
  29. }
  30.  
  31.  
  32.  
  33.  
  34. int main ()  
  35. {  
  36.     float kelvin_temp;
  37.     char type;
  38.  
  39.  
  40.     Get_Conversion (&type); 
  41.     printf("Your type is %c\n", type); 
  42.     Conversion (type);
  43.  
  44.     getchar();
  45.     getchar ();
  46.     return 0; 
  47.  
  48. }  
  49.  
  50.  
But when i add the gettemp function it will crash after i enter in the conversion type.

Expand|Select|Wrap|Line Numbers
  1.  
  2. void Get_Temp (float *kelvin_temp)
  3.  
  4.      printf ("Welcome to the temperature conversion program\n"); 
  5.      printf ("Please enter the temperature in Kelvin degres:\n"); 
  6.      scanf ("%f", kelvin_temp); 
  7.  
  8.      return; 
  9.  
  10.  
What do you mean? It crashes without even calling the function? What kind of input are you giving it?
Sep 25 '07 #14
ahhh dont worry i think i got it sorted
Sep 25 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Just starting out | last post by:
I am very new to C code and I'm having a lot of trouble with a homework assignment. This program is supposed to take the amount of Euros that the user enters and convert it to US dollars. It...
1
by: sunnyhanda | last post by:
Write a program that will generate, but not display, a three-digit "target" number that has three distinct digits. Then, inputs a maximum of eight user guesses and, for each guess, outputs the number...
0
by: georges the man | last post by:
The purpose: • Sorting and Searching • Numerical Analysis Design Specification You are to write a program called “StockAnalyser”. Your program will read a text file that contains historical...
4
by: pms | last post by:
Need suggestions to improve this program. using System; namespace CalculateWage { class GetEmployeeDetails {
3
by: powerej | last post by:
writing a program that asks how many numbers the users want to enter, then input an integer for the amount of numbers said, output: number of integers entered, sum of them, average of them, maximum...
1
by: haelly | last post by:
write a program that prompts the user to enter three different positive integer values.If the values are not different, the program prints a message"equal value" and terminates(hint:use the return...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
11
by: icarus | last post by:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to Kelvin). Using gcc 4.0. The user is supposed to enter q or any other non-character to exit the program. Problem with this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.