Thread: I am trying to create a very simple program but running into difficulty

  1. #1
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59

    I am trying to create a very simple program but running into difficulty

    I am only just learning this. I am trying to create a program in my own way instead of doing exactly what the tutorial says. What I am trying to do is print only a remainder of a simple division if the program divides a value by itself and the number isn't 1. Maybe you can follow my code. Haha sry if its tough to follow it I am very new to this:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define YARDS_PER_SWEATER 460
    #define YARDS_PER_HAT 220
    
    int main(void) {
        
        int num_balls, yards_per_ball;
        int num_h, num_s;
        float yards_left;
        
        printf("How many balls of yarn do you have?\n");
        scanf("%d" , &num_balls);
        
        printf("How many yards are in each of your balls of yarn?\n");
        scanf("%d" , &yards_per_ball);
        
        num_h = (num_balls*yards_per_ball)/(YARDS_PER_HAT);
        num_s = (num_balls*yards_per_ball)/(YARDS_PER_SWEATER);
        
        if ((num_h||num_s)/(num_h||num_s) != 1)
        
        printf("You have some yarn left over after you make %d sweaters or %d hats for other projects,\nif you want to know how many yards you have, type 'yes' below.\n" , num_s, num_h);
        scanf("%d%d%d" , &Y, &E, &S);
        
        yards_left = num_h
        
        
        
        else if ((num_h||num_s)/(num_h||num_s) == 1))
        
        print("You can make %d sweaters or %d hats, but you have no yarn left over.")
        
        getchar();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    My first suggestion is for you to review your documentation for the if() statement. Here is a good tutorial on the subject.

    Also do you understand that when doing integer math there are no fractions, 1/5 will yield zero.

    Jim

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your avatar! It's Ricky from Trailer Park Boys!
    "It doesn't take rocket appliances to get your grade 10."


    This is incomprehensible:
    Code:
     if ((num_h||num_s)/(num_h||num_s) != 1)
    I can't even guess what you meant here.
    Can you describe what you want this line to do?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Quote Originally Posted by oogabooga View Post

    This is incomprehensible:
    Code:
     if ((num_h||num_s)/(num_h||num_s) != 1)
    I can't even guess what you meant here.
    Can you describe what you want this line to do?
    if num_h or num_s divided by themselves does not equal one, then print stuff

    Hahaha this made me laugh.. Yea what I am trying to do is take a computed value, divide it by itself and if the result didnt equal 1, I wanted to display only the remaining yards left over
    Last edited by Mike Garber; 09-02-2013 at 07:51 AM. Reason: clarification

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by Mike Garber View Post
    if num_h or num_s divided by themselves does not equal one, then print stuff

    Hahaha this made me laugh.. Yea what I am trying to do is take a computed value, divide it by itself and if the result didnt equal 1, I wanted to display only the remaining yards left over
    Something like this?
    Code:
    a = num_h / num_h;
    b = num_s / num_s;
    
    if (a != 1 || b != 1)  //maybe can be shorter?
    {
       //print stuff
    }
    *please read the post by jim on if statements

    fix above and post again man. Will go through it again! There are a few more errors.
    Try to get it to at least compile by fixing syntax errors.
    Last edited by bos1234; 09-02-2013 at 08:22 AM. Reason: fixed code slightly and added text

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I'm stymied, man.
    Unless it's zero, a number divided by itself is always 1.
    Can you describe your assignment?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Quote Originally Posted by oogabooga View Post
    I'm stymied, man.
    Unless it's zero, a number divided by itself is always 1.
    Can you describe your assignment?

    Yea, I dont know what I was thinking at the time hahaha. What I want to do is basically tell the program

    if num_h or num_s has a remainder, I want to only display the number of hats with the remaining yards left over.. Still struggling to make that happen.spelling

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Then you should be looking into using the modulus operator %, remember there are no fractions with integers, just whole numbers.


    Jim

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Considering just the hats.
    Code:
        total_yards = num_balls * yards_per_ball;
        num_h = total_yards / YARDS_PER_HAT;
        yards_left = total_yards - num_h * YARDS_PER_HAT;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Quote Originally Posted by jimblumberg View Post
    Then you should be looking into using the modulus operator %, remember there are no fractions with integers, just whole numbers.


    Jim
    I looked at it and it threw me for a loop. I dont know what that cin >> means.. All I want to do is do that calculation for num_hats and show a remainder

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    To use the modulus operator as jim has suggested (and it's a better suggestion than mine) you'd replace the last line in my last post with this:
    Code:
    yards_left = total_yards % YARDS_PER_HAT;
    BTW: cin is from C++, so you don't have to understand that (yet!).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Ok I got it to go further without errors but not compiled yet. After reading about the modulus and seeing oogabooga's post, this is what I have now

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define YARDS_PER_SWEATER 460
    #define YARDS_PER_HAT 220
    
    int main(void) {
        
        int num_balls, yards_per_ball;
        float num_h, num_s;
        float yards_left;
        
        printf("How many balls of yarn do you have?\n");
        scanf("%d" , &num_balls);
        
        printf("How many yards are in each of your balls of yarn?\n");
        scanf("%d" , &yards_per_ball);
        
        num_h = (num_balls*yards_per_ball)/(YARDS_PER_HAT);
        num_s = (num_balls*yards_per_ball)/(YARDS_PER_SWEATER);
        yards_left = total_yards % (YARDS_PER_HAT || YARDS_PER_SWEATER);
        
        if (yards_left > 0)
        
           printf("You have some yarn left over after you make %lf sweaters or %lf hats for other projects,\nif you want to know how many yards you have, type 'yes' below.\n" , num_s, num_h);
           scanf("%d%d%d" , &Y, &E, &S);
        
        else
        
            print("You can make %lf sweaters or %lf hats, but you have no yarn left over.");
        
        getchar();
        return 0;
    }

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to declare total_yards and do the calculation to set it. Then you may as well use it in your num_h and num_s calculations. Try to carefully reason about what is going on. Every detail in a program must be correct.

    And you'll need two yards_left variables, call them yards_left_h and yards_left_s. You're going to have to do separate calculations for these. You can't combine them (if I understand your assignment correctly).

    And you'll need two separate outputs. One for how many yards left if the user makes all hats and the other for all sweaters.

    I'd remove the scanf line for now, since it's completely broken. You don't have to program in every element of a program on your first go. Just get something compiling and working. Then you can add more.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Try not to use too long lines, widely used agreement is to limit line length to 80 character

    for example your line 25 could be formated in the follwoing way

    Code:
        printf("You have some yarn left over after you make %lf sweaters"
            "or %lf hats for other projects,\n"
            "if you want to know how many yards you have, type 'yes' below.\n",
            num_s, num_h);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Ok, this is what I have now, but I am getting an error at the yards_left calculation
    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define YARDS_PER_SWEATER 460
    #define YARDS_PER_HAT 220
    
    int main(void) {
        
        int num_balls, yards_per_ball;
        int num_h, num_s;
        float yards_left, yards_left1, total_yards;
        
        
        
        printf("How many balls of yarn do you have?\n");
        scanf("%d" , &num_balls);
        
        printf("How many yards are in each of your balls of yarn?\n");
        scanf("%d" , &yards_per_ball);
        
        
        total_yards = (num_balls*yards_per_ball);
        num_h = (total_yards)/(YARDS_PER_HAT);
        num_s = (total_yards)/(YARDS_PER_SWEATER);
        yards_left = total_yards % (YARDS_PER_HAT || YARDS_PER_SWEATER);
        
        
        
        if (yards_left > 0)
        
           printf("You have %lf yards of  yarn left over after you make %d sweaters"
                  "or %d hats.\n" , yards_left, num_s, num_h);
                  
        
        else
        
           print("You can make %d sweaters or %d hats with no yarn left over.");
        
        getchar();
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a simple program using linked list
    By tillu in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2011, 12:53 AM
  2. create a dll for C++ simple program and call from VB
    By motiz in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2008, 04:54 AM
  3. Replies: 6
    Last Post: 07-20-2007, 09:23 AM
  4. how to create file in the running program
    By skyboy5122 in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 01:52 AM
  5. Error when running a simple program
    By swishiat.com in forum C Programming
    Replies: 11
    Last Post: 12-14-2003, 06:53 AM