0

I am new here and a student in a basic programming class at community college. I am having trouble understanding why this program simply closes instead of displaying the output.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int quantity;   // contains the amount of items purchased 
    float   itemPrice;        // contains the price of each item
    float   totalBill;        // contains the total bill.
    
    cout << setprecision(2) << fixed << showpoint;  // formatted output 
    cout << "Please input the number of items bought " << endl;
    cin >> quantity;
    // Fill in the prompt to ask for the price.
    cout << "Please enter Item Price" << endl;
    // Fill in the input statement to bring in the price of each item.
    cin >> itemPrice;
    // Fill in the assignment statement to determine the total bill.
    totalBill = quantity * itemPrice;            
    // Fill in the output statement to print total bill, // with a label to the screen.
    cout << "The bill is $" 
         << totalBill << endl;
         
return 0;
}
tadman
  • 208,517
  • 23
  • 234
  • 262
  • It probably displays the output then ends and after ending most operating systems will not keep program windows around. – drescherjm Feb 09 '21 at 22:29
  • Most likely this is a duplicate of a question that has been asked several thousand times. – drescherjm Feb 09 '21 at 22:31
  • Ah, thank you drescherjm. Is there anything I could add to allow the output to be seen? – PotatoSpike Feb 09 '21 at 22:31
  • Here is one such duplicate: [https://stackoverflow.com/questions/4164077/c-on-windows-the-console-window-just-flashes-and-disappears-whats-going-on](https://stackoverflow.com/questions/4164077/c-on-windows-the-console-window-just-flashes-and-disappears-whats-going-on) – drescherjm Feb 09 '21 at 22:34
  • 1
    Depending on what OS you use and how you are building or if you are using an IDE there may be other solutions. Like putting a breakpoint at the last line of `int main()` – drescherjm Feb 09 '21 at 22:36
  • Once I knew the issue I was able to find that the solution was system("pause"). A simple answer but I did not know the question. Thank for the timely help and humoring this question. – PotatoSpike Feb 09 '21 at 22:36
  • I had a little difficulty finding a good duplicate because there are a lot of similar questions but some are OS or IDE specific and the question made no mention of either. – drescherjm Feb 09 '21 at 22:38
  • What if your system doesn't have a pause command? What if the pause command is replaced with a program that formats your hard drive? – user4581301 Feb 09 '21 at 22:51

0 Answers0