What do I need to do in order to get my program to print out two decimal places?

#include "stdafx.h"
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
 char movie[50];
 int adult_ticket_price, child_ticket_price;
 int adult_ticket_sold, child_ticket_sold;
 int percentage_of_gross_amount_donated;
 
  cout <<"Enter The Movie Name: ";
 cin >> movie;
 cout << "";
 cout <<"Enter The Adult Ticket Price: $";
 cin >> adult_ticket_price;
 cout << "";
 cout <<"Enter The child ticket price: $";
 cin >> child_ticket_price;
 cout << "";
 cout <<"Enter The Number of Adult Tickets Sold: ";
 cin >> adult_ticket_sold;
 cout << "";
 cout <<"Enter The Number of Child Tickets Sold: ";
 cin >> child_ticket_sold;
 cout << "";
 cout <<"Enter The Percentage of Gross Amount Donated: ";
 cin >> percentage_of_gross_amount_donated;
 cout << "";
 cout <<"Number of Tickets Sold: " << ((adult_ticket_sold)+(child_ticket_sold))<< endl;
 cout <<"Gross Amout is: $" << (((adult_ticket_sold)*(adult_ticket_price))+((child_ticket_sold)*(child_ticket_price))) << endl;
 cout <<"Amount Donated is: $" << ((((adult_ticket_sold)*(adult_ticket_price))+((child_ticket_sold)*(child_ticket_price)))*((percentage_of_gross_amount_donated)*(.01)));
 cout <<" "<<endl;
 return 0;
}

i need to be able to put in 10.75 for the prices, it wont let mr with out going crazy

"int" isnt for decimal variables , its for whole numbers

you should use the "float" datatype for decimals

e.g

float pricea, priceb;

There is no easy way to limit output to a given number of decimal places in C/C++ that I've seen. After wrangling through a rather lengthy discussion previously, I'd store the whole number part of the given number as an int through casting, then multiply the given number by 100, casting the new number to a second int. I'd then take the second int and use modulo 100 to get a third int. Then I'd output the result as cout << firstInt << decimalPoint << thirdInt. I'm happy to hear about any other approaches.

By the way, the consensus of the other discussion was to use a language that has this capability built in rather than a sloppy work around in C/C++. But that may not be an option for you.

how about this

//float price = 765.87590234;
//price = ((int)(price*100))/100;

I don't think the external set of () is needed but not absolutely certain so figured better safe than sorry.

Try this, you should use formatting, that is where the setiosflags and setprecision comes from. I've tried this program and it works fine for me. Also, when using formatting, you must add

#include <iomanip>

as a header file. Thanks,
Matt

#include <iostream>
#include <ctype.h>
#include <iomanip>
using namespace std;
int main()
{
 char movie[50];
 float adult_ticket_price, child_ticket_price;
 float adult_ticket_sold, child_ticket_sold;
 float percentage_of_gross_amount_donated;
 
 cout <<"Enter The Movie Name: ";
 cin >> movie;
 cout << "";
 cout <<"Enter The Adult Ticket Price: $";
 cin >> adult_ticket_price;
 cout << "";
 cout <<"Enter The child ticket price: $";
 cin >> child_ticket_price;
 cout << "";
 cout <<"Enter The Number of Adult Tickets Sold: ";
 cin >> adult_ticket_sold;
 cout << "";
 cout <<"Enter The Number of Child Tickets Sold: ";
 cin >> child_ticket_sold;
 cout << "";
 cout <<"Enter The Percentage of Gross Amount Donated: ";
 cin >> percentage_of_gross_amount_donated;
 cout << "";
 cout <<"Number of Tickets Sold: " << ((adult_ticket_sold)+(child_ticket_sold))<< endl;
 cout <<"Gross Amout is: $" << ((adult_ticket_sold*adult_ticket_price)+(child_ticket_sold*child_ticket_price)) << endl;
 cout <<"Amount Donated is: $" << setiosflags(ios::fixed)<< setiosflags(ios::showpoint)<< setprecision(2)
   << ((adult_ticket_sold*adult_ticket_price)+(child_ticket_sold*child_ticket_price))*(percentage_of_gross_amount_donated/100.0);
 cout <<" "<<endl <<endl;
 return 0;
}

This worked when I tried it out. I wish i could have posted it earlier but it would send email activation code. =/
The comments are in the code. :)
Hope it helped.

#include <iostream>
 #include <string>
using namespace std;
int main()
{
 string movie;
 double adult_ticket_price, child_ticket_price; //Changed from int to double so it can take decimal numbers
 int adult_ticket_sold, child_ticket_sold;
 int percentage_of_gross_amount_donated;
 
 cout <<"Enter The Movie Name: ";
 getline(cin,movie); //Changed it so your movie name could contain more than one word
 cout << "";
 cout <<"Enter The Adult Ticket Price: $";
 cin >> adult_ticket_price;
 cout << "";
 cout <<"Enter The child ticket price: $";
 cin >> child_ticket_price;
 cout << "";
 cout <<"Enter The Number of Adult Tickets Sold: ";
 cin >> adult_ticket_sold;
 cout << "";
 cout <<"Enter The Number of Child Tickets Sold: ";
 cin >> child_ticket_sold;
 cout << "";
 cout <<"Enter The Percentage of Gross Amount Donated: ";
 cin >> percentage_of_gross_amount_donated;
 cout << "";
 cout <<"Number of Tickets Sold: " << ((adult_ticket_sold)+(child_ticket_sold))<< endl;
 cout <<"Gross Amout is: $" << (((adult_ticket_sold)*(adult_ticket_price))+((child_ticket_sold)*(child_ticket_price))) << endl;
 cout <<"Amount Donated is: $" << ((((adult_ticket_sold)*(adult_ticket_price))+((child_ticket_sold)*(child_ticket_price)))*((percentage_of_gross_amount_donated)*(.01)));
 cout <<" "<<endl;
 system("pause"); //So when you run it it would pause the screen so you can read it.
 return 0;
}
//I used Visual C++ and it worked fine for me.
//Hope it helpped.

Hey I made a C++ program that does Taxes, I had to use Stream and float, hope this helps

// The Taxer
// By: David Seviour
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std;
int main ()
{
  string mystr;
  int price=0;
  float tax=0;
  loop:
  system("Title Taxer");
  system("color 0c");
  cout << "The Taxer\n" << endl;
  system("color 08");
  cout << "(Example for $10.50 you would type 10.50)" << endl;
  cout << "Enter price: ";
  getline (cin,mystr);
  stringstream(mystr) >> price;
  cout << "\n\n";
  cout << "(Example for %14 tax you would type 14)\n";
  cout << "Enter The Tax: ";
  getline (cin,mystr);
  stringstream(mystr) >> tax;
  tax = tax / 100;
  tax = tax + 1;
  cout << "\n\n";
  cout << "\n\n";
  system("color 14");
  Sleep(100);
  system("color 29");
  Sleep(100);
  system("color 2b");
  Sleep(100);
  system("color 3a");
  Sleep(100);
  system("color 08");
  Sleep(100);
  system("cls");
  system("echo.");
  system("echo.");
  system("echo.");
  system("echo.");
  system("color 0c");
  cout << "Total price: $" << price*tax << endl;
  cout << "\n";
system("PAUSE");
system("cls");
goto loop;
  return 0;
}

hey also if u want the decimals to be eaven greater then that you could use double instead of float just incase u didnt know

This article has been dead for over six months. Start a new discussion instead.