home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Having trouble with this C++ program.

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2056
Discussions: 200,936, Posts: 2,379,239, Members: 246,301
Old August 21st, 2005, 11:56 PM   Digg it!   #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.
DarkShadowOZE is offline   Reply With Quote
Old August 22nd, 2005, 12:17 AM     #2 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
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.
originel is offline   Reply With Quote
Old 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)
DarkShadowOZE is offline   Reply With Quote
Old August 22nd, 2005, 02:18 AM     #4 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
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;
originel is offline   Reply With Quote
Old 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.
DarkShadowOZE is offline   Reply With Quote
Old August 22nd, 2005, 10:55 AM     #6 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
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?
Rootstonian is offline   Reply With Quote
Old August 22nd, 2005, 10:58 AM     #7 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
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!!!
Rootstonian is offline   Reply With Quote
Old August 22nd, 2005, 03:18 PM     #8 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
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;
originel is offline   Reply With Quote
Old August 22nd, 2005, 03:52 PM     #9 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
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?
Rootstonian is offline   Reply With Quote
Old August 22nd, 2005, 05:04 PM     #10 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
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.
originel is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
I've got trouble, please help...anyone pitbull64 Graphics Cards and Displays 24 June 17th, 2005 04:21 AM
hardware trouble or software trouble? need help bad... abolith Technical Support 16 February 28th, 2005 03:20 PM
Having Trouble Again njolakoski Technical Support 35 June 1st, 2004 06:25 PM
trouble super gerbil IMO Community 47 October 2nd, 2003 10:55 PM
Trouble... fatal xception IMO Community 27 May 27th, 2002 01:23 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
windows 7 problem (6)
Is It Just Me? (2899)
CPU fan stops spinning randomly (8)
Wireless Televisions. (8)
California Passes Anti-Flat-HDTV Le.. (41)
Obama the Muslim (14)
Is the PSU I received dead? (11)
windows vista security holes (9)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Print spooler problem (13)
Foreign voltage (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
Recent Discussions
CPU fan stops spinning randomly (8)
Partition Magic caused HDD problem (3)
Is the PSU I received dead? (11)
Have you switched yet? (85)
Regular Build (4)
windows 7 problem (6)
Point and Shoot Camera Suggestions. (2)
Modern Warfare 2 freeze (13)
Wireless Televisions. (8)
wireless user (1)
World's largest Monopoly Game using G.. (332)
Ideal cheap graph card for PC-Gaming? (17)
BIOS won't read disk when I try to fl.. (0)
Install XP pro and a Vista laptop ?? (11)
Graphics Card Upgrade Question (1)
favorit (1)
solutions for virtical white lines on.. (1)
Fire in DVD (2)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Wireless Router+Cable Modems and Much.. (0)
Optical Audio A-B Switch (1)
windows vista security holes (9)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)


All times are GMT -4. The time now is 11:59 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28