While loop condition not working - c++

Well basically the condition for healthtwo causes the program to stop but not healthone for some reason
complete code: http://en.textsave.org/CmN
if (chance<=rando) {
cout << " " << endl;
cout << "Hit! Dealing " << attackp << " Damage!" << endl;
healthtwo=healthtwo-attackp;
}
else {
cout << " " << endl;
cout << "Miss!" << endl;
}
chance=1+rand()%23;
if (chance<=rando) {
cout << "Comp Used " << comattackname << "!" << " Hit!" << " Dealing " << attackcp << " Damage" << endl;
cout << " " << endl;
healthone=healthone-attackcp;
}
else {
cout << "Comp Used " << comattackname << "!" << " Miss!" << endl;
cout << " " << endl;
}
} while (healthone>=0 || healthtwo>=0);

That should be a logical and (&&). After all, you want to check whether the health of both contestants is greater or equal to zero.

Related

C++ array beginner

I have this code that I have been working on for fun, it is as basic as it gets, becuase I am a beginner, and it works fine, but I can't seem to be able to figure out how to make it show the least and the most amount of pancakes ate. Thank you a lot in advance.
#include <iostream>
using namespace std;
int main(){
int pancakes[10];
int x,i;
cout << "Hello user!" << endl;
cout << endl;
cout << "Please enter how many pancakes did each of the 10 people eat:" << endl;
cout << endl;
for (i=0;i<10;i++ ){
cin >> x;
pancakes[i]=x;
}
cout << "1st person ate" << " " << pancakes[0] << " " << "pancakes" << endl;
cout << "2nd person ate" << " " << pancakes[1] << " " << "pancakes" << endl;
cout << "3rd person ate" << " " << pancakes[2] << " " << "pancakes" << endl;
cout << "4th person ate" << " " << pancakes[3] << " " << "pancakes" << endl;
cout << "5th person ate" << " " << pancakes[4] << " " << "pancakes" << endl;
cout << "6th person ate" << " " << pancakes[5] << " " << "pancakes" << endl;
cout << "7th person ate" << " " << pancakes[6] << " " << "pancakes" << endl;
cout << "8th person ate" << " " << pancakes[7] << " " << "pancakes" << endl;
cout << "9th person ate" << " " << pancakes[8] << " " << "pancakes" << endl;
cout << "10th person ate" << " " << pancakes[9] << " " << "pancakes" << endl;
return 0;
}
Since you are a beginner, I will put a simple solution using a loop.
int max = 0;
for(i = 0; i < 10; i++) {
if(pancakes[i] > max) max = pancakes[i];
}
cout << "Most amount of pancakes eaten by a single person: " << max << endl;
You can use min_element and max_element from the standard library to do this:
#include <algorithm>
cout << "The smallest number of pancakes was " << *min_element(pancakes, pancakes + 10) << endl;
cout << "The largest number of pancakes was " << *max_element(pancakes, pancakes + 10) << endl;
Firstly, instead of having around 10 cout's, you can use a loop to print them :
for (i=0;i<10;i++ ){
if(i==0)
cout <<i+1<< "st person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else if(i==1)
cout << i+1<<"nd person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else if(i==2)
cout << i+1<<"rd person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else
cout <<i+1<< "th person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
}
Secondly, you can directly enter values into your array, no need for an intermediate variable x:
for (i=0;i<10;i++ ){
cin >> pancakes[i];
}
For your max and min problem, take two variables, say - max and min. Initialise them to any arbitrary smallest (say 0, if you are not dealing with negative numbers) and largest value (say, INT_MAX) respectively. Alternatively, you can initialise them to the first element of your array.
For finding max and min, you can traverse the entire array, while checking if the elements are greater or lesser than your max and min variables. If they are, then assign them to your variables:
for (i=0;i<10;i++ ){
if(pancakes[i]>max)
max = pancakes[i];
if(pancakes[i]<min)
min = pancakes[i];
}
You can add std::cout inside for loop like below,
for (int i = 0; i < 10; i++ )
{
std::cin >> pancakes[i];
std::cout << i+1 <<"st person ate" << " " << pancakes[i] << " " << "pancakes" << std::endl;
}
int maxpancakes = pancakes[0];
int minpancakes = pancakes[0];
for(int i = 0; i < pancakes.length(); i++ )
{
if( pancakes[i] < minpancakes )
minpancakes = pancakes[i];
if( pancakes[i] > maxpancakes )
maxpancakes = pancakes[i];
}
std::cout << "The smallest pan cake had is :" << minpancakes << std::endl;
std::cout << "The max pan cake had is :" << maxpancakes << std::endl;

