1

Just started to learn C++ and i'm having an issue with getting a function to loop... not really sure if i'm even doing it right! Any help would be greatly appreciated.

To give some context, i'm trying to build a simple degrees to farenheit converter which takes a user input for the value in degrees and outputs a value in farenheit. Also, like in python where you can use: time.sleep() to set a delay between messages, can you do that in C++?

Here's what i've managed to do so far:

#include <iostream>
using namespace std;
//-------------------------------------------------

void DegreesToFarenheit()
{
     //Declaration
    float Degrees, Farenheit;

    //User Prompt
    cout << "Please Enter a Temperature in Degrees: " << endl;
    cin >> Degrees;
    cout << "" << endl;
    cout << "" << endl;

    //Program
    Farenheit = (((Degrees * 9)/5) + 32);
    cout << Degrees << " Degrees" << " is " << Farenheit << " Farenheit";
    cout << "" << endl;

}
char RepeatPrompt()
{
    char Ans;
    cout << "Would you like to enter a new value? ";
    cin >> Ans;
    cout << "" << endl;
    if(Ans = "y" or "Y")
        {DegreesToFarenheit();}
    else if(Ans = "n" or "N")
        {return 0;}
    else
        {main();}
}

int main()
{
    cout << "Degrees To Farenheit Converter V1.0" << endl;
    cout << "----------------------------------------" << endl;
    DegreesToFarenheit() ;
    RepeatPrompt() ;
    return 0;
}
MistUnleashed
  • 309
  • 1
  • 3
  • 15

1 Answers1

4

In C++ there are 3 loops.

while

do while

for

You want to look at your main method as the starting point of the program - and also view it as the first control level. From there you should delegate out to methods that manage the runtime of the program. If you want to reuse a body of code you'll want to use a loop and call it again. Your code example is similar to recursion, but isn't the right implementation of it nor the right time to use it. Recursion can be a powerful tool to simplify complex iterative algorithms, but doesn't fit all cases that act like a loop. It doesn't fit here.

In your case, do while seems fitting. Do also note that developers have style in their coding preference, and technically any loop can be used with some finesse.

EDIT I did a little bit of code cleanup. Much more could be done of course. Note that your teacher/online tutorial likely shows variable declarations grouped together at the start of a method. That is an old carryover from c days, is not necessary, and I find it messy. Keep the variables close to their usage. At the point you feel you're declaring too many variables, consider breaking your function apart.

void DegreesToFarenheit()
{
  cout << "Please Enter a Temperature in Degrees: ";

  float degrees;
  cin >> degrees;

  float farenheit = (((degrees * 9)/5) + 32);
  cout << degrees << " Degrees is " << farenheit << " Farenheit";
  cout << endl;
}

bool RepeatPrompt()
{
  cout << "Would you like to enter a new value? ";

  char ans;
  cin >> ans;

  cout << endl;

  return ans == 'y' || ans == 'Y';
}

int main()
{
  do
  {
    DegreesToFarenheit();
  } while(RepeatPrompt());

  return 0;
}
Community
  • 1
  • 1
payo
  • 4,501
  • 1
  • 24
  • 32
  • Are there any easy to understand articles on while/for/do/if loops? Also is `Void` the correct way to define a function? – MistUnleashed Mar 19 '14 at 18:56
  • 1
    @MistUnleashed: You really need a good beginner book. See http://stackoverflow.com/q/388242/10077 – Fred Larson Mar 19 '14 at 18:59
  • @MistUnleashed mouse over the loop types in my answer, those are links. Perhaps I should change the formatting of my answer. – payo Mar 19 '14 at 19:01
  • instead of using `||` for `or` is there any way to do something like: `.lower()` (something we have in python) – MistUnleashed Mar 19 '14 at 22:44
  • 1
    @MistUnleashed consider making new stackoverflow questions rather than using comments as a medium for new questions. But yes, you can lower a char using tolower http://www.cplusplus.com/reference/cctype/tolower/. Also, if my answer resolves your question, feel free to accept it as the answer (check mark on left) – payo Mar 20 '14 at 00:49