Thread: if loop only goes through once

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    8

    Post if loop only goes through once

    Hey guys! I'm writing a program that reads a starting time and an ending time for delivery that it normally takes. There is construction with a 25% delay on deliveries. The program must take the two times entered, incorporate the 25 percent delay and output the new ending time.
    It reads times using a 24 hour time period (so 2345, 0615, etc.) converts the time to minutes by multiplying by .6. But if I try to divide by .6 to put it back into hour time, it goes to base 10 not base 60 so it comes up with times like 0697. So I made an if loop to figure out number of hours and then the left over put into minutes. Anyway, my problem is the if loop only goes through once. Any way you can see why it's doing this? (P.S. I'm pretty proficient in C++ and C# is a new language for a class in college, so I understand terms pretty well)

    Code:
    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            Double _startTimeMinutes, _endTimeMinutes, _totalTime, _timeAddedMinutes, _percent, _newEndTimeMinutes, _startTime, _endTime;
            _percent = .25;
            int _hour = 0;
    
            Console.WriteLine("Please input the start time in 24 hour mode (no colon or decimals): ");
            _startTime = int.Parse(Console.ReadLine());
            Console.WriteLine("Please input the end time in 24 hour mode (no colon or decimals): ");
            _endTime = int.Parse(Console.ReadLine());
            _startTimeMinutes = _startTime * .6;
            _endTimeMinutes = _endTime * .6;
            _totalTime = _endTimeMinutes - _startTimeMinutes;
            _timeAddedMinutes = _totalTime * _percent;
            _newEndTimeMinutes = _timeAddedMinutes + _endTimeMinutes;
            
            Double _newEndTime = _newEndTimeMinutes;
            if (_newEndTime > 60)
            {
                _newEndTime = _newEndTimeMinutes - 60;
                _hour++;
            }
           char _space = ' ';
            if (_hour < 10)
            {
                _space = ' ';
            }
            else { _space = '0'; }
            Console.WriteLine("New end time because of construction is: {0}{1}:{2}",_space, _hour, _newEndTime);
    
            Console.ReadLine();
            
        }
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    There are no such things as if loops. There are while loops, do while, for loops, foreach loops.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I see no loop in the code. If you are as proficient in C++ as you claim then C# syntax and keywords should be a cinch for you. The C# loop constructs, save foreach, are all natively supported in C++. STL has a foreach although it is not nearly as simple to use as a C# foreach.

    Another type of loop in C# is the enumerator or iterator loop. C++ has this as well but you use the iterators inside of while or for loops. C# actually has a type of iterator loop that will iterate over collections and it is a part of the language.
    Last edited by VirtualAce; 01-19-2011 at 06:06 PM.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Yeah i saw that right after I posted haha I feel dumb sorry guys my mistake!

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    .NET has a lot of classes to help you do this kind of stuff. For instance:
    Code:
            static void Main(string[] args)
            {
                string input;
                Console.WriteLine("Please input the start time in 24 hour mode (no colon or decimals): ");
                input = Console.ReadLine();
                DateTime startTime = DateTime.Parse(input.Substring(0, 2) + ":" + input.Substring(2, 2));
    
                Console.WriteLine("Please input the end time in 24 hour mode (no colon or decimals): ");
                input = Console.ReadLine();
                DateTime endTime = DateTime.Parse(input.Substring(0, 2) + ":" + input.Substring(2, 2));
    
                TimeSpan totalTime = endTime - startTime;
                TimeSpan adjustedTotalTime = new TimeSpan((long)Math.Ceiling(totalTime.Ticks * 1.25));
    
                Console.WriteLine("Original total time: " + totalTime.ToString());
                Console.WriteLine("Total time with construction: " + adjustedTotalTime.ToString());
            }
    Also, stuff like:
    Code:
           char _space = ' ';
            if (_hour < 10)
            {
                _space = ' ';
            }
            else { _space = '0'; }
    ...can be shortened to just:
    Code:
    char _space = _hour < 10 ? ' ' : '0';
    Last edited by itsme86; 01-19-2011 at 06:18 PM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    I didn't understand a lot of the first code you posted, but I did under stand the ? : statement since that's in c++.
    I did run into a problem using an actual loop though... its become an infinate loop and I don't know why...

    Code:
     double _newEndTime = 61;
            for (_newEndTime = 61; _newEndTime > 60; _newEndTime = _newEndTimeMinutes - 60)
            {
                _hour++;
            }
    before the for loop I used the while loop:

    Code:
    double _newEndTime = 61;
    while (_newEndTime > 60)
    {
    _newEndTime = _newEndTimeMinutes - 60;
    _hour++;
    }

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by treesap526 View Post
    I didn't understand a lot of the first code you posted, but I did under stand the ? : statement since that's in c++.
    I did run into a problem using an actual loop though... its become an infinate loop and I don't know why...

    Code:
     double _newEndTime = 61;
            for (_newEndTime = 61; _newEndTime > 60; _newEndTime = _newEndTimeMinutes - 60)
            {
                _hour++;
            }
    before the for loop I used the while loop:

    Code:
    double _newEndTime = 61;
    while (_newEndTime > 60)
    {
    _newEndTime = _newEndTimeMinutes - 60;
    _hour++;
    }
    When I say before the for loop I meant I've tried it both ways and cant get it to work

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I think you're using the wrong variable or something. You have:
    Code:
    _newEndTime = _newEndTimeMinutes - 60;
    But I think it should be:
    Code:
    _newEndTime = _newEndTime - 60;
    This kind of problem can be avoided by using the -= operator (_newEndTime -= 60).
    If you understand what you're doing, you're not learning anything.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you can't ever change what happens inside your loop. Suppose _newEndTimeMinutes is 135. Then _newEndTime gets set to 75, and hour is incremented. We do it again: _newEndTime gets set to 75 (_newEndTimeMinutes didn't change, after all), and hour is incremented. We do the loop again: _newEndTime gets set to 75 (_newEndTimeMinutes didn't change, after all), and hour is incremented. We do the loop again....

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by itsme86 View Post
    I think you're using the wrong variable or something. You have:
    Code:
    _newEndTime = _newEndTimeMinutes - 60;
    But I think it should be:
    Code:
    _newEndTime = _newEndTime - 60;
    This kind of problem can be avoided by using the -= operator (_newEndTime -= 60).
    If you look above at my original code, _newEndTimeMinutes is from taking the total time of the trip, taking 25 percent of that, adding that onto the end time so the trip takes longer. The variable _newEndTimeMinutes holds that end time (so say 6:15 pm) but it's in minutes and I'm trying to convert it to hours and minutes, but if i just divide by .6, it gives me like .97 when i want it to give me base 60 or what it's called.
    TECHNICALLY I could do this instead, and this is what I mean, but i dont know if it works:
    Code:
    while (_newEndTimeMinutes > 60)
    { _newEndTimeMinutes = _newEndTimeMinutes - 60;
    _hour++
    }
    // so just take away 60 from the value and add one to the hour mark while //_newEndTimeMinutes is over 60

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just save yourself headaches by using DateTime and TimeSpan like I showed:
    Code:
    Please input the start time in 24 hour mode (no colon or decimals):
    0125
    Please input the end time in 24 hour mode (no colon or decimals):
    0848
    Original total time: 07:23:00
    Total time with construction: 09:13:45
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by itsme86 View Post
    Just save yourself headaches by using DateTime and TimeSpan like I showed:
    Code:
    Please input the start time in 24 hour mode (no colon or decimals):
    0125
    Please input the end time in 24 hour mode (no colon or decimals):
    0848
    Original total time: 07:23:00
    Total time with construction: 09:13:45
    I would if we had covered classes and objects and such. I'm in a major beginner level c# class and we only JUST learned about loops and so we have to use loops and variables and output and input (which is all we've learned) in order to write this program or we dont get the points. It's lame and holding me back, but i have to ughhh

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    wow. solved it. haha all I did was retype the while loop. I don't even know what I did different... but hey, it works!
    for anyone else who had the same question and came here for answers, here was my final code:
    Code:
    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            Double _startTimeMinutes, _endTimeMinutes, _totalTime, _timeAddedMinutes, _percent, _newEndTimeMinutes, _startTime, _endTime;
            _percent = .25;
            int _hour = 0;
    
            Console.WriteLine("Please input the start time in 24 hour mode (no colon or decimals): ");
            _startTime = int.Parse(Console.ReadLine());
            Console.WriteLine("Please input the end time in 24 hour mode (no colon or decimals): ");
            _endTime = int.Parse(Console.ReadLine());
            _startTimeMinutes = _startTime * .6;
            _endTimeMinutes = _endTime * .6;
            _totalTime = _endTimeMinutes - _startTimeMinutes;
            _timeAddedMinutes = _totalTime * _percent;
            _newEndTimeMinutes = _timeAddedMinutes + _endTimeMinutes;
            while (_newEndTimeMinutes > 60)
            {
                _newEndTimeMinutes = _newEndTimeMinutes - 60;
                _hour++;
            }
           char _space = ' ';
            if (_hour > 10)
            {
                _space = ' ';
            }
            else { _space = '0'; }
            Console.WriteLine("New end time because of construction is: {0}{1}:{2}",_space, _hour, _newEndTimeMinutes);
    
            Console.ReadLine();
            
        }
    }

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by treesap526 View Post
    but i dont know if it works:
    This is really a very problematic statement. You're not in the business of creating mysteries for yourself -- you should be able to sit down with a few lines of code (especially when it doesn't have any "new" syntax), and work out whether the code will "work" (as in, do what you want to do) or not. If it does, then fine; if it doesn't, then seeing how it fails will often give a clue as to what to do instead. But there's no substitute for thinking about the problem; and the method for how to do the work isn't really very language-specific.

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by tabstop View Post
    This is really a very problematic statement. You're not in the business of creating mysteries for yourself -- you should be able to sit down with a few lines of code (especially when it doesn't have any "new" syntax), and work out whether the code will "work" (as in, do what you want to do) or not. If it does, then fine; if it doesn't, then seeing how it fails will often give a clue as to what to do instead. But there's no substitute for thinking about the problem; and the method for how to do the work isn't really very language-specific.
    Let me rephrase haha: I don't know if that's what is causing the problems, I haven't worked it out yet. But I did get it to work and that method did work. But for some reason when I tried it earlier it didn't work, even though to me it looked like it should. Hence the "dont know" part. But thanks for the philosophy, it hit home haha very nice tabstop!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

Tags for this Thread