Syntax error, also would like to know if i can make this code more efficient

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main()
{
double weight, sgrav, distance, speed, pweight, distanceplanet, travel;
char selection;
string planet;
cout << "Welcome to INTERPLANETARY TRAVEL PROGRAM!" << endl;
cout << "This program enables you to find out your travel time to the planet" << endl;
cout << "you want to travel to as well as your weight on that planet." << endl;
cout << "Please enjoy the program and find the perfect planet for you!" << endl;
cout << endl
<< endl
<< "INTERPLANETARY TRAVEL MENU" << endl
<< "--------------------------" << endl
<< "a) Mercury" << endl
<< "b) Venus" << endl
<< "c) Earth" << endl
<< "d) Mars" << endl
<< "e) Jupiter" << endl
<< "f) Saturn" << endl
<< "g) Uranus" << endl
<< "h) Neptune" << endl
<< "q) quit" << endl
<<endl
<< "Select a planet to travel to or q to quit the program: " << endl;
cin >> selection;
cout << "Please enter your weight (in lbs): " << endl;
cin >> weight;
cout << "Please enter the speed (in mile per hour) that you would like to travel at: " << endl
<< endl;
cin >> speed;
if (selection >= 'a' && selection <= 'h')
{
if (selection == 'a')
{
sgrav = 0.86;
pweight = (weight*sgrav);
planet = "Mercury";
distance = abs(36 - 93);
travel = (distance * speed);
cout << "INTERPLANETARY TRAVEL: " << "Earth to " << planet << endl
<< "--------------------------------------------------" << endl
<< "Your weight on " << planet << ": " << setprecision(2) << pweight << " lbs" << endl
<< endl
<< "Your travel time to " << planet << ":" << endl
<< " In Hours: " << travel << " hours" << endl
<< " In Days : " << (travel / 24) << " days" << endl
<< " In years : " << (travel / 8760) << " years" << endl;
}
else if (selection == 'b')
{
sgrav = 0.86;
pweight = (weight*sgrav);
planet = "Venus";
distance = abs(67 - 93);
travel = (distance * speed);
cout << "INTERPLANETARY TRAVEL: " << "Earth to " << planet << endl
<< "--------------------------------------------------" << endl
<< "Your weight on " << planet << ": " << setprecision(2) << pweight << " lbs" << endl
<< endl
<< "Your travel time to " << planet << ":" << endl
<< " In Hours: " << travel << " hours" << endl
<< " In Days : " << (travel / 24) << " days" << endl
<< " In years : " << (travel / 8760) << " years" << endl;
}
else if (selection == 'c')
{
sgrav = 1.00;
pweight = (weight*sgrav);
planet = "Earth";
distance = abs(93 - 93);
travel = (distance * speed);
cout << "INTERPLANETARY TRAVEL: " << "Earth to " << planet << endl
<< "--------------------------------------------------" << endl
<< "Your weight on " << planet << ": " << setprecision(2) << pweight << " lbs" << endl
<< endl
<< "Your travel time to " << planet << ":" << endl
<< " In Hours: " << travel << " hours" << endl
<< " In Days : " << (travel / 24) << " days" << endl
<< " In years : " << (travel / 8760) << " years" << endl;
}
else if (selection == 'd')
{
sgrav = 0.37;
pweight = (weight*sgrav);
planet = "Mars";
distance = abs(141 - 93);
travel = (distance * speed);
cout << "INTERPLANETARY TRAVEL: " << "Earth to " << planet << endl
<< "--------------------------------------------------" << endl
<< "Your weight on " << planet << ": " << setprecision(2) << pweight << " lbs" << endl
<< endl
<< "Your travel time to " << planet << ":" << endl
<< " In Hours: " << travel << " hours" << endl
<< " In Days : " << (travel / 24) << " days" << endl
<< " In years : " << (travel / 8760) << " years" << endl;
}
else if (selection == 'e')
{
sgrav = 2.64;
pweight = (weight*sgrav);
planet = "Jupiter";
distance = abs(483 - 93);
travel = (distance * speed);
cout << "INTERPLANETARY TRAVEL: " << "Earth to " << planet << endl
<< "--------------------------------------------------" << endl
<< "Your weight on " << planet << ": " << setprecision(2) << pweight << " lbs" << endl
<< endl
<< "Your travel time to " << planet << ":" << endl
<< " In Hours: " << travel << " hours" << endl
<< " In Days : " << (travel / 24) << " days" << endl
<< " In years : " << (travel / 8760) << " years" << endl;
}
else if (selection == 'f')
{
sgrav = 1.17;
pweight = (weight*sgrav);
planet = "Saturn";
distance = abs(886 - 93);
travel = (distance * speed);
cout << "INTERPLANETARY TRAVEL: " << "Earth to " << planet << endl
<< "--------------------------------------------------" << endl
<< "Your weight on " << planet << ": " << setprecision(2) << pweight << " lbs" << endl
<< endl
<< "Your travel time to " << planet << ":" << endl
<< " In Hours: " << travel << " hours" << endl
<< " In Days : " << (travel / 24) << " days" << endl
<< " In years : " << (travel / 8760) << " years" << endl;
}
else if (selection == 'g')
{
sgrav = 0.92;
pweight = (weight*sgrav);
planet = "Uranus";
distance = abs(1782 - 93);
travel = (distance * speed);
cout << "INTERPLANETARY TRAVEL: " << "Earth to " << planet << endl
<< "--------------------------------------------------" << endl
<< "Your weight on " << planet << ": " << setprecision(2) << pweight << " lbs" << endl
<< endl
<< "Your travel time to " << planet << ":" << endl
<< " In Hours: " << travel << " hours" << endl
<< " In Days : " << (travel / 24) << " days" << endl
<< " In years : " << (travel / 8760) << " years" << endl;
}
else if (selection == 'h')
{
sgrav = 1.44;
pweight = (weight*sgrav);
planet = "Neptune";
distance = abs(2793 - 93);
travel = (distance * speed);
cout << "INTERPLANETARY TRAVEL: " << "Earth to " << planet << endl
<< "--------------------------------------------------" << endl
<< "Your weight on " << planet << ": " << setprecision(2) << pweight << " lbs" << endl
<< endl
<< "Your travel time to " << planet << ":" << endl
<< " In Hours: " << travel << " hours" << endl
<< " In Days : " << (travel / 24) << " days" << endl
<< " In years : " << (travel / 8760) << " years" << endl;
}
}
else if (selection == 'q')
{
cout << "You have chosen to quit the program. Thank you for using the program!" << endl;
}
else
{
cout << "You have entered an invalid selection." << endl;
}
//system("PAUSE");
return 0;
Its awfully long for such a simple program, and i was wondering if anyone would know a more efficient way to output the information using if-else statements instead of copy pasting into each case. Also, I am getting a syntax error on the line where i declare my double variables, and its with the semi colon, and i don't understand why.
1) if (selection >= 'a' && selection <= 'h') The user could have caps lock pressed, so consider A~Z also
2) (in my opinion) Quit should be at the start of logic.
3) You can use switch(selection) instead. dafault: Invalid Input. Try again.
4) Put your output in a function and pass the variables to it, instead of writting the same code over again
void prn(float sgrav, int pweight, char arr[], float distance, float travel)
{
cout << "INTERPLANETARY TRAVEL: " << "Earth to " << planet << endl
<< "--------------------------------------------------" << endl
<< "Your weight on " << planet << ": " << setprecision(2) << pweight << " lbs" << endl
<< endl
<< "Your travel time to " << planet << ":" << endl
<< " In Hours: " << travel << " hours" << endl
<< " In Days : " << (travel / 24) << " days" << endl
<< " In years : " << (travel / 8760) << " years" << endl;
}

