Thread: Help with some quick questions.

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    17

    Help with some quick questions.

    What I need to do is make a program that takes a number less than 6 digits that is inputed by the user and then solve according to the op inputed.

    I just need to know how to get the reverse of the number the user enters without asking for the reverse.

    My second question is: is it legal to use a while statement like this?
    while(op == + || - || * || /)

    This is the code I have so far. If you see any other problems feel free to correct. Thanks!
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        float num, sum, reverse;
        char op;
    
    
        printf("This program solves a five digit of smaller number and it reveresed.");
    
    
        printf("Press any key to exit.");
    
    
        while(op == + || - || * || /)
        {
        printf("Enter the operator you wish to use (+, -, /, *): ");
        scanf(" %c", &op);
    
    
        printf("Enter a 5 or less digit number: ");
        scanf(" %f", &num);
    
    
        
        reverse = ;
    
    
        if (op == '+')
            sum = num + reverse;
        printf(" %f", sum);
        if (op == '-')
            sum = num - reverse;
        printf(" %f", sum);
        if (op == '*')
            sum = num * reverse;
        printf(" %f", sum);
        if (op == '/')
            sum = num / reverse;
        printf(" %f", sum);
        
        }
    
    
    getchar();
    getchar();
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jp5a9852 View Post
    My second question is: is it legal to use a while statement like this?
    while(op == + || - || * || /)
    No it is not.

    There are plenty of other problems in your "code" as well. But the nature of them makes it obvious you are just tapping away at your keyboard, rather than trying to understand how C works. It would be a waste of time for anyone else attempting to correct it, since you are not actually trying to understand, and will not understand the changes they might make.

    Try actually reading any half decent textbook or tutorial before proceeding further.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
     while(op == + || - || * || /)
    must be written this way
    Code:
     while(op == '+' || op == '-' || op == '*' || op == '/')

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're kidding, eh, camel-man.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Well not really? lol I mean is not that the correct syntax? Now whether or not that is logically the right way about doing this is extremely questionable

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by camel-man View Post
    Well not really? lol I mean is not that the correct syntax? Now whether or not that is logically the right way about doing this is extremely questionable
    Just to be clear, I wrote my "you're kidding" comment BEFORE you inserted single quote characters into the code sample.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    I'm just doing it the way I've been taught in class so far, as you can tell, I've not been learning for very long. Thanks for the help anyway, it was so obvious too, I used the single quotes below it even. However I still need to know to reverse the number.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    By "reverse" a number, do you mean reverse the digits in the number?
    ie., 91 becomes 19?


    There is an easy way to reverse numbers, if you take the number in, as digits in a string, but if it's an int, for instance, it can be done by "peeling" the digits off the 1's column, one at a time, like so:

    Code:
    #include <stdio.h>
    
    int main(void) {
       int i,one,n=1234567;
       char digits[8] = {""};
       i=sizeof(digits)-1;
       digits[i]='\0';
       i=0;
       while(n>0) {
          one=n%10;
          digits[i++]=one+'0';
          n/=10;  
       }
       printf("%s\n",digits);
       return 0;
    }
    If you need the digits to be an actual number, use sscaf(digits, "%d", &n); and then print out n.

    For a while loop, you have to have it "set up" before the while() is reached by the program. That's where the test is made by the program, and only if the test return true (non-zero), will the rest of the loop be executed.

    That while loop test makes me reach for the Pepto Bismol.
    Last edited by Adak; 11-10-2012 at 06:56 PM.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm not aware of any library function that can perform this for you. It would take several lines of code to accomplish.

    Can you be a little more clear on the specs? Reversing integers shouldn't be too hard, but you're declaring the input as a floating point. How are you expecting it to look when reversed? Something like 123.45 -> 54.321?

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    That is correct Matticus.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If your teacher is requiring you to reverse digits in floating point, s/he should be summarily fired for incompetence. If you have decided you need to use floating point variables, you are badly mistaken.

    Floating point representations are an approximation, so many attempts to reverse digits will also be an approximation (i.e. inaccurate). Work with integral values instead.

    For example, a floating point variable cannot represent 0.1 exactly. So, using floating point operations, there is no way to even reverse 1.234 to obtain 432.1 (since a floating point variable can't exactly represent 432.1). The only way you can do it is read two integers separated by a '.', reverse the digits on those, and output the two integers in reverse order (second before first). And, even if you do that, it is not possible to store the result in a floating point variable. So work with integers instead.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    My teacher never specified whether it was supposed to be float or int, so I'll use int, thanks.

    I've revised my code, this is what I have now. I can see why you were mad at my code earlier, it was nonsensical The reverse = 3; was just to test if it would work this far.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main(void)
    {
        int num, sum, reverse;
        char op, ans;
    
    
        printf("This program solves a five digit or smaller number and it reversed.\n");
    
    
        do
        {
        printf("Enter the operator you wish to use (+, -, /, *): ");
        scanf(" %c", &op);
    
    
        printf("Enter a 5 or less digit number: \n");
        scanf(" %d", &num);
    
    
        
        reverse = 3;
    
    
        if (op == '+')
            sum = num + reverse;
        if (op == '-')
            sum = num - reverse;
        if (op == '*')
            sum = num * reverse;
        if (op == '/')
            sum = num / reverse;
        
        printf(" %d", sum);
        
        printf("\nWould you like to continue? Y or N?: ");
        scanf(" %c", &ans);
    
    
        }
        while (toupper(ans) == 'Y');
    
    
        
        printf("Press any key to exit.\n");
    
    
    getchar();
    getchar();
    return 0;
    
    
    }
    Last edited by jp5a9852; 11-10-2012 at 07:59 PM.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jp5a9852 View Post
    I can see why you were mad at my code earlier, it was nonsensical
    I wasn't mad. But, yes, the "code" in your first post was completely nonsensical and, if anything, my initial comments were gentler than warranted.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 quick questions
    By rTeapot in forum C++ Programming
    Replies: 11
    Last Post: 06-28-2012, 05:44 PM
  2. 2 Quick questions
    By omGeeK in forum C Programming
    Replies: 2
    Last Post: 12-06-2010, 03:51 PM
  3. two quick questions
    By procyon4476 in forum Tech Board
    Replies: 6
    Last Post: 03-24-2003, 10:24 AM
  4. 2 quick questions
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-02-2002, 10:37 AM
  5. 2 quick questions
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-29-2001, 12:32 AM