![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
problem with standard function
Im doing a simple Convertion program. It converts from Fahrenheit to Celsius and Visa Versa. My problem involves the scanf Function. Im really curious why it happens; the function has trouble handling Char types.
for example, code exerpt: char choice;
printf("\n\t--If converting from Celsius to Fahrenheit, please press 'C'-");
printf("\n\t--If converting from Fahrenheit to Celsius, please press 'F'-\n");
printf("\n\tChoice:");
scanf("%c", &choice);but strangely, the code below works, and its almost exactly the same ( the only difference is that the %c is replaced with %s to handle strings, but 'choice' is not a string! char choice;
printf("\n\t--If converting from Celsius to Fahrenheit, please press 'C'-");
printf("\n\t--If converting from Fahrenheit to Celsius, please press 'F'-\n");
printf("\n\tChoice:");
scanf("%s", &choice);Im guessing it has something to do with how the function was written, but i cant find the actual code on stdio.h or stdlib.h . Can someone please tell me why this happens? any help will be appreciated, thanks. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
Here is a suggestion, post the rest of the code, because the input stream might be catching an extra enter, and it is hard to tell with the code we have. Also, the scanf with the %s should NOT have an ampersand.
|
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 579
Rep Power: 5
![]() |
Your first piece of code should work...Can you post your entire program?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#4 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#5 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 579
Rep Power: 5
![]() |
You don't have to use the & because it alrady holds an address no?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Sort of correct, Benoit. The name of a char array is not the same as it's address, but is treated by the compiler in some contexts as if it is
|
|
|
|
|
|
#8 |
|
Newbie
|
Here is the entire code, as nindoja said, i think it has something to do
with the input stream. #include <stdio.h>
#include <stdlib.h>
int main()
{
float cels, farh, var1;
char choice;
// Intro
printf("\t\tThis is a program that will convert\n");
printf("\t\ta temperature from Fahrenheit to Celsius or\n");
printf("\t\tfrom Celsius to Fahrenheit.\n\n\n");
//
printf("\t\tPlease Select a Temperature: ");
scanf("%f", &var1);
cels = (5.0/9.0)*(var1 - 32);
farh = (9.0/5.0)*(var1 + 32);
printf("\n\t--If converting from Celsius to Fahrenheit, please press 'C'--");
printf("\n\t--If converting from Fahrenheit to Celsius, please press 'F'--\n");
printf("\n\tChoice:");
scanf("%s", &choice);
if(choice == 'C' || choice == 'c')
{
printf("\n\t%5.2f degrees Celsius is converted to %5.2f degrees Fahrenheit\n", var1, farh);
system("pause");
}
if(choice == 'F' || choice == 'f')
{
printf("\n\t%5.2f degrees Fahrenheit is converted to %5.2f degrees Celsius\n", var1, cels);
system("pause");
}
system("pause");
return 0;
} |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
This works in terms of I/O and getting variables read in correctly.
The formulas are implemented incorrectly - you get wrong answers - you can fix that. I added a switch() -by "standard" convention whenever you have three or more condtions (choice = C or c or F or c and choice not = "F,f,C,c" ---> five tests) you employ a switch(). #include <stdio.h>
#include <stdlib.h>
int main()
{
double cels=0.,
farh=0.,
var1=0.;
char choice[10]={0x0};
printf("\t\tThis is a program that will convert\n");
printf("\t\ta temperature from Fahrenheit to Celsius or\n");
printf("\t\tfrom Celsius to Fahrenheit.\n\n\n");
printf("\t\tPlease Select a Temperature: ");
scanf("%s", choice);
var1=atof(choice);
/* the math here is WRONG */
cels = ((5.0/9.0)*var1) - 32.;
farh = ((9.0/5.0)*var1) + 32.;
printf("\n\t--If converting from Celsius to Fahrenheit, please press 'C'<return>--\n");
printf("\n\t--If converting from Fahrenheit to Celsius, please press 'F'<return>--\n");
printf("\n\tChoice:\n");
scanf("%s", &choice);
switch(choice[0])
{
case 'C':
case 'c':
printf("\n\t%5.2f degrees Celsius is converted to %5.2f degrees Fahrenheit\n",
var1, farh);
break;
case 'F':
case 'f':
printf("\n\t%5.2f degrees Fahrenheit is converted to %5.2f degrees Celsius\n",
var1, cels);
default :
break;
}
system("pause");
return 0;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|