Thread: Issue with program that's calling a function and has a loop

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    Question Issue with program that's calling a function and has a loop

    I was working on a program for school and I corrected the errors that Visual Studio, but the program doesn't do anything. I've struggled with loops and I think that's where the problem lies. Can anyone assist me with what I've done wrong? I've included the assignment instructions and the actual program I wrote.


    Instructions:

    Create and run a program that includes a main and a function. The function name should be convert_to_secs. the function receives an integer representing seconds, an integer representing minutes and an integer representing hours. The function determines the total number of seconds and returns the results to the caller.

    The main program should have a loop to perform conversion 3 times ( loop to be performed 3 times ). Inside the loop :

    1. prompt the user for seconds, minutes and hours.

    2. call the function to perform conversion.

    3. inform the user of the result.

    Run the program. Provide your own data.

    Copy the output from the output window and imbed at the bottom of the program before submitting.

    Student Response: ******I worked on this program and I got the errors cleared but I messed up somewhere as the program didn't work. Here is what I have.**********
    Code:
    #include <iostream> // Robert Sutherland CPT 234 Test 3
    using namespace std; 
    
    int convert_to_secs(int minutes, int hours);
    
    int main()
    
    {
    int convert_to_secs(int minutes, int hours);
    
    int seconds;
    
    while (seconds > 60)
    {
    int seconds;
    int minutes;
    int hours;
    int total_converted_minutes;
    int total_converted_hours;
    
    cout << "Please enter in a number of seconds: ";
    cin >> seconds;
    cout << "Please enter in a number for minutes: ";
    cin >> minutes;
    cout << "Please enter in a number for hours: ";
    cin >> hours;
    
    int convert_to_secs(int minutes, int hours);
    
    if(seconds >= 61)
    
    
    cout << "The number of seconds is " << seconds;
    
    return 0;
    
    
    
    if (minutes > 1)
    
    
    int total_converted_minutes;
    
    cout << minutes << " eqaul " << total_converted_minutes << " seconds ";
    
    return 0;
    
    
    
    
    if (hours > 0)
    
    int total_converted_hours;
    
    total_converted_hours= 3600*hours; //there are 3600 seconds in 1 hr
    
    cout << hours << " equals " << total_converted_hours << " seconds ";
    
    return 0;
    
    }
    }
    
    int convert_to_secs(int minutes, int hours)
    {
    int total_converted_minutes;
    int total_converted_hours;
    
    total_converted_minutes= 60*minutes;
    
    total_converted_hours= 3600*hours;
    
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    For starters, to help you understand the flow of control in your program and the scope of your variables, indent it properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just because you use variables with the same names in your main function and your convert function does not mean that they are related in any way, shape, or form.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    Quote Originally Posted by tabstop View Post
    Just because you use variables with the same names in your main function and your convert function does not mean that they are related in any way, shape, or form.
    Ok so even though I use the same variables in the convert function it doesn't mean they go back to the main()?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by tigerfansince84 View Post
    Ok so even though I use the same variables in the convert function it doesn't mean they go back to the main()?
    All variables declared in a function are completely inaccessible outside that function (so anything declared in main is not visible in convert, and anything declared in convert is not visible in main). So you have two variables named total_converted_minutes, but they're never both defined at the same time.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    Question

    Quote Originally Posted by tabstop View Post
    All variables declared in a function are completely inaccessible outside that function (so anything declared in main is not visible in convert, and anything declared in convert is not visible in main). So you have two variables named total_converted_minutes, but they're never both defined at the same time.
    I see. So if they're not related how can I use the results from the function to apply to the loop in main? Or is that not possible?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Functions return answers, using "return".

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    Quote Originally Posted by tabstop View Post
    Functions return answers, using "return".
    So I'd need to write two different functions. One for minutes and then one for hours or can I return both from the same function using two "returns"?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Each function returns one answer.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Also, your "convert" function doesn't have a parameter for seconds, which can just be added once you convert the hours and minutes to seconds. Also, you are supposed to have a for loop to do 3 iterations (i = 0; i < 3; i++0 ), then get the hours, minutes, and seconds from user input, then call the "convert" function to return ONE value in seconds.

    Honestly, if you can't follow these simple directions, I think programming is not a viable career choice for you.
    Last edited by kcpilot; 11-12-2008 at 01:39 PM. Reason: typo correction

Popular pages Recent additions subscribe to a feed