How do you compare two variables in an if statement? - c++

I'm creating a program where a user tries to guess the amount of times two dice have to roll to reach the totalsum of 100.
I've finished the loop but now I'm stuck on the point where I want the program to compare the two variables. I've googled but nothing has been coming up regarding the issue.
In regards to the code, here's what I have:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
const int min_value=1;
const int max_value=6;
int die1,die2,sum,totalRolls, totalSum, prediction;
unsigned seed = time(0);
srand(seed);
sum=0;
totalRolls=0;
totalSum=0;
cout << "How many rolls will it take to reach a total of 100?\n";
cin >> prediction;
while (totalSum <= 100)
{
cout << "Rolling the die\n";
die1=(rand() % (max_value - min_value + 1) + min_value);
die2=(rand() % (max_value - min_value + 1) + min_value);
cout << die1 << endl;
cout << die2 << endl;
sum=die1+die2;
totalSum+=sum;
cout << "Your current total is " << totalSum << endl;
totalRolls++;
cout << "Last roll number = " << totalRolls << endl;
}
if (totalRolls <= 5)
{
cout << "Amazing!\n";
}
else if (totalRolls <=10)
{
cout << "Good\n";
}
else if (totalRolls <= 15)
{
cout << "Okay\n";
}
else if (totalRolls <= 20)
{
cout << "Try harder\n";
}
system("Pause");
return 0;
}
I honestly just don't know how to compare the predicted total number of rolls compared to the actual rolls it took.

Related

C++ assignment that requires a tricky while loop at the end

Is there some way to write a condition within a while loop that creates output if the user guesses a number that is within 10 units (plus or minus) from a random number generated by the program (integers)?
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand ( time(NULL) );
bool valid;
int randNum;
int sum = 0;
int userNum;
for (int x = 1; x < 11; x++)
{
randNum = rand() % (71) + 7;
cout << "Random number " << x << ": " << randNum << endl;
sum = sum + randNum;
}
cout << "\nThe total of all the random numbers is " << sum << "\n\n";
cout << "Guess a number between 70 and 770: ";
do
{
cin >> userNum;
while(userNum >= 70 && userNum <= 770)
{
while(userNum == sum)
{
cout << "You win";
break;
}
while(/* the number given by the user is within 10 units from the random number generated by the program*/)
{
cout << "You almost won";
break;
}
break;
}
while(userNum < 70 || userNum > 770)
{
cout << "Try again.";
valid = false;
break;
}
}
while (!valid);
return 0;
}
You will have to #include <stdlib.h> then for the condition in your while loop you write abs(userNum - randNum) <= 10. This will give the magnitude of the difference between the userNum and randNum, which you want to be less than or equal to 10.

This program is supposed to trigger the 'if' statements at the bottom but it wont as is. How can I get this to work?

I've tried a bunch of garbage to try and get the variables right but I keep screwing it up and I can't figure out where to place the variables correctly. I'm also having a really hard time placing my if and else if statements.
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int iseed = time(NULL);
srand(iseed);
int min = 1;
int max = 6;
int sum1 = 0;
int sum2 = 0;
int A = 0;
int B = 0;
int C = min + rand() % (max - min +1);
int D = min + rand() % (max - min +1);
char repeat2;
char repeat1;
cout << "Beat the computer!\n";
do {
int A = min + rand() % (max - min +1);
int B = min + rand() % (max - min +1);
int sum1 = A + B;
cout << "you rolled a " << A << " and a " << B << endl;
cout << "Total roll = " << sum1 << endl;
cout << "Would you like to keep this roll? Y or N: ";
cin >> repeat1;
} while (repeat1 == 'N');
cout << "The computer rolled a " << C << " and a " << D << endl;
cout << "Total roll = " << sum2 << endl;
if (sum2 > sum1) {cout << "You lose! D= YOU ROLLED " << sum1 << "HE ROLLED " << sum2 << ".";}
else if (sum1 < sum2) {cout << "You win! =D ";}
else if (sum1 = sum2) {cout << "It's a tie /= ";}
cout << "Would you like to play again?: ";
cin >> repeat2;
}
Problem 1
int sum1 = A + B; in the do/while loop declares a new sum1 that hides int sum1 = 0; as a result, nothing ever changes the outer sum1 from 0.
Solution 1
change
int sum1 = A + B;
to
sum1 = A + B;
Problem 2
Nothing sums C and D to provide a non-zero sum2.
Solution 2
Add in
sum2 = C + D;
Problem 3
if (sum2 > sum1)
else if (sum1 < sum2)
both test the same condition, that the player's rolls are less than the computer's rolls.
Solution 3
Change
else if (sum1 < sum2)
to
else if (sum2 < sum1)
To avoid problems like this in the future, give variable good descriptive names. It's a lot easier to see that sum_player was accidentally swapped with sum_computer than it is to spot that sum1 and sum2 have been exchanged.
Problem 4
else if (sum1 = sum2)
is not a comparison. It is an assignment followed by a test that the new value of sum1 is not 0 (which it will be).
Solution 4
Use
else if (sum1 == sum2)
instead.

