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?. -
-
#include <stdio.h>
-
#include <ctype.h>
-
-
float Get_Temp (float &kelvin_temp, char &conversion_type)
-
{
-
-
printf ("Welcome to the temperature conversion program\n");
-
printf ("Please enter the temperature in Kelvin degres:\n");
-
scanf ("%f", &kelvin_temp);
-
-
printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n");
-
scanf ("%c", &conversion_type);
-
-
return (kelvin_temp, conversion_type);
-
-
}
-
-
void Water_Type (float &celsius_temp)
-
{
-
if ( celsius_temp <= 0 )
-
printf ("Your water type is a solid\n");
-
else if (( celsius_temp > 0 ) && ( celsius_temp < 100 ))
-
printf ("Your water type is a liquid\n");
-
else if ( celsius_temp >= 100 )
-
printf ("Your water type is a gas\n");
-
-
return;
-
-
}
-
-
void Conversion (char conversion_type, float kelvin_temp)
-
{
-
float fahrenheit_temp, celsius_temp;
-
-
switch ( conversion_type )
-
{
-
case 'c': {
-
celsius_temp = kelvin_temp - 273;
-
Water_Type(celsius_temp);
-
printf ("Your temperature in degrees celsius is %f\n", celsius_temp);
-
}
-
break;
-
-
case 'f': {
-
celsius_temp = kelvin_temp - 273;
-
fahrenheit_temp = ((9 * celsius_temp) / 5) + 32;
-
Water_Type(celsius_temp);
-
printf ("Your temperature in degrees fahrenheit is %f\n", fahrenheit_temp);
-
}
-
break;
-
-
return;
-
}
-
}
-
-
int main ()
-
{
-
float kelvin_temp;
-
char conversion_type;
-
-
-
Get_Temp (kelvin_temp, conversion_type);
-
printf ("%c\n", conversion_type);
-
Conversion (conversion_type, kelvin_temp);
-
-
-
getchar();
-
getchar ();
-
return 0;
-
-
}
-
14 1584
Function can only return a single value: -
float Get_Temp (float &kelvin_temp, char &conversion_type)
-
{
-
-
printf ("Welcome to the temperature conversion program\n");
-
printf ("Please enter the temperature in Kelvin degres:\n");
-
scanf ("%f", &kelvin_temp);
-
-
printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n");
-
scanf ("%c", &conversion_type);
-
-
return (kelvin_temp, conversion_type);<-------------------------
-
-
}
Ahh yer, how could i over
Ahh yer, how could i over come this, cause i have to return these values back to main some how?
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: -
void GetTemp(float *kelvTemp, char *convers)
-
{
-
scanf("%f", kelvTemp);
-
scanf("%c", convers);
-
}
-
Then the changes should occur in the calling function.
I now get this error when addding the pointers:
cannot convert `float' to `float*' for argument `1' to `void Get_Temp(float*, char*)' -
-
Get_Temp (kelvin_temp, conversion_type);
-
I now get this error when addding the pointers:
cannot convert `float' to `float*' for argument `1' to `void Get_Temp(float*, char*)' -
-
Get_Temp (kelvin_temp, conversion_type);
-
You have to pass the addresses of the integers: -
Get_Temp(&kelvin_Temp, &conversion_type);
-
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
FYI-
I've changed the title of the thread to be a bit more descriptive.
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: -
-
void Get_Temp (float *kelvin_temp)
-
{
-
-
printf ("Welcome to the temperature conversion program\n");
-
printf ("Please enter the temperature in Kelvin degres:\n");
-
scanf ("%f", &kelvin_temp);
-
-
return;
-
-
}
-
-
void Get_Conversion (char *conversion_type)
-
{
-
printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n");
-
scanf ("%c", &conversion_type);
-
-
return;
-
-
}
-
-
-
void Water_Type (float &celsius_temp)
-
{
-
if ( celsius_temp <= 0 )
-
printf ("Your water type is a solid\n");
-
else if (( celsius_temp > 0 ) && ( celsius_temp < 100 ))
-
printf ("Your water type is a liquid\n");
-
else if ( celsius_temp >= 100 )
-
printf ("Your water type is a gas\n");
-
-
return;
-
-
}
-
-
void Conversion (char conversion_type, float kelvin_temp)
-
{
-
float fahrenheit_temp, celsius_temp;
-
-
switch ( conversion_type )
-
{
-
case 'c': {
-
celsius_temp = kelvin_temp - 273;
-
Water_Type(celsius_temp);
-
printf ("Your temperature in degrees celsius is %f\n", celsius_temp);
-
}
-
break;
-
-
case 'f': {
-
celsius_temp = kelvin_temp - 273;
-
fahrenheit_temp = ((9 * celsius_temp) / 5) + 32;
-
Water_Type(celsius_temp);
-
printf ("Your temperature in degrees fahrenheit is %f\n", fahrenheit_temp);
-
}
-
break;
-
-
return;
-
}
-
}
-
-
int main ()
-
{
-
float kelvin_temp;
-
char conversion_type;
-
-
-
Get_Temp (&kelvin_temp);
-
Get_Conversion (&conversion_type);
-
Conversion (conversion_type, kelvin_temp);
-
-
-
getchar();
-
getchar ();
-
return 0;
-
-
}
-
-
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: -
-
void Get_Temp (float *kelvin_temp)
-
{
-
-
printf ("Welcome to the temperature conversion program\n");
-
printf ("Please enter the temperature in Kelvin degres:\n");
-
scanf ("%f", &kelvin_temp);
-
-
return;
-
-
}
-
-
void Get_Conversion (char *conversion_type)
-
{
-
printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n");
-
scanf ("%c", &conversion_type);
-
-
return;
-
-
}
-
-
-
void Water_Type (float &celsius_temp)
-
{
-
if ( celsius_temp <= 0 )
-
printf ("Your water type is a solid\n");
-
else if (( celsius_temp > 0 ) && ( celsius_temp < 100 ))
-
printf ("Your water type is a liquid\n");
-
else if ( celsius_temp >= 100 )
-
printf ("Your water type is a gas\n");
-
-
return;
-
-
}
-
-
void Conversion (char conversion_type, float kelvin_temp)
-
{
-
float fahrenheit_temp, celsius_temp;
-
-
switch ( conversion_type )
-
{
-
case 'c': {
-
celsius_temp = kelvin_temp - 273;
-
Water_Type(celsius_temp);
-
printf ("Your temperature in degrees celsius is %f\n", celsius_temp);
-
}
-
break;
-
-
case 'f': {
-
celsius_temp = kelvin_temp - 273;
-
fahrenheit_temp = ((9 * celsius_temp) / 5) + 32;
-
Water_Type(celsius_temp);
-
printf ("Your temperature in degrees fahrenheit is %f\n", fahrenheit_temp);
-
}
-
break;
-
-
return;
-
}
-
}
-
-
int main ()
-
{
-
float kelvin_temp;
-
char conversion_type;
-
-
-
Get_Temp (&kelvin_temp);
-
Get_Conversion (&conversion_type);
-
Conversion (conversion_type, kelvin_temp);
-
-
-
getchar();
-
getchar ();
-
return 0;
-
-
}
-
-
Since you are already passing a pointer to the function, you don't need to get the address of the variable (pointer): -
scanf("%c", conversion_type);
-
Same thing for the float.
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?
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. -
-
-
void Get_Conversion (char *type)
-
{
-
printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n");
-
scanf ("%c", type);
-
-
return;
-
-
}
-
-
void Conversion (char type)
-
{
-
-
switch ( type )
-
{
-
case 'c': {
-
-
printf ("C has been selected \n");
-
}
-
break;
-
-
case 'f': {
-
-
printf ("F has been selected \n");
-
}
-
break;
-
-
return;
-
}
-
}
-
-
-
-
-
int main ()
-
{
-
float kelvin_temp;
-
char type;
-
-
-
Get_Conversion (&type);
-
printf("Your type is %c\n", type);
-
Conversion (type);
-
-
getchar();
-
getchar ();
-
return 0;
-
-
}
-
-
But when i add the gettemp function it will crash after i enter in the conversion type. -
-
void Get_Temp (float *kelvin_temp)
-
{
-
-
printf ("Welcome to the temperature conversion program\n");
-
printf ("Please enter the temperature in Kelvin degres:\n");
-
scanf ("%f", kelvin_temp);
-
-
return;
-
-
}
-
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. -
-
-
void Get_Conversion (char *type)
-
{
-
printf ("What is your conversion type:\n F for Fahrenheit \n C for Celsius\n");
-
scanf ("%c", type);
-
-
return;
-
-
}
-
-
void Conversion (char type)
-
{
-
-
switch ( type )
-
{
-
case 'c': {
-
-
printf ("C has been selected \n");
-
}
-
break;
-
-
case 'f': {
-
-
printf ("F has been selected \n");
-
}
-
break;
-
-
return;
-
}
-
}
-
-
-
-
-
int main ()
-
{
-
float kelvin_temp;
-
char type;
-
-
-
Get_Conversion (&type);
-
printf("Your type is %c\n", type);
-
Conversion (type);
-
-
getchar();
-
getchar ();
-
return 0;
-
-
}
-
-
But when i add the gettemp function it will crash after i enter in the conversion type. -
-
void Get_Temp (float *kelvin_temp)
-
{
-
-
printf ("Welcome to the temperature conversion program\n");
-
printf ("Please enter the temperature in Kelvin degres:\n");
-
scanf ("%f", kelvin_temp);
-
-
return;
-
-
}
-
What do you mean? It crashes without even calling the function? What kind of input are you giving it?
ahhh dont worry i think i got it sorted
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
by: pms |
last post by:
Need suggestions to improve this program.
using System;
namespace CalculateWage
{
class GetEmployeeDetails
{
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |