Having trouble with this C++ program.  | | |
August 21st, 2005, 11:56 PM
|
#1 (permalink)
| | Junior Member
Join Date: Aug 2005
Posts: 7
| Having trouble with this C++ program.
Hey.
I'm fairly new to C++, and I was trying to create a program that could figure out something for a roleplaying game I play in.
Basically, everything is working fine right now, the only problem is that I wanted it to keep looping until the user typed in QUIT. The only problem I'm having is that I'm not sure how to get it to start all over again?
Also.. would anyone know how I could get it to show proper percentages? If not. That's okay too.
Here's the code..
// Program to determine chance of missing.
#include <cstdio>
#include <cstdlib>
#include <iostream>
// AGI / DEX = sum converted to % / 2 = final result
// 3 AGI, 1 DEX: 3 / 1 = 3(300%) / 2 = 150% chance to miss, cut down to 99%
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
cout <<"This Program was created by the Android Hunter\n"
<<"Cyrus. It's used to quickly determine your chance\n"
<<"of missing. Enjoy!\n\n"
<<"Simply type in QUIT to exit the program.\n\n";
double AGI;
cout << "Enter Your AGI:";
cin >> AGI;
// Now we have to get the ATP!
double DEX;
cout << "Enter Defenders DEX:";
cin >> DEX;
// Now we'll figure out the chance of missing.
double percenty;
double thelastone;
thelastone = (AGI/DEX)/2;
cout << "Your chance of missing is:";
cout << thelastone << endl;
system("Pause");
return 0;
}
Last edited by DarkShadowOZE : August 21st, 2005 at 11:59 PM.
|
| |
August 22nd, 2005, 12:17 AM
|
#2 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
to get it to loop, just wrap it in a while loop
this would look like: Code: bool quit = false;
while(!quit)
{
// code you want to loop
} quit is some boolean variable that you would set whenever the user types "quit". You can check this with an if statement: Code: if(cinvariable == "quit")
quit = true; Just put this code after every cin statement (if you want the user to be able to type quit at any time).
to show the percent, just multiply by 100. |
| |
August 22nd, 2005, 12:41 AM
|
#3 (permalink)
| | Junior Member
Join Date: Aug 2005
Posts: 7
|
Thanks.
Okay, percents work now. Anyway I can make it show a '%' after it posts the number?
It loops, good.
The only problem I have now, is that when I put the if statement after those, I get this error.
int main(int, char**)':
cinvariable' undeclared (first use this function) |
| |
August 22nd, 2005, 02:18 AM
|
#4 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
cinvariable is not a real variable...replace it with whatever variable you used with your cin variable (i.e. AGI and DEX).
To show the percent sign change Code: cout << thelastone << endl; to Code: cout << thelastone << "%" << endl; |
| |
August 22nd, 2005, 08:25 AM
|
#5 (permalink)
| | Junior Member
Join Date: Aug 2005
Posts: 7
|
Okay, just getting this random error now for when I replace the cinvariable with AGI/DEX
invalid operands of types `double' and `const char[5]' to binary `operator=='
After that, it's all done. Thanks alot. |
| |
August 22nd, 2005, 10:55 AM
|
#6 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
Sounds like you have this coded:
if (DEX == "quit") // DEX is the double and "quit" is the const char[5] as "q-u-i-t-\0"
You'll have to use a number as the loop terminator if you're comparing to DEX. Can you use zero? Negative 1? |
| |
August 22nd, 2005, 10:58 AM
|
#7 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
Also, I beleive you can cast DEX to a char value...
if ( (char) DEX == "quit") // not tested, should work
Edited...
Uh, probably not...casting DEX to a single char..should be:
if ( (char) DEX = 'Q') //now quit!!! |
| |
August 22nd, 2005, 03:18 PM
|
#8 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
Oops...my mistake for missing that DEX was a double...
The ascii value for 'Q' could be a valid value, so it won't make a good terminating character.
I think you will need some other way of terminating the program.
One idea would be to have a third prompt asking if the user would like to input more values: Code: char continue;
cout<<"Would you like to enter another character? (y/n) ";
cin>>continue;
if(continue == 'Y' || continue == 'y')
quit = true; |
| |
August 22nd, 2005, 03:52 PM
|
#9 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
Yeah, that works too. ASCII value of "Q" is 81 decimal and "q" is 113; would a character's AGI or DEX be that high? |
| |
August 22nd, 2005, 05:04 PM
|
#10 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
Probably not...but ya never know. One of my friend who DMs sometimes has pulled some crazy stuff like that (although I think just to be funny). Plus it's just good coding practice. |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |