Thread: Please help me with the 'Logic' of this program

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    Please help me with the 'Logic' of this program

    Hello All,

    The question that I am attempting to work out is as follows :

    "A government research lab has concluded that an artificial sweetener commonly used in diet soda pop
    will cause death in lab mice. A friend of yours is desperate to lose weight but can't give up soda pop.
    Your friend wants to know how much soda pop it's possible to drink without dying as a result. Write a
    program to supply the answer. The input to the program is the amount of artificial sweetener needed to kill
    a mouse, the weight of the mouse and the weight of the dieter. To ensure the safety of your friend, be sure
    the program requests the weight at which the dieter will stop dieting, rather than the dieter's current weight.
    Assume that diet soda contains 1/10th of 1% artificial sweetener. Use a variable declaration with the modifier
    'const' to give a name to this fraction. You may want to express the percent as the double value of 0.001.
    Your program should allow the calculation to be repeated as often as the user wishes."

    I have completed the first draft of the code which is attached. My question is :

    1) Is the logic of the program correct ? For example the cin values ;

    SweetnerMouse = 0.080
    WeightMouse(grams) = 50grams
    WeightDieter(grams) = 65000grams

    Yields the value '104000' for the variable (DietSodaPopCans); meaning that it takes 104000 cans
    to kill the dieter at his/her given dieting weight. Does it sound sensible ? I feel that the two
    equations written on the C++ code is logical. But I would need some checking on that please.

    2) The question above requires that the program be repeated as many times as the user wishes. For this,
    what type of looping mechanism could I use ? Would a while looping suffice or should I go for
    do..while ?

    Code:
    The  Source  Code  of  the  program  is  shown  below :
    
    //Author: Andy8
    //Soda Pop Death
    //Date: 23 August 2011
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
        
        const double DIET_SODA_SWEETNER = 0.001;
        int DietSodaPopCans;
        double SweetnerMouse;
        double WeightMouse;
        double SweetnerDieter;
        double WeightDieter;
        
        cout << "This program calculates how many cans of soda it will take to kill you !\n";
        cout << "Each can contains 0.001 (0.1%) of artificial sweetener\n" << endl;
        
        cout << "Enter the amount of Artificial Sweetner needed to kill a mouse: \n";
        cin >> SweetnerMouse;
            
        cout << "Enter the weight of the mouse in grams: \n";
        cin >> WeightMouse;
        
        cout << "Enter the weight of the dieter in grams at which dieting activity will be stopped: \n";
        cin >> WeightDieter;
        
            SweetnerDieter = (SweetnerMouse/WeightMouse) * WeightDieter;
        
            DietSodaPopCans = (SweetnerDieter/DIET_SODA_SWEETNER);
        
        cout << "The amount of Diet Soda Pop Can's that would kill the dieter is: " << DietSodaPopCans; 
        
        return 0;
        
        
    }
    Thanks

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Would a while looping suffice or should I go for
    do..while ?
    Doesn't matter.
    You can fit any iterating logic into any form of loop.

    Also, Why do you think you need loops for this?
    Wouldn't a straight calculation of the value suffice ?

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    Hi ,

    Thanks for your reply. You're right I wouldn't need a looping mechanism as the straight calculation seems to work just fine.
    But the program should allow the calculation to be repeated as often as the user wishes.
    So over here I think a looping mechanism must be employed. Which is why I was considering to use the 'while' looping.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Ok..in that case..
    Put the main part within the loop
    like:
    Code:
    while(true)
    {
        /*Main Code*/
        if(/*Condition for Quitting*/)
              break;
        cin.ignore();
    }

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How To Ask Questions The Smart Way

    Please Help Me With The &#39;Logic&#39; Of This Program - C And C++ | Dream.In.Code

    Is there anywhere else we need to look before deciding whether it's worth spending any effort on your assignment?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Salem View Post
    Is there anywhere else we need to look before deciding whether it's worth spending any effort on your assignment?
    Perhaps: Please Help me....blah

    You know if you spent as much time thinking about the problem as you did posting it around the internet you would have your solution by now.

    EDIT: What school is this for? I have seen this exact question on and off for years.
    Last edited by AndrewHunter; 08-23-2011 at 10:27 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. logic of the program.
    By vijayshankar in forum C Programming
    Replies: 5
    Last Post: 02-08-2011, 05:30 AM
  2. What is the logic for this program
    By Shidlingayya in forum C Programming
    Replies: 4
    Last Post: 08-27-2007, 06:03 AM
  3. program logic
    By kermit in forum C Programming
    Replies: 12
    Last Post: 08-16-2003, 02:42 PM
  4. Need help with the logic of the program
    By unregistered in forum C++ Programming
    Replies: 6
    Last Post: 01-16-2002, 07:39 PM
  5. logic on a program
    By officegirl in forum C Programming
    Replies: 0
    Last Post: 10-13-2001, 10:41 PM