GetConsoleSelectionInfo C++

cI want to select some text with my cursor using the Mark Function from Console, but my code doesn't work ...
CONSOLE_SELECTION_INFO c;
if(GetConsoleSelectionInfo(&c))
{
while((c.dwFlags & CONSOLE_MOUSE_DOWN) == 0) { if(c.dwFlags) cout << c.dwFlags; }
cout << "SelectionAnchor: " << c.dwSelectionAnchor.X << " " << c.dwSelectionAnchor.Y;
cout << "RectangleSelection: " << c.srSelection.Top << " " << c.srSelection.Left << c.srSelection.Bottom << c.srSelection.Right;
}
else cout << "\n\nError: " << GetLastError();
Whatever I'm selecting or I'm doing, always c.dwFlags will be 0 ...

How to make an array print zeros when there is no input in that slot

The user inputs a double and string which gets stored into two arrays. Like so :
{
double DoC;
string Nm;
cout << " Please enter the amount charged to your credit card " << endl;
cin >> DoC;
cout << " Please enter where the charge was made " << endl;
cin >> Nm;
getline(cin, Nm);
cca.doCharge(Nm,DoC);
break;
}
Then the double and string are passed to this :
{
if (amount > 0)
{
for (int i = 9; i != 0; i--)
{
last10withdraws[i] = last10withdraws[i-1];
last10charges[i] = last10charges[i-1];
}
last10withdraws[0] = amount;
last10charges[0] = name;
setBalanceW(amount);
}
else
{
cout << " ERROR. Number must be greater then zero. " << endl;
}
return 0;
}
This seems to work very well for storing the data into the arrays. However I then use this function to display the data inside of the arrays :
{
cout << " Account: Creditcard Withdraws " << " " << " Account: Creditcard Deposits " << " " << " Account: Creditcard last 10 charges " << endl;
cout << " " << last10withdraws[0] << " " << last10deposits[0] << " " << last10charges[0] << endl;
cout << " " << last10withdraws[1] << " " << last10deposits[1] << " " << last10charges[1] << endl;
cout << " " << last10withdraws[2] << " " << last10deposits[2] << " " << last10charges[2] << endl;
cout << " " << last10withdraws[3] << " " << last10deposits[3] << " " << last10charges[3] << endl;
cout << " " << last10withdraws[4] << " " << last10deposits[4] << " " << last10charges[4] << endl;
cout << " " << last10withdraws[5] << " " << last10deposits[5] << " " << last10charges[5] << endl;
cout << " " << last10withdraws[6] << " " << last10deposits[6] << " " << last10charges[6] << endl;
cout << " " << last10withdraws[7] << " " << last10deposits[7] << " " << last10charges[7] << endl;
cout << " " << last10withdraws[8] << " " << last10deposits[8] << " " << last10charges[8] << endl;
cout << " " << last10withdraws[9] << " " << last10deposits[9] << " " << last10charges[9] << endl;
cout << endl;
}
Lets say the user has inputted three doubles into the deposit array. When I call the function to display it I get something like this :
60
30
20
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
How can I make it so that the -9.25596e+061's are 0's? I have not been able to find anything really helping me. Also with the array that contains strings, when it is called to display it displays nothing. Why is this ?
Initialize the array as:
int last10withdraws[10] = {0};
Now all elements are zero.
If the user enters three numbers, then first three elements will be non-zero (assuming only non-zero is allowed), and the rest will be zero.
If last10withdraws is a member of a class m(and you're using C++03), then you can use member-initializer list to default-initialize which will initialize all the elements to zero.
class myclass
{
int last10withdraws[10];
public:
myclass() : last10withdraws()
{ //^^^^^^^^^^^^^^^^^^^ member initializer list
}
};
Hope that helps.
You are getting the error because you are not initializing the values. Start by saying int last10withdraws[10] = {0}; That will initialize all values to zero.

Help implementing a "store buying" program

My professor instructed us to make a Starbucks like menu where the user can continue to input orders until they are finished. I got the menu display down along with the loop, but I can't get it to add up the orders that were inputted and display a total.
#include <iostream>
using namespace std;
int main()
{
int choice = 1;
cout << endl << "Welcome to Hunterbucks!";
while (choice > 0)
{
cout << endl << "Input -1 when you're finished ordering!";
cout << endl << endl << "Coffee" << " " << "($)";
cout << endl << "1. Regular" << " " << "1.50";
cout << endl << "2. Decaf" << " " << "1.23";
cout << endl << "3. Americano" << " " << "2.25";
cout << endl << "4. Espresso" << " " << "2.25";
cout << endl << "5. Latte" << " " << "2.50";
cout << endl << "6. Cappuccino" << " " << "2.75";
cout << endl << "7. Frappuccino" << " " << "2.75";
cout << endl << "8. Macchiato" << " " << "2.50";
cout << endl << endl << "Snacks" << " " << "($)";
cout << endl << "9. Muffin" << " " << "1.00";
cout << endl << "10. Blueberry Muffin" << " " << "1.25";
cout << endl << "11. Raspberry Muffin" << " " << "1.25";
cout << endl << "12. Scone" << " " << "0.75";
cout << endl << "13. Blueberry Scone" << " " << "1.00";
cout << endl << "14. Croissant" << " " << "0.75";
cout << endl << endl << "What would you like to order? ";
cin >> choice;
if (choice <= 0)
cout << endl << "Thank you for your order.";
else
cout << endl << "What else would you like to order?";
}
cout << endl << "Thank you for choosing Hunterbucks! Come again soon.";
return 0;
}
Any info that can help me? I'm just a beginner and have been trying this for a few hours.
In pseudo-code you want something like this:
float total = 0.0;
while (choice > 0)
{
....
cin >> choice;
if (choice <= 0)
cout << endl << "Thank you for your order.";
else
{
total += costs[choice];
cout << endl << "What else would you like to order?";
}
}
You'll need to define an array names costs that contains the cost of each item. You'll also want to tackle validation of the user input so that you don't erroneously attempt to read outside the range of the costs array.
You're probably looking at something like this:
#include <iostream>
using namespace std;
int main()
{
int choice = 1;
float sum = 0.0;
float arr[] = {
0.00, 1.50, 1.23, 2.25, 2.25, 2.50, 2.75, 2.75, 2.50,
1.00, 1.25, 1.25, 0.75, 1.00, 0.75
};
cout << endl << "Welcome to Hunterbucks!";
while (choice > 0)
{
cout << endl << "Input -1 when you're finished ordering!";
cout << endl << endl << "Coffee" << " " << "($)";
cout << endl << "1. Regular" << " " << "1.50";
cout << endl << "2. Decaf" << " " << "1.23";
cout << endl << "3. Americano" << " " << "2.25";
cout << endl << "4. Espresso" << " " << "2.25";
cout << endl << "5. Latte" << " " << "2.50";
cout << endl << "6. Cappuccino" << " " << "2.75";
cout << endl << "7. Frappuccino" << " " << "2.75";
cout << endl << "8. Macchiato" << " " << "2.50";
cout << endl << endl << "Snacks" << " " << "($)";
cout << endl << "9. Muffin" << " " << "1.00";
cout << endl << "10. Blueberry Muffin" << " " << "1.25";
cout << endl << "11. Raspberry Muffin" << " " << "1.25";
cout << endl << "12. Scone" << " " << "0.75";
cout << endl << "13. Blueberry Scone" << " " << "1.00";
cout << endl << "14. Croissant" << " " << "0.75";
cout << endl << endl << "What would you like to order? ";
cin >> choice;
if (choice <= 0){
cout << endl << "Thank you for your order.";
} else {
cout << endl << "What else would you like to order?";
sum += arr[choice];
}
}
cout << "Total: " << sum << endl;
cout << endl << "Thank you for choosing Hunterbucks! Come again soon.";
return 0;
}
Do note the following:
1) Your menu choices being with '1' thus there is a need to offset your arr at index '0' with the '0.00' value there.
2) The cost added up follows that of your indexed array, thus you would probably want to format your output according to your array, so that next time, all you need to do is to update your array.
Hope it helped. Cheers!
The way you have your code set up warrants a switch statement, like the following:
double total = 0;
switch (choice)
{
case 1:
total += 1.50; // Regular.
break;
case 2:
total += 1.23; // Decaf.
break;
// Etc.
}
cout << endl << "Your total is " << total;
That being said, the easiest way to do this would be to have an array of prices:
double prices[] = {1.50, 1.23, 2.25};
// ...
total += prices[choice - 1]; // No switch statement needed.