Introduction to C++ assignment : parallel arrays

I am a beginner student looking for help on a program my professor assigned. The task is to use 2 parallel loops to ask the user 5 different salsas and their cost. Then print out the name, total sold, average, best seller and worst selling. The thing is, I can calculate best and worst selling, but those are ints. I don't understand how I can pull the string instead of int? Any help would be appriciated!#include
#include <string>
#include <cmath>
#include <cstring>
using namespace std;
int main() {
string name[5];
int total[5];
int i, final, counter, high, low;
for(i=0; i<=4; i++){
cout << "Salsa name: ";
cin >> name[i];
cout << "How many of " << name[i] << " were sold? ";
cin >> total[i];
final += total[i];
if(i < 0) {
"Sorry, you cannot have negative sales!";
return 0;
} else {
if(counter == 0) {
low = total[i];
} else if (total[i] < low) {
low = total[i];
} else if (total[i] > high) {
high = total[i];
} counter++;
}
}
cout << "Name Amount Sold\n"
<< "-------------------------\n";
for(int i = 0; i <= 4; i++){
cout << name[i] << " " << total[i] << endl;
}
cout << "Total sold: " << final << endl
<< "Most Sold: " << high << endl
<< "Least Sold: " << low;
return 0;
}
output:
Running /home/ubuntu/workspace/Ch6_Ex3.cpp
Salsa name: salsa1
How many of salsa1 were sold? 10
Salsa name: salsa2
How many of salsa2 were sold? 20
Salsa name: salsa3
How many of salsa3 were sold? 30
Salsa name: salsa4
How many of salsa4 were sold? 40
Salsa name: salsa5
How many of salsa5 were sold? 50
Name Amount Sold
-------------------------
salsa1 10
salsa2 20
salsa3 30
salsa4 40
salsa5 50
Total sold: 32862
Most Sold: 50
Least Sold: -547659664
Process exited with code: 0
Your code has some hiccups:
There is no point in the variable counter because you mean it to have the same value as i, so use i; nor do you initialize it. Comparing counter == 0 is undefined behaviour.
Instead of keeping track of the highest/lowest values in total, you should instead keep track of the indexes of those specific values, because the corresponding value in names is the name of each salsa brand...
Take note of the changes below:
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
using namespace std;
int main() {
string name[5];
int total[5];
int i;
int final = 0;
int high, low;
high = 0, low = 0;
for(i=0; i<=4; i++){
cout << "Salsa name: ";
cin >> name[i];
cout << "How many of " << name[i] << " were sold? ";
cin >> total[i];
final += total[i];
// if(i < 0) {
// "Sorry, you cannot have negative sales!";
// return 0;
// }
// i is never going to be less than 0.
if (total[high] < total[i])
high = i;
if (total[low] > total[i])
low = i;
}
// ...
cout << "Most-sold salsa: " << name[high]
<< "\nLeast-sold: " << name[low] << "\n";
return 0;
}
Other notes:
I strongly advise against using namespace std; See why
You initially forgot #include <iostream>

Not taking the input

