gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Write a program that gets input from the user until user enters -1, and display the…
- User
Write a program that gets input from the user until user enters -1, and display the output. Use for loop. in C++.
- Assistant
Sure! Here's a C++ program that gets input from the user until they enter -1, and then displays the output using a for loop:
cpp#include <iostream> using namespace std; int main() { int inputNum; cout << "Enter numbers (-1 to stop): " << endl; // Getting input until user enters -1 for (;;) { cin >> inputNum; // Check if user has entered -1 if (inputNum == -1) { break; } // Displaying the input number cout << "You entered: " << inputNum << endl; } cout << "Program ended." << endl; return 0; }In this program, we use a
for(;;)loop which creates an infinite loop. Inside the loop, we get input from the user usingcinand check if the input number is equal to -1 using anifstatement. If the user enters -1, we use the statement to exit the loop. Otherwise, we display the input number using . Finally, we display "Program ended" and return 0 to indicate successful program execution.