need help with strings

This is a discussion on need help with strings within the C Programming forums, part of the General Programming Boards category; I'm writing this program the long way with "if" statements instead of using an array. I know, there is an ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    need help with strings

    I'm writing this program the long way with "if" statements instead of using an array. I know, there is an easier way of doing this but I want to understand the long way before the shortway. Anyways, I'm writing a C program for the following problem:

    The table below shows the normal boiling points of several substances. Write a program that prompts the user for the observed boiling point of a substance in Celsius degrees and identifies the substance if the observed boiling point is within 5% of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message “Substance unknown.” Your program should define and call a function “within_x_percent” that takes as parameters a reference value ref, a data value data, and a percentage value x and returns 1 meaning true if data is within x percent of ref –that is,

    (ref – x% * ref) < data < (ref + x% * ref). Otherwise “within_x_percent would return zero, meaning false. For example, the call “within_x_percent(357, 323, 10) would return true, since 10% of 357 is 35.7, and 323 falls between 321.3 and 392.7.

    Table:

    Substance / Normal boiling point (celcius)

    Water / 100

    Mercury / 357

    Copper / 1187

    Silver / 2193

    Gold / 2660

    The problem I'm having is with my declarations of the substances (water, mercury,copper,gold). When I compile, I get the error, "warning: overflow in implicit constant conversion" and "warning: character constant is too long for its type." I'm not too familiar with strings yet. Can I use int instead of char? Can someone tell me the proper way of declaing these? I'm sure I may have more wrong with my code but I would like to get by this problem first before I tweak anything else.

    Here is my code:

    Code:
    /* 
       Filename:    range.c
    
       Description: Displays a substance if the observed boiling point is 
                    within 5% of the expected boiling point.
    
    */
    
    # include <stdio.h>
    
       void introduction (void);
    
       int within_x_percent (double ref, double data, double x);
    
     int main()
    
      {
      
      /*Allocate memory for data*/
    
       introduction ();
       
       double ref,data,x;
    
       char s1 = 'Water';
       char s2 = 'Mercury';
       char s3 = 'Copper';
       char s4 = 'Silver';
       char s5 = 'Gold'; 
    
      /*Prompt user for data*/
    
         printf ("Enter the observed boiling point in degrees celcius:\n");
         scanf ("%f",&data);
    
      /*Processing and display results*/
    
           if (data = within_x_percent(100,data,x)
              printf("%d is within 5% of %c.\n",data,s1);
    
           else if (data = within_x_percent(357,data,x)
              printf("%d is within 5% of %c.\n",data,s2);
    
           else if (data = within_x_percent(1187,data,x)
              printf("%d is within 5% of %c.\n",data,s3);
    
           else if (data = within_x_percent(2193,data,x)
              printf("%d is within 5% of %c.\n",data,s4);
    
           else if (data = within_x_percent(2660,data,x)
              printf("%d is within 5% of %c.\n",data,s5);
    
           else 
              printf("Substance Unknown\n"); 
    
    
     return 0;
     
      }
    
         void introduction (void)
    
          {
    
           printf ("\nThis program identifies a substance if the observed");
           printf ("is within 5% of the expected boiling point. If the"); 
           printf ("data is more than 5% higher or lower, the program will"); 
           printf ("display the message substance unknown.\n");
    
          }
    
         int within_x_percent (double ref, double data, double x)
    
          {
            x=.05;
    
            (ref-x*ref) < data < (ref+x*ref);
    
    
          }

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The problem is that you're confusing strings with chars. A char variable will hold one character, and a string (in C this is - C++ programmers prefer to use the string class) is an array of characters. If you do not understand how to use arrays of C-style strings - you can refer to the tutorials on this website.

    Single quotes are used to surround a single character, like a char variable. If you want to specify a string literal, you need to use double quotes.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Thanks for your reply. This is probably a stupid question but, is it possible to use int as the data type for these?

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by s_ny33
    Thanks for your reply. This is probably a stupid question but, is it possible to use int as the data type for these?
    Remember, there are no stupid questions, only stupid people.

    Use int for what, the data type to store a string? Not practically, you'd use an array of char.

    Code:
    char s1[] = "Water";
    char s2[] = "Mercury";
    Of course, you can have an array of pointers to these, so you don't need to refer to s1, s2, s3, etc. individually, and you could also store the temperature associated with them using a struct array, but that's probably beyond what you've learned so far.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    I can't believe it...I think the lightbulb is coming on now. I commented out the s1,s2,s3..., and hardcoded the substances within the printf statements. Now I think my if statements are wrong. The only error I get now is "In function 'main' "48: error: parse error before printf". Does anyone know what I'm doing wrong? Here is what I changed after commenting out the s1,s2,s3,s4 and s5 variables:

    Code:
      /*Processing and display results*/
    
           if (data = within_x_percent(100,data,x)
              printf("%d is within 5% of water.\n",data);
    
           else if (data = within_x_percent(357,data,x)
              printf("%d is within 5% of mercury.\n",data);
    
           else if (data = within_x_percent(1187,data,x)
              printf("%d is within 5% of copper.\n",data);
    
           else if (data = within_x_percent(2193,data,x)
              printf("%d is within 5% of silver.\n",data);
    
           else if (data = within_x_percent(2660,data,x)
              printf("%d is within 5% of gold.\n",data);
    
           else 
              printf("Substance Unknown\n");

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    I forgot to mention, I did change the %d to %lf in the printf statements.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by s_ny33
    I forgot to mention, I did change the %d to %lf in the printf statements.
    It would be in the best interest of being correct to use %f for both float and double with printf (*scanf is a different story).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by s_ny33
    Code:
    if (data = within_x_percent(100,data,x)
    How many open parentheses are there? How many close parentheses are there?

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    That did it...thanks. Now I'm trying to do some formatting of my output but I get parse errors. Can someone tell me what is wrong with this? I addedd some words to the printf statement to make more sense but it doesn't like that I put the printf statement on two separate lines. How do I split the line properly?

    Here is what I did:

    Code:
    if (data = within_x_percent(100,data,x))
              printf("%f is within 5 percent of the boiling point of"); 
              printf("water.\n",data);

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by s_ny33
    That did it...thanks. Now I'm trying to do some formatting of my output but I get parse errors. Can someone tell me what is wrong with this? I addedd some words to the printf statement to make more sense but it doesn't like that I put the printf statement on two separate lines. How do I split the line properly?

    Here is what I did:

    Code:
    if (data = within_x_percent(100,data,x))
              printf("%f is within 5 percent of the boiling point of"); 
              printf("water.\n",data);
    1. When you do:
    Code:
    if (condition)
        statement;
    You can only have one statement, otherwise you need a block:
    Code:
    if (condition)
    {
        statement1;
        statement2;
        statement3;
    }
    Your example above contains two statements, only one is part of the if.

    2. The first printf has a %f specifier, so it expects a float or double within that function. The second printf has no %f specifier, but a type "data", which it will ignore, because there's no format specifer. You want:
    Code:
    if (data = within_x_percent(100,data,x))
              printf("%f is within 5 percent of the boiling point of \
    water.\n",data);
    If you wish to split it to two lines. Alternatively, you can just have two strings:
    Code:
    if (data = within_x_percent(100,data,x))
        printf(""%f is within 5 percent of the boiling point of"
            " water.\m", data);
    Last edited by cwr; 10-11-2005 at 08:19 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How many format specifiers are in the first printf statement? (Here's a hint, it's one.) How many arguments do you have to fill those specifiers? (Here's a hint, it's zero.)
    How many format specifiers are in the second printf statement? (Here's a hint, zero.) How many arguments do you have to fill those? (Here's your final hint, one.)

    See anything wrong with those? (Here's the for real last hint: Yes.)


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    Code:
    if (data = within_x_percent(100,data,x))
              printf("%f is within 5 percent of the boiling point of water.\n", data);

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    OK...thanks. Why is the first %f in my printf statement return 1.000000 if the user inputs 97 ("data" variable)? Why doesn't this display 97?

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Dave_Sinkula
    It would be in the best interest of being correct to use %f for both float and double with printf (*scanf is a different story).
    Perhaps this?

    Post your latest code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Here is the code:

    Code:
    /* 
       Filename:    range.c
    
       Description: Displays a substance if the observed boiling point is 
                    within 5% of the expected boiling point.
    
    */
    
    # include <stdio.h>
    
       void introduction (void);
    
       int within_x_percent (double ref, double data, double x);
    
    
     int main()
    
      {
      
      /*Allocate memory for data*/
    
       introduction ();
       
       double ref,data,x;
    
    
      /*Prompt user for data*/
    
         printf ("\nEnter the observed boiling point in degrees celcius:");
         scanf ("%f",&data);
    
      /*Processing and display results*/
    
           if (data = within_x_percent(100,data,x))
              printf("%f is within 5 percent of the boiling point of \
    water.\n",data);
    
           else if (data = within_x_percent(357,data,x))
              printf("%f is within 5 percent of the boiling point of \
    mercury.\n",data);
    
           else if (data = within_x_percent(1187,data,x))
              printf("%f is within 5 percent of the boiling point of \
    copper.\n",data);
    
           else if (data = within_x_percent(2193,data,x))
              printf("%f is within 5 percent of the boiling point of \
    silver.\n",data);
    
           else if (data = within_x_percent(2660,data,x))
              printf("%f is within 5 percent of the boiling point of \
    gold.\n",data);
    
           else 
              printf("Substance Unknown\n"); 
    
    
     return 0;
     
      }
    
         void introduction (void)
    
          {
    
           printf ("\nThis program identifies a substance if the observed \
    substance is within 5 percent of the expected boiling point.If \
    the data is more than 5 percent higher or lower, the program \
    will display the message substance unknown.\n");
    
          }
    
         int within_x_percent (double ref, double data, double x)
    
          {
            x=.05;
    
            (ref-x*ref) < data < (ref+x*ref);
    
    
          }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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