character string as a variable?

This is a discussion on character string as a variable? within the C Programming forums, part of the General Programming Boards category; Can I use a character string as a variable to get a program to do one action if a specific ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    Question character string as a variable?

    Can I use a character string as a variable to get a program to do one action if a specific word is typed and a different action if another word is typed?

    ___________
    #include <stdio.h>

    int main(void)
    {
    char scale[12];
    float temp;
    float temp2;

    printf("Would you like to convert Fahrentheit or Celsius?\n");
    scanf(" %s", scale);/*This works*/
    printf("Enter the temperature in %s that you would like to convert:", scale);
    scanf(" %f", temp);
    if (scale == Fahrenheit)/*This does not work*/
    *rest of program follows*
    }
    __________

    What am I missing in the second instance? Is it even possible to use a string in that manner?

    Guideon72 - I will understand sooner or later

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    strcmp(scale,"Fahrenheit");
    or case insensitive
    _srticmp(scale,"Fahrenheit");

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    What do those mean? I'm just learning C right noow and I don't know all of the commands yet.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    #include <stdio.h>
    #include<string.h>

    int main(void)
    {
    char scale[12];
    float temp;
    float temp2;

    printf("Would you like to convert Fahrentheit or Celsius?\n");
    scanf(" %s", scale);/*This works*/
    printf("Enter the temperature in %s that you would like to convert:", scale);
    scanf(" %f", temp);
    if(_stricmp(scale,"Fahrentheit")==0)
    Fahrentheit2Celsius(); // implement it by yourself
    else if(_stricmp(scale,"Clesius")==0)
    Celsius2Fahrentheit(); // implement it by yourself

    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    Thank you That's what I needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21