Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 25th, 2005, 8:59 PM   #1
sparda
Newbie
 
Join Date: Apr 2005
Location: US
Posts: 15
Rep Power: 0 sparda is on a distinguished road
Send a message via Yahoo to sparda
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);
The Code above does not work, i mean it compiles, but it doesnt store a value at '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 using Dev C++, and consider myself an intermidiate C programmer, so please dont tell me to go learn something easier.
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.
sparda is offline   Reply With Quote
Old Oct 25th, 2005, 11:44 PM   #2
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
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.
nindoja is offline   Reply With Quote
Old Oct 25th, 2005, 11:53 PM   #3
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 579
Rep Power: 5 Benoit is on a distinguished road
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
Benoit is offline   Reply With Quote
Old Oct 26th, 2005, 12:45 AM   #4
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
Quote:
Originally Posted by nindoja
Also, the scanf with the %s should NOT have an ampersand.
Technically it should, because the variable being stored in is still a char so you need to pass its pointer (However, I do not recommend leaving the variable as a char when using %s).
Kaja Fumei is offline   Reply With Quote
Old Oct 26th, 2005, 7:48 AM   #5
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Kaja Fumei
Technically it should, because the variable being stored in is still a char so you need to pass its pointer (However, I do not recommend leaving the variable as a char when using %s).
When using a char array, you always use its address. Using an ampersant is in many compilers still correct, because they remove it - it's a common mistake.
Polyphemus_ is offline   Reply With Quote
Old Oct 26th, 2005, 7:49 AM   #6
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 579
Rep Power: 5 Benoit is on a distinguished road
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
Benoit is offline   Reply With Quote
Old Oct 26th, 2005, 8:00 AM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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
grumpy is offline   Reply With Quote
Old Oct 26th, 2005, 2:54 PM   #8
sparda
Newbie
 
Join Date: Apr 2005
Location: US
Posts: 15
Rep Power: 0 sparda is on a distinguished road
Send a message via Yahoo to sparda
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;
}
sparda is offline   Reply With Quote
Old Oct 26th, 2005, 3:27 PM   #9
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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;
}
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC