In this digital age, weather forecast has become one of the important parts of our lives. Through weather forecasts, we can understand future weather changes and prepare accordingly. Nowadays, weather query programs have become one of the important tools in our daily lives.
If you want to learn how to create a simple weather query program through a programming language, then this article will provide you with some useful guidance. This article will introduce how to use C to write a weather query program that can output weather information for various cities. This article will introduce how to write the code, how to add cities and their corresponding weather information, and how to make the program run normally.
Preparation work
Before writing code, you need to do some preparation work, such as installing IDE, etc. In this article, we will use CodeBlocks as our IDE. If you haven't installed CodeBlocks yet, please go to its official website to download and install it. Once installed, open CodeBlocks IDE and create a new project.
Create a new project
Creating a new project is easy. In the CodeBlocks window that opens, select "File" -> "New" -> "Project". Then, select "Console Application" and make sure your selected programming language is C.
Next, please select the folder location where you want to save your project. Then you need to specify a project name. After specifying a project name, click Next.
Next, you need to choose the compiler you want to use. Please make sure that the compiler you choose matches the compiler already installed on your computer. If you are not sure which compiler you should choose, choose the default option and proceed.
Add code
After creating a new project, you need to add code to implement your weather query program. In this article, we will only provide a basic code example that you can change and extend according to your needs.
The following is a basic code example:
// C program to display weather details of cities
using namespace std;
int main(){
char city[20]; cout<<"Please enter the name of city: "; cin>>city; if(strcmp(city,"Beijing") == 0) cout<<"Temperature: 20 degrees Celsius
Humidity: 50 %"< Humidity: 70%"< Humidity: 80%"< Humidity: 60%"< } In this basic code example, the program first prompts the user to enter the name of a city. The program then compares the city names to derive the correct weather information. If the user enters an unknown city name, the program will output an error message. Add more cities You can modify the code like below to add more cities: // C program to display weather details of cities using namespace std; int main(){ Humidity: 50%"< Humidity: 70%"< Humidity: 80%"< Humidity: 60%"< Humidity: 90%"< Humidity: 40%"< Humidity: 45%"< } In this modified code, we added 3 cities : Shenzhen, Hangzhou and Tianjin. You can add more cities this way. Execute the program In the console window, the program will prompt the user to enter the name of the city. According to your For the entered city name, the program will output the corresponding weather information. Summary The above is the detailed content of How to write a simple weather query program in C++?. For more information, please follow other related articles on the PHP Chinese website!else if(strcmp(city,"Shanghai") == 0)
cout<<"Temperature: 25 degrees Celsius
else if(strcmp(city,"Guangzhou") == 0)
cout<<"Temperature: 28 degrees Celsius
else if(strcmp(city,"Chongqing") == 0)
cout<<"Temperature: 22 degrees Celsius
else
cout<<"Sorry, we do not have weather information for this city."<<endl;
return 0;
In the previous code, we only provided weather forecasts for the four cities of Beijing, Shanghai, Guangzhou and Chongqing. If you want engineering effects, you can add more cities and their corresponding weather information. include
include
include
include
char city[20];
cout<<"Please enter the name of city: ";
cin>>city;
if(strcmp(city,"Beijing") == 0)
cout<<"Temperature: 20 degrees Celsius
else if(strcmp(city,"Shanghai") == 0)
cout<<"Temperature: 25 degrees Celsius
else if(strcmp(city,"Guangzhou") == 0)
cout<<"Temperature: 28 degrees Celsius
else if(strcmp(city,"Chongqing") == 0)
cout<<"Temperature: 22 degrees Celsius
else if(strcmp(city,"Shenzhen") == 0)
cout<<"Temperature: 30 degrees Celsius
else if(strcmp(city,"Hangzhou") == 0)
cout<<"Temperature: 17 degrees Celsius
else if(strcmp(city,"Tianjin") == 0)
cout<<"Temperature: 18 degrees Celsius
else
cout<<"Sorry, we do not have weather information for this city."<<endl;
return 0;
After you finish editing the code, you need to execute this program to see if it is running normally. To do this, please Press the F9 key or select the "Compile and Run" option. Then, you will see the console window pop up on the screen.
In this article, we introduced how to use C to write a weather query program. We discussed how to add cities and their corresponding weather information, and how to write code to make the program run properly. Whether you are a beginner or a developer who already has some programming experience, we believe this article can help you get started quickly and create a program that can provide useful information to your users. Informational weather query program.