I want to write a program that only takes odd numbers, and if you input 0 it will output the addition and average, without taking any even number values to the average and the addition. I'm stuck with not letting it take the even values..
Heres my code so far:
int num = 0;
int addition = 0;
int numberOfInputs = 0;
cout << "Enter your numbers (only odd numbers), the program will continue asking for numbers until you input 0.." << endl;
for (; ;) {
cin >> num;
numberOfInputs++;
addition = addition + num;
if (num % 2 != 0) {
//my issue is with this part
cout << "ignored" << endl;
}
if (num == 0) {
cout << "Addition: " << addition << endl;
cout << "Average: " << addition / numberOfInputs << endl;
}
}
Solution of your code:
Your code doesn't working because of following reasons:
Issue 1: You adding inputs number without checking whether it's even or not
Issue 2: If would like skip even then your condition should be as follow inside of the loop:
if (num%2==0) {
cout << "ignored:" <<num << endl;
continue;
}
Solving your issues, I have update your program as following :
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num = 0;
int addition = 0;
int numberOfInputs = 0;
cout << "Enter your numbers (only odd numbers), the program will continue asking for numbers until you input 0.." << endl;
for (; ;) {
cin>> num;
if (num%2==0) {
cout << "ignored:" <<num << endl;
continue;
}
numberOfInputs++;
addition = addition + num;
if (num == 0) {
cout << "Addition: " << addition << endl;
cout << "Average: " << addition / numberOfInputs << endl;
break;
}
}
}
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
int number;
int sum=0;
int average=0;
int inputArray[20]; // will take only 20 inputs at a time
int i,index = 0;
int size;
do{
cout<<"Enter number\n";
cin>>number;
if(number==0){
for(i=0;i<index;i++){
sum = sum + inputArray[i];
}
cout << sum;
average = sum / index;
cout << average;
} else if(number % 2 != 0){
inputArray[index++] = number;
} else
cout<<"skip";
}
while(number!=0);
return 0;
}
You can run and check this code here https://www.codechef.com/ide
by providing custom input

Explanation of C++ Code

I'm currently working on a program that outputs the number 1089 (i.e the Magic Number) of a three digit integer who's first digit is greater than its last. I have some code typed up, but am not receiving 1089, instead I'm receiving 891. Could anyone offer some explanation as to why.
//Uses a cout to inform user will be using the number 412 as an example.
//Uses a cout to explain to user the number needs to be reversed.
cout << "Alright, let's walk through an example, using the number 412." << endl;
int numExample = 412;
cout << "Please input 412" << endl;
cin >> numExample;
cout << "First, the number 412 is reversed." << endl;
//The following is done for reversing the number 412:
int reverseNum = 0, remainder = 0;
while (numExample)
{
remainder = numExample % 10;
reverseNum = (reverseNum * 10) + remainder;
numExample = numExample / 10;
}
cout << "The reverse of 412 is " << reverseNum << endl;
cout << "Next, we want to subtract the reverse of the original number from its reverse" << endl;
cout << "This gives us 198" << endl;
cout << "From there, we want to reverse 198." << endl;
//The same procedure is used to reverse 198
int numExample2 = 198;
cout << "Please enter 198" << endl;
cin >> numExample2;
int reverseNum2 = 0, remainder2 = 0;
while (numExample2)
{
remainder2 = numExample2 % 10;
reverseNum2 = (reverseNum2 * 10) + remainder2;
numExample2 = numExample2 / 10;
}
cout << "The reverse of 198 is " << reverseNum2 << endl;
int magicNumber = (reverseNum2 + numExample2);
cout << "Following that, we want to add 891 to 189 which gives us " << magicNumber << endl;
Try writing a function to handle this so your code is cleaner (It will also make it easier for people to help you!)
#include <iostream>
#include <cmath>
using namespace std;
int reverseDigit(int num); // For example purposes
int _tmain(int argc, _TCHAR* argv[])
{
int Number1,
Number2,
temp1,
temp2,
Result;
cout << "Enter the number 412: ";
cin >> Number1;
temp1 = reverseDigit(Number1);
temp1 = Number1 - temp1;
cout << "Enter the number 198: ";
cin >> Number2;
temp2 = reverseDigit(Number2);
Result = temp1 + temp2;
cout << "The magic number is: " << Result << endl;
return 0;
}
int reverseDigit(int num)
{
int reverseNum = 0;
bool isNegative = false;
if (num < 0)
{
num = -num;
isNegative = true;
}
while (num > 0)
{
reverseNum = reverseNum * 10 + num % 10;
num = num / 10;
}
if (isNegative)
{
reverseNum = -reverseNum;
}
return reverseNum;
}
I realize you're not working with negatives so you can remove that bit if you chose to use this, you can also expand on it... That being said this will make the actual " Reversing process " easier to work with and improve upon and read.