logo elektroda
logo elektroda
X
logo elektroda

C++ Program: Temperature Conversion with T Key Repeating & N Key Closing (Celsius to Fahrenheit)

kakus777 20474 12
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 8636185
    kakus777
    Level 10  
    Hello, I have a program that converts degrees Celsius to degrees Fahrenheit and I have to put a query into it at the end after the calculations. When I press the T key, it is to repeat the calculations, and if the N key is to close the program.

    I put a program and ask for help

    [code:1:6854be4f2d]
    #include
    #include
    #include
    #include
    using namespace std;

    int main()
    {
    float c;
    float f;
    cout > c;
    cout
  • ADVERTISEMENT
  • #2 8636329
    Dżyszla
    Level 42  
    heard a colleague about the loops?
  • ADVERTISEMENT
  • Helpful post
    #3 8636708
    Mad.
    Level 18  
    As above, read about loops first and then try something like this:
    do{
    // wstaw tutaj swój program
    // dopisz sprawdzanie klawisza, wynik zapisuj do zmiennej
    
    }while() // w () sprawdzaj wartość zmiennej
  • #4 8638670
    kakus777
    Level 10  
    I understand loops but I have such a task to do to make loops with the keys from the keyboard t and n. And I do not know how to assign it; /

    Quote:
    Write a program to convert temperature in degrees Celsius to temperature in degrees Fahrenheit according to the formula TC = 5/9 (TF-32). Each time the calculations are made, the user is asked "Should I repeat the calculations (Y / N)?" If the answer is "T" (the letter T is pressed on the keyboard), the calculations continue. If the user presses the N key, the program exits. The program should only respond to the T and N keys.
  • #5 8638773
    walek33
    Level 29  
    I would like the use more if .
  • #6 8638972
    kakus777
    Level 10  
    but how to assign it because I don't know how; /
  • #7 8639226
    Anonymous
    Anonymous  
  • ADVERTISEMENT
  • #8 8639311
    przemo_wielki
    Level 24  
    This can only be done with if, but without labels, it will not happen because "The program should only respond to the T and N keys". Listen to the advice of colleagues, do it on the while loop because you are unnecessarily trying to complicate such a banal code.
  • #9 8639383
    kakus777
    Level 10  
    char Czykoniec;

    what should be in the final condition ???

    while (Czykoniec == 'T');
  • ADVERTISEMENT
  • #10 8639392
    Anonymous
    Anonymous  
  • #11 8642769
    kakus777
    Level 10  
    someone could correct me, because I click a key on the keyboard when calculations are made and the program turns off; /

    [code:1:a2c5fdd813]#include
    #include
    #include
    #include
    using namespace std;

    int main()
    {
    char Czykoniec = 'T';
    do {
    float c;
    float f;
    cout > c;
    cout
  • #12 8642800
    Dżyszla
    Level 42  
    to my knowledge this program should not compile at all. What is this incomplete do loop, and then two while loops? I don't understand ...

    First, by doing a do..while loop, you don't need to initialize the variable's value before the loop. Second - where is getting the key character right before the end of the loop?
  • #13 8642867
    Mad.
    Level 18  
    The program compiles surprisingly ;) What is in red -> does not match.

    down Czykoniec = 'T';
    while (Czykoniec! = 'T' && Czykoniec! = 'N');
    } while (Does the end! = 'T');
    getch ();

    It should be like this:
    do 
    {
       // obliczenia temp.
    
       do 
       {
          // instrukcje:
          // pytanie o powtórzenie programu
          // pobranie klawisza wciśniętego przez użytkownika
       }
       while(warunek1); 
    }
    while(warunek2); 

    Where there is an instruction, you have to read the typed key by the user and save it to the variable Czykoniec. Additionally, just enter the yes / no question there, because if the person gives something other than Y / N, he will see the same question again. The outer do-while loop is fine, so are the conditions.
    As Dżyszla wrote, you do not have to initiate Czykoniec with some value because you will have to get it from the user anyway.

Topic summary

The discussion revolves around a C++ program designed for converting Celsius to Fahrenheit, with specific requirements for user interaction via keyboard inputs. The user seeks assistance in implementing a loop that allows the program to repeat calculations when the 'T' key is pressed and to terminate when the 'N' key is pressed. Various responses suggest using a do-while loop to handle the input and control flow, emphasizing the need to read a character from the keyboard and check its value to determine the next action. Suggestions include initializing a variable to store the key input and using conditional statements to manage the program's execution based on user input.
Summary generated by the language model.
ADVERTISEMENT