User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,509 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,161 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 401 | Replies: 11 | Solved
Reply
Join Date: Jul 2008
Posts: 7
Reputation: xxjinseruxx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xxjinseruxx xxjinseruxx is offline Offline
Newbie Poster

C++ Ratio! help please!

  #1  
Jul 23rd, 2008
Hey, basically im trying to create a Kill/Death ratio program where the user types x number of kills and y number of deaths and this creates an answer for ratio " :1" for example " how many kills ? 3388 " "How many deaths? 1237" "Your ratio is 12.129:1" the decimal is needed if possible oh and just one more i need the program to loop [hence the end code, not that it works]

so heres the code im using up to now. and if you need it explaining any more or dont understand please tell me!

// ArenaRatio.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "Converter.h"

using namespace std;

 int x;
 int y;


int main ()

 

{


float x,y, dummy;



float ratio = .046;
//declare variables
	char rerun;

cout<<"Welcome to Easy ArenaRatio finder!\n";

cout<<"\n";

cout<<"\n";

cout<<"\t Please enter the numbers of kills: \n" << endl;
cin >> x;
cout<<"\t Please enter the numbers of deaths: \n" << endl;
cin >> y;

 



cout<<"Your Ratio is: "<< converter(x, y) / ratio << ":1" << endl;



system("PAUSE");

//prompt user if they wish to run the program again
cout << "Would you like to input information for another ratio? <y/n>: ";
cin >> rerun;





return 0;
}
Last edited by xxjinseruxx : Jul 23rd, 2008 at 11:51 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Posts: 232
Reputation: ssharish2005 is on a distinguished road 
Rep Power: 2
Solved Threads: 18
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: C++ Ratio! help please!

  #2  
Jul 23rd, 2008
Ohh man, you need to properly indentent your code. It so difficult to go through with that code.

Identent your code, you might except more help.

And what is '#' char in each line? Dosnt look good!

ssharish
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 858
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 86
vmanes's Avatar
vmanes vmanes is offline Offline
Practically a Posting Shark

Re: C++ Ratio! help please!

  #3  
Jul 23rd, 2008
First, what's with all the extraneous "#' symbols? They're only needed for the include statements.

To allow the user to continue entering data sets, enclose the main process in a loop, such as
  1. char rerun = 'y';
  2. while ( rerun == 'y' )
  3. {
  4. //do the work
  5.  
  6. //ask if player wants another go
  7. }
As to your calculation, where's the converter( ) function? What does it do?
What's the purpose of the ratio variable? In your example, how do come up with 12.129?
I think congressmen should wear uniforms like NASCAR drivers so we could identify their corporate sponsors. (unknown)
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: xxjinseruxx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xxjinseruxx xxjinseruxx is offline Offline
Newbie Poster

Re: C++ Ratio! help please!

  #4  
Jul 23rd, 2008
Indentent? O_O?

Edit : Removed not needed "#"
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: xxjinseruxx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xxjinseruxx xxjinseruxx is offline Offline
Newbie Poster

Re: C++ Ratio! help please!

  #5  
Jul 24th, 2008
Ok Vmanes, the loop works perfect thanks on that one, and to answer the part about "12.219:1" was just and example and doesnt really mean anything i was just explaining what i need the end result to show after you enter how many kills and deaths you have divided by the ratio the exact ratio is 2.74:1 rounded up but when i do the ratios i end up with wrong numbers example

i input 3388 in kills and 1272 in deaths the answer should be about 2.68:1 but if i do it right now with the code i get "1.#INF:1" [ratio set at = 0.000] all i need it to do is divide like a calculator does but with only 0.00 digits or rounded up to the nearest hundreth and not the long amount of digits a calculator shows and stick ":1" at the end of it. and as for the converter

#include "stdafx.h"
#include <iostream>

int converter(int x, int y)
{
	return x / y;
}

nothing really.
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 858
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 86
vmanes's Avatar
vmanes vmanes is offline Offline
Practically a Posting Shark

Re: C++ Ratio! help please!

  #6  
Jul 24th, 2008
You've got two parts to getting the result you want.
First, get the correct numeric value. Your function, as shown above, will not give you the correct result.
int converter(int x, int y)
{
	return x / y;
}
dividing an int by an int results in an int, which is also the type of your return value. You will lose the fractional part of the division's result. This should be
double converter(int x, int y)
{
	return (double)x / y; //cast x to type double, get double result
}
Forget that ratio variable, it's not needed. Especially if you set it to 0.00 !!!

To get a value displayed just to the hundredths, you need to use output formatting.

cout.setf(ios::fixed | ios::showpoint);
cout.precision(2);
cout<<"Your Ratio is: "<< converter(x, y)  << ":1" << endl;

Val
I think congressmen should wear uniforms like NASCAR drivers so we could identify their corporate sponsors. (unknown)
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: xxjinseruxx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xxjinseruxx xxjinseruxx is offline Offline
Newbie Poster

Re: C++ Ratio! help please!

  #7  
Jul 24th, 2008
oh wow!, thanks the new edited code works....you helped alot Vman your awsome
Reply With Quote  
Join Date: Jan 2008
Location: USA East Cost
Posts: 389
Reputation: CoolGamer48 is on a distinguished road 
Rep Power: 1
Solved Threads: 37
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Whiz

Re: C++ Ratio! help please!

  #8  
Jul 24th, 2008
You might want to also add a check in converter() (or somewhere else) to make sure that the number of deaths is not 0.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: xxjinseruxx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xxjinseruxx xxjinseruxx is offline Offline
Newbie Poster

Re: C++ Ratio! help please!

  #9  
Jul 24th, 2008
Right, how do i do that? oh and one more thing.. when it reruns..it does it underneath the old text..is there anyway to make it rerun as though its been opened fresh? thanks alot!
Reply With Quote  
Join Date: Jan 2008
Location: USA East Cost
Posts: 389
Reputation: CoolGamer48 is on a distinguished road 
Rep Power: 1
Solved Threads: 37
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Whiz

Re: C++ Ratio! help please!

  #10  
Jul 24th, 2008
int converter(int x, int y)
{
    if(y == 0)
        return 0;//or -1, or whatever you want to happen instead of a divide by zero	
    return x / y;
}


You could use backspace to get rid of the old text:
for(int i = 0;i < numChars;i++)
    cout << "\b";
numChars is the number of characters you output.

There's a clear command on the Linux kernel that clears the prompt of text. I don't know if there's a Windows equivalent, but you could call it with system();
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:47 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC