1

My program runs, but does not actually do what I want it to do. It prompts for start size, then prompts for end size, and then that's it. I want it to then use the inputted numbers to calculate how many years it would take but I can't seem to get it to do that. Thanks in advance for any suggestions!

My code is as follows:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    // TO DO: Prompt for start size
    int start;
    do
    {
        start = get_int("Start Size: ");
    }
    while (start < 9);

    // TO DO: Prompt for end size
    int end;
    do
    {
        end = get_int("End Size: ");
    }
    while (end <= start);

    // TO DO: Calculate number of years until we reach threshold
    int years;
    do
    {
        years = (start + start/3 - start/4);
    }
    while (end > start);

    // TO DO: Print number of years
    printf("Years:%i\n", years);
}
Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32
  • 2
    Please reformat (remove empty lines, indent etc). Also, be more precise of what output you are getting and what are expecting. Also, you should provide us with self-contained example. get_in() is not defined. – Allan Wind Mar 28 '21 at 19:56
  • 5
    Your last loop will run forever, since you never modify either `start` or `end` inside of it – UnholySheep Mar 28 '21 at 19:58
  • 1
    Suggest you learn to do basic debugging so that you can find such issues yourself. Run your program in a debugger and step through it line by line, examining the variable values as it runs. – kaylum Mar 28 '21 at 19:59
  • 1
    Don't you mean `int years = 0;` and `start = (start + start/3 - start/4);` followed by `years = years + 1;`?? – David C. Rankin Mar 28 '21 at 20:50
  • Thanks guys, I appreciate the suggestions. I don't know what I mean...*sigh*. I'm VERY new to the programming world, and this is the first assignment I've worked on. I used the help50 tool and have been modifying and checking again...for many hours. I've finally got it to calculate the problem, but it doesn't pass all the tests yet. –  Mar 29 '21 at 00:05

1 Answers1

2

As @kaylum suggests, you would easily see what's wrong with your program if you were to step through it, using a debugger.

Here are two Stackoverflow questions about debugging on Linux and on Windows:

although I am guessing your course staff has suggested some development environment which itself allows for debugging, possibly a bit differently than the suggestions in those questions.

If you were to debug the program, stepping through individual commands, you would notice that execution keeps iterating the do ... while (end > start); loop, as @UnholySheep notes. Then you would think "wait, why isn't it exiting the loop?" and you would notice that you only change the years value, while your loop exit condition regardings end and start, which don't change.

Also, and for future reference - most StackOverflow users expect question askers to perform their "due diligence", making reasonable efforts to figure out their problems before asking us for a solution. Now that you know about debuggers - please use one before asking "Why does my program not do what I expected".

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Thanks for your explanation, this is all new to me and I have no idea what I'm doing. I have been modifying and running this program for many hours and honestly did not know how else or where else to ask for help. This is week 2 of an online course I'm taking that does not have the opportunity to ask for help. Do you have a better suggestion for where I can ask these questions? –  Mar 29 '21 at 00:08
  • @ladyviper92: You should ask your course staff for help, when you're stuck. That's what they're there for, right... Also, consult your fellow students. – einpoklum Mar 29 '21 at 08:55