I am writing a program to simulate a dice roll with random function.
This is my code, but I am having infinite loop.
The program is supposed to ask the user how many times they would like to roll the dice.
Then I used a for loop to roll the dice from 1 to 6
and I put all that in a do-while in order to only allow the user to select between 1 and 6 , if the selection is outside of 1 to 6 it is supposed to say it is an invalid selection.
I am not allowed to use functions or arrays on this
#include<iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
//i is the counter for the loop
int i = 0;
//store total count for landing number
int num1 =0;
int num2 =0;
int num3 =0;
int num4 =0;
int num5 =0;
int num6 =0;
int Num1 =0;
int Num2 =0;
int Num3 =0;
int Num4 =0;
int Num5 =0;
int Num6 =0;
int randNum;
int times;
//User selection
int selection;
/* initialize random seed: */
srand ((unsigned int)time(NULL));
/* generate random number: */
randNum = rand() % 6 + 1;
cout<<"How many times would you like to roll the dice? " << endl;
cin >> selection;
do{
for(i = 1; i <= selection; i++)
{
if(randNum==1)
{
num1++;
}
else if(randNum==2)
{
num2++;
}
else if(randNum==3)
{
num3++;
}
else if(randNum==4)
{
num4++;
}
else if(randNum==5)
{
num5++;
}
else if(randNum==6)
{
num6++;
}
}
Num1 = num1/times *100;
Num2 = num2/times *100;
Num3 = num3/times *100;
Num4 = num4/times *100;
Num5 = num5/times *100;
Num6 = num6/times *100;
cout <<"# Rolled \t # Times \t % Times" << endl;
cout <<"----- \t ------- \t -------" << endl;
cout <<" 1 \t " << num1 << "\t " << fixed << setprecision(2) << Num1 << "%\n";
cout <<" 2 \t " << num2 << "\t " << fixed << setprecision(2) << Num2 << "%\n";
cout <<" 3 \t " << num3 << "\t " << fixed << setprecision(2) << Num3 << "%\n";
cout <<" 4 \t " << num4 << "\t " << fixed << setprecision(2) << Num4 << "%\n";
cout <<" 5 \t " << num5 << "\t " << fixed << setprecision(2) << Num5 << "%\n";
cout <<" 6 \t " << num6 << "\t " << fixed << setprecision(2) << Num6 << "%\n";
}
while(i >= 1 || i <= 6);
{
cout << "This is an invalid number. \n"
<< "The number of rolls should be equal to or greater than 1.\n"
<< "Please enter again.\n";
}
}
Your conditional statement: i = 1; i <= selection; i++ should go in the final while you have.
Try:
do {
if (randNum==1) {
num1++;
} else if(randNum==2) {
num2++;
} else if(randNum==3) {
num3++;
} else if(randNum==4) {
num4++;
} else if(randNum==5) {
num5++;
} else if(randNum==6) {
num6++;
}
i += 1;
} while (i <= selection);
The infinite loop is the do - while...
I'm not sure what you were trying to do there, but you will always get a number greater than 1 or smaller than 6 in your test - you can't fail both...
Related
Below if my code that is supposed to ask for user for 2 numbers and output the sum of all even numbers between them. I am only having trouble using the count function as I don't believe I am setting it right and google has only helped me so far
#include <iostream>
using namespace std;
int main() {
int num1, num2, sum;
while(num1 > num2) {
cout << "Enter 2 numbers seperated by a space. " << endl;
cout << "First number must be smaller then second number. " << endl;
cin >> num1 >> num2;
cout << endl;
}
if(num1 % 2 == 0)
count(== num1);
else
count(== num1 + 1);
while(count(<= num2)) {
sum = sum + count;
count = count + 2;
}
cout << "The sum of the even intergers between " << num1 << "and " << num2
<< " = " << sum << endl;
return 0;
}
There is no count() function in your code, nor is there a standard count() function in the standard C++ library that does what you want (though std::accumulate() comes close). Nor do you really need such a function anyway, a simple loop will suffice.
Try something more like this:
#include <iostream>
#include <limits>
using namespace std;
int main() {
int num1, num2, sum = 0;
do {
cout << "Enter 2 numbers seperated by a space. " << endl;
cout << "First number must be smaller then second number. " << endl;
if (cin >> num1 >> num2) {
if (num1 < num2) break;
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ā\nā);
}
cout << endl;
}
while (true);
for (int num = num1; num <= num2; ++num) {
if ((num % 2) == 0)
sum += num;
}
cout << "The sum of the even integers between " << num1 << " and " << num2 << " = " << sum << endl;
return 0;
}
If you want to omit num1 and num2 from the sum, simply change the loop accordingly:
for (int num = num1 + 1; num < num2; ++num) {
So I'm trying to write a program that will read in ten whole numbers and outputs the sum of all numbers greater than zero, the sum of all numbers less than zero,
and the sum of all numbers, whether positive, negative, or zero.
Currently only the total sum and negative sum is adding correctly. My positive sum is always 1 or 0. Any tips to get me in the right direction would help.
Current Code:
#include <iostream>
using namespace std;
int main()
{
int N = 0;
int sum = 0;
int positiveSum = 0;
int negativeSum = 0;
cout << "Number 1" << endl;
cin >> N;
cout << "Number 2" << endl;
cin >> N;
cout << "Number 3" << endl;
cin >> N;
cout << "Number 4" << endl;
cin >> N;
cout << "Number 5" << endl;
cin >> N;
cout << "Number 6" << endl;
cin >> N;
cout << "Number 7" << endl;
cin >> N;
cout << "Number 8" << endl;
cin >> N;
cout << "Number 9" << endl;
cin >> N;
cout << "Number 10" << endl;
cin >> N;
for(int i=0;i<10;i++)
{
if (N >= 0 )
{
positiveSum += N;
}
else
{
negativeSum += N;
}
}
sum = positiveSum + negativeSum;
cout << "The positive sum is= " << positiveSum << endl;
cout << "the negative sum is= " << negativeSum << endl;
cout << "The total sum is= " << sum << endl;
return 0;
}
After entering all Ns and when entring to the loop, you have only the last N. because after assigning the first number to N you don't process the value but you assigning the next value to N again after asking the user to enter it and so on. Use something like the following
#include <iostream>
int main()
{
int N = 0;
int sum = 0;
int positiveSum = 0;
int negativeSum = 0;
for(int i=0;i<10;i++)
{
std::cout << "Number "<<i + 1<< std::endl;
std::cin >> N;
if (N >= 0 )
{
positiveSum += N;
}
else
{
negativeSum += N;
}
}
sum = positiveSum + negativeSum;
std::cout << "The positive sum is= " << positiveSum << std::endl;
std::cout << "the negative sum is= " << negativeSum << std::endl;
std::cout << "The total sum is= " << sum << std::endl;
return 0;
}
And see Why is "using namespace std;" considered bad practice?
I am a beginner in c++. I wrote a program to separate the digits in an integer entered and display them and their sum. However, when the loop repeats, the program hangs even though it compiled perfectly. I tried a '... while' loop and a while loop but the problem persists. What should I do to have it repeat (ask user for next integer, calculate and display the results) without problems? Any help will be appreciated.
//Preprocessor directives
#include <iostream>
#include <cmath>
//Standard library
using namespace std;
//enter function main
int main()
{
int num;
int digit;
int sum = 0;
int pwr = 0;
cout << "Enter an integer: " << endl;
cin >> num;
cout << endl;
while (num != 0 )
{
//while loop to ask user to enter another number
cout << "The integer you entered is: " << num << endl;
cout << "The digits of " << num << " are: " << endl;
if (num < 0)
num = -num;
//find the highest number of 10 that divides the number
while (num / static_cast<int>(pow(10.0, pwr)) >= 10)
pwr++;
while (num > 0)
{
digit = num / static_cast<int>(pow(10.0, pwr));
cout << digit << " ";
sum = sum + digit;
num = num % static_cast<int>(pow(10.0, pwr));
pwr--;
}
if (pwr != -1) //Either num is 0 or there are trailing zeros in num
while (pwr != -1)
{
cout << 0 << " ";
pwr--;
}
cout << endl;
cout << "The sum of the digits = " << sum << endl;
while (num != 0);
cout << "Enter another integer: " << endl;
cin >> num;
cout << endl;
}
return 0;
}
//Preprocessor directives
#include <iostream>
#include <cmath>
//Standard library
using namespace std;
//enter function main
int main()
{
while (true )
{
//reset initial values every loop
int num;
int digit;
int sum = 0;
int pwr = 0;
cout << "Enter an integer: " << endl;
cin >> num;
cout << endl;
//same exit condition
if (num == 0)
break;
//while loop to ask user to enter another number
cout << "The integer you entered is: " << num << endl;
cout << "The digits of " << num << " are: " << endl;
if (num < 0)
num = -num;
//find the highest number of 10 that divides the number
while (num / static_cast<int>(pow(10.0, pwr)) >= 10)
pwr++;
while (num > 0)
{
digit = num / static_cast<int>(pow(10.0, pwr));
cout << digit << " ";
sum = sum + digit;
num = num % static_cast<int>(pow(10.0, pwr));
pwr--;
}
if (pwr != -1) //Either num is 0 or there are trailing zeros in num
while (pwr != -1)
{
cout << 0 << " ";
pwr--;
}
cout << endl;
cout << "The sum of the digits = " << sum << endl;
//extraneous
/*while (num != 0);
cout << "Enter another integer: " << endl;
cin >> num;
cout << endl;*/
}
return 0;
}
I think you should use vector to store the digits... also the logic inside should be a bit smaller (see note 1), (note that I didn't test for negatives, you may use abs from cmath)
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
//Standard library
using namespace std;
//enter function main
int main()
{
int num;
std::vector<int> digits;
int sum = 0;
int pwr = 0;
cout << "Enter an integer: " << endl;
cin >> num;
cout << endl;
while (num) {
while (num) {
int temp = num % 10;
digits.push_back(temp);
sum += temp;
num /= 10;
}
std::reverse(digits.begin(), digits.end());
cout << sum << endl;
for(auto & a : digits)
{
cout << a << " ";
}
cout << endl;
cout << "Enter another integer: " << endl;
cin >> num;
cout << endl;
}
return 0;
}
note 1:
while (num) {
int temp = num % 10;
sum += temp;
num /= 10;
}
I was given the task of displaying Fibonacci numbers, but while asking the user how many number he/she would like to compute at a given time.
There was an example in the book they told me to refer. I figured a few lines of change in the code would produce the answer to my problem, but I'm having trouble understanding where I went wrong with this code.
Here's my code :
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
const int NumsToCal = 5;
cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;
int Num1 = 0, Num2 = 1;
char WantMore = '\0';
cout << Num1 << " " << Num2 << " " ;
do
{
for( int Index = 0; Index < NumsToCal; ++Index)
{
cout << Num1 + Num2 << " ";
int Num2Temp = Num2;
Num2 = Num1 + Num2;
Num1 = Num2Temp;
}
cout << "Do you want more numbers (y/n)? " << endl;
cin >> WantMore;
}
while (WantMore == 'y');
cout << "Goodbye!" << endl;
return 0;
}
Edit
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
int NumsToCal = 5;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;
int Num1 = 0, Num2 = 1;
char WantMore = '\0';
cout << Num1 << " " << Num2 << " " ;
do
{
for( int Index = 0; Index < NumsToCal; ++Index)
{
cout << Num1 + Num2 << " ";
int Num2Temp = Num2;
Num2 = Num1 + Num2;
Num1 = Num2Temp;
}
cout << "Do you want more numbers (y/n)? " << endl;
cin >> WantMore;
}
while (WantMore == 'y');
cout << "Goodbye!" << endl;
return 0;
}
The program output should look like this:
Enter an even number: 23
The number is not a positive even number.
Enter an even number: -6
The number is not a positive even number.
Enter an even number: 4
20 20.25 20.50 20.75 21
The sum is 102.5
program doesn't run properly. the odd/ even numbers are identified, but the loop to increment the variables (20 + 1 / (even number entered)) does not work right.
#include <iostream>
int main(int argc, char *argv[])
{
float val, sum, incr;
int num;
cout << "Enter an even number: ";
cin >> num;
if (num % 2 != 0)
cout << "The number " << num << " is not a positive even number." << endl;
else
cout << num << " is even!" << endl << endl;
incr = 1 / num;
for (val = 20.0F; val <= 21.0; val += incr)
{
cout << val;
sum += val;
}
cout << "The sum is " << sum << endl;
return 0;
}
If you divide one integer 1 between another num the result is an integer that as chris said is 0.
You should do:
incr = 1.0F / (float)num;
And for exiting for wrong introduced values you should return from main
#include <iostream>
int main() {
float val, sum, incr;
int num;
cout << "Enter an even number: ";
cin >> num;
if (num < 0 || num % 2 != 0){
cout << "The number " << num << " is not a positive even number." << endl;
return -1;
}
else {
cout << num << " is even!" << endl << endl;
}
incr = 1.0 / num;
for (val = 20.0F; val <= 21.0; val += incr) {
cout << val << " ";
sum += val;
}
cout << "The sum is " << sum << endl;
return 0;
}