Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why this code is sowing error?

+6 votes
asked Sep 16, 2023 by Akib Zawad (180 points)
/*I want to make a program of Temperature conversion.But when I tried to run this part of my program it showed error,what is wrong
with my code of taking input? */

#include <iostream>
int main()
{
   std::cout<<"Welcome to Temperature Conversion!"<<std::endl<<"Please Select Your Conversion Type:"<<std::endl
   <<"Press 1 to convert temperature in Fahrenheit to Kelvin"<<std::endl<<"Press 2 to convert temperature in Kelvin to Celsius"
   <<std::endl<<"Press 3 to convert temperature in Fahrenheit to Celsius"<<std::endl;
   
   int temperature = 0;
   std::cin>>temperature>>std::endl;
  
  return 0;
}

3 Answers

0 votes
answered Sep 16, 2023 by ABDUL EJAZAHMAD (150 points)


   std::cin>>temperature>>endl;

You can't use endl in cin 
Solution:
std::cin>>temperature;
std::cout<<endl;

  
Hope this is Helpful for you

0 votes
answered Sep 17, 2023 by sakthii A (140 points)

The issue in your code is with the std::cin statement. You should not use >> followed by std::endl when reading input with std::cin. std::endl is used to insert a newline character and flush the output stream. When reading input, you should use >> without std::endl. Here's the corrected code:

#include <iostream>

int main() {
    std::cout << "Welcome to Temperature Conversion!" << std::endl
              << "Please Select Your Conversion Type:" << std::endl
              << "Press 1 to convert temperature in Fahrenheit to Kelvin" << std::endl
              << "Press 2 to convert temperature in Kelvin to Celsius" << std::endl
              << "Press 3 to convert temperature in Fahrenheit to Celsius" << std::endl;

    int temperature = 0;
    std::cin >> temperature;

    return 0;
}

With this change, your code should work as expected, allowing the user to input a number for the temperature conversion type.

0 votes
answered Sep 19, 2023 by Gulshan Negi (1,580 points)

The isssue is with your code, to fix this error and execute it you need to replace your code with below one.

#include <iostream>

int main() {
    std::cout << "Welcome to Temperature Conversion!" << std::endl
              << "Please Select Your Conversion Type:" << std::endl
              << "Press 1 to convert temperature in Fahrenheit to Kelvin" << std::endl
              << "Press 2 to convert temperature in Kelvin to Celsius" << std::endl
              << "Press 3 to convert temperature in Fahrenheit to Celsius" << std::endl;

    int temperature = 0;
    std::cin >> temperature; // Remove >>std::endl

    return 0;
}

Thanks

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...