How come my code wont show pct values

trying to get my code to display the percentage

Page 1 of 1

7 Replies - 813 Views - Last Post: 16 August 2008 - 01:02 PM Rate Topic: -----

#1 zartek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-May 08

How come my code wont show pct values

Posted 15 August 2008 - 08:25 PM

im trying to get my program to display percentages as well as the count i have programmed before. I thought i had it right, but it isnt working properly, can anyone help?

ZG



#include <cstdlib>
#include <iostream>

using namespace std;

main()
{
/*
 float a = 6; 
 float b = 5;
 float x;
 x==a/b;
 
 cout << x << endl; */
 
 
   
	float made = 0;
	float total = 0;
	float number = 0;
	
	float pct = made/total;
	
	do
	  {
	cout << "Enter number:" << endl;
	cin >> number;
	
	
	if (number==1)
	{
	made==made++; total==total++; 
	
   
	
			 cout << " You are " << made <<  " of " << total << endl;
			 
			 cout << pct << " % " << endl;
	}
	else
	{
	   total==total++;
			cout << " You are " << made << " of " << total << endl;
			cout << pct << "%" << endl;
	}
}

	while(true);

system("pause");//i know im not suppose to use this but my program wont stay open if i dont. cin.get() doesnt works
return 0;

}







Is This A Good Question/Topic? 0
  • +

Replies To: How come my code wont show pct values

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: How come my code wont show pct values

Posted 15 August 2008 - 08:36 PM

you calculate pct in the beginning as 0/0

Put it after you have determined what the totals are first--division by zero is bad!
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How come my code wont show pct values

Posted 15 August 2008 - 08:36 PM

float made = 0;
	float total = 0;
	float number = 0;
	
	float pct = made/total;


if made = 0 and total = 0 then made/total is division by zero.

don't use the formula until you need it. You can declare the variable befor you use it, but you can't use the formula until made and total have values.
Was This Post Helpful? 0
  • +
  • -

#4 zartek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-May 08

Re: How come my code wont show pct values

Posted 15 August 2008 - 09:06 PM

View PostNickDMax, on 15 Aug, 2008 - 08:36 PM, said:

float made = 0;
	float total = 0;
	float number = 0;
	
	float pct = made/total;


if made = 0 and total = 0 then made/total is division by zero.

don't use the formula until you need it. You can declare the variable befor you use it, but you can't use the formula until made and total have values.

thanks for the valuable tip on using the formula until i need it. one other thing though, how do i get the compiler to show me the zero in the .50 or anything with a zero. Basically im trying to get just a 2 digit percentage displayed.


thanks again for the help

Z

This post has been edited by zartek: 15 August 2008 - 09:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How come my code wont show pct values

Posted 16 August 2008 - 06:38 AM

You want to look into the include <iomanip>. This has a set of objects that can be used to format your output.

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

int main() {
	double pi = 3.14159265;
	for (int i = 0; i < 8; ++i) {
	    cout << "pi = " << setprecision(i) << pi << endl;
    }
	for (int i = 1; i < 8; ++i) {
	    cout << "pi = " 
             << setprecision(i)
			 << setw(8) 
			 << pi << endl;
    }
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: How come my code wont show pct values

Posted 16 August 2008 - 10:49 AM

View Postzartek, on 15 Aug, 2008 - 10:25 PM, said:

system("pause");//i know im not suppose to use this but my program wont stay open if i dont. cin.get() doesnt works
return 0;

}



Try getchar(); instead of sysem("pause");
There is a discussion about holding the execution window open here.
Was This Post Helpful? 0
  • +
  • -

#7 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: How come my code wont show pct values

Posted 16 August 2008 - 12:03 PM

off topic
What about cin.get() to hold the window open?

:)
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How come my code wont show pct values

Posted 16 August 2008 - 01:02 PM

off topic again...

you know I just use system("pause"); because it is easy to search for and replace when I am done debugging my code. cin.get() is not great because cin gets into an error state and then cin.get() does not pause to even tell you something is wrong.... so you have to make this somehting like:

cin.clear();cin.ignore(numeric_limits<streamsize>::max(), '\n' );cin.get()

but now you have to remember to include the #include <limits> line... and sometimes you are not in the right name space so you have to make your template into:
std::cin.clear();std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );std::cin.get()

Then that is getting too long for a single line!

So... I just use system("pause"); at the end of my programs while debugging. Of course I would NEVER put it inside the logic of my code!!!

Thats like use system("Sort < mydata.dat >mysorteddata.dat") to do a sort for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1