A little Help with a small program

This is a discussion on A little Help with a small program within the C Programming forums, part of the General Programming Boards category; I've just started out with C awhile ago I'm just making my way down to learning Arrays and such... But ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    Exclamation A little Help with a small program

    I've just started out with C awhile ago I'm just making my way down to learning Arrays and such...
    But I'm having a little problem with this program and I"m not sure why I was wondering if anyone could help..

    This is the error...

    [Warning] comparison is always false due to limited range of data type in function `main'

    Can't figure out how to fix it..

    Code is in the next thread...

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    Here is the code...

    Code:
    #include <stdio.h>
    
    int main()
     {
     int x,y;
     char choice;
      printf("\n  Number Squarer/SquareRooter; Coded by whtpirate(elder(justin)) of LogiTeam.\n");
      puts("\n");
      
      // Start of the main stuff.
      printf("\tWhat do you want to do? (s/sr): ");
      scanf("%c", &choice);
      if (choice == 's')
       { 
        printf("\n\tEnter any number: ");
         scanf("%d", &x);
         // This is what squares the numbers
           y = x*x;
             printf("\n\t'%d' is your number squared.\n", y);
        }
      if (choice == 'sr')
        {
         printf("\tEnter any number: ");
          scanf("%d", &x);
           // This is what squareroots the numbers
           y = x^x;
            printf("\n\t'%d' is your numbers sq-rooted.\n", y);
         }         
     return 0;
    }
    Last edited by whtpirate; 06-05-2003 at 05:07 PM.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>if (choice == 'sr')

    Choice is a char. Thus, it holds one character. You will need to use a character array and strcmp to check multiple letters.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    yay
    thank you
    I didn't think I would get such a quick reply

  6. #6
    .........
    Join Date
    Nov 2002
    Posts
    303
    woops didn't see the above post, edited out heh.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    Thank you SourceCode
    yep it makes sense, I'm busy learning strcmp(); right now heh
    If I have any problems from now i know where to come

    thanks..

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    Hey guys I got it working thanks to your help and a friend of mines helps

    Code:
    #include <stdio.h>
    
    int main()
     {
    int x;
    float y;
    char choice[3];
     
      printf("\n  Number Squarer/SquareRooter; Coded by whtpirate(elder(justin)) of LogiTeam.\n");
      puts("\n");
      
      // Start of the main stuff.
      printf("\tWhat do you want to do? (s/sr): ");
       scanf("%2s", choice);
      if (!strcmp(choice, "s"))
       { 
        printf("\n\tEnter any number: ");
         scanf("%d", &x);
         // This is what squares the numbers
           x *=x;
             printf("\n\t'%d' is your number squared.\n", x);
        }
      if (!strcmp(choice, "sr"))
        {
         printf("\tEnter any number: ");
          scanf("%d", &x);
           // This is what squareroots the numbers
           y = sqrt(x);
            printf("\n\t'%.0f' is your number sq-rooted.\n", y);
         }         
     return 0;
    }
    Last edited by whtpirate; 06-05-2003 at 07:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Please Help with small program
    By cjohnman in forum C Programming
    Replies: 11
    Last Post: 04-14-2008, 09:59 AM
  3. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 09:52 AM
  4. Help writing small program
    By guitarhead2000 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2004, 12:42 PM
  5. Very small program...Whats wrong???
    By SmokingMonkey in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2003, 09:09 PM

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