How to ask for user-input in nested loops? - c++

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;
}

Related

Trouble using count function

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) {

Only negative numbers adding correctly

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?

Need to understand how to add values in a loop

#include <iostream>
using namespace std;
int main()
{
int numint, sum, a, b, ctr;
cout << "Enter the number of intervals: ";
cin >> numint;
ctr = 0;
while (ctr != numint)
{
cout << "Enter the lower and upper bounds of the interval: ";
cin >> a;
cin >> b;
ctr++;
}
cout << "The sum of the " << numint << " intervals is: [" << a << "," << b << "]";
return 0;
}
I need to add the upper bounds of the interval (a) and lower bounds of the interval (b). This depends on the amt of intervals there are.
here:
cout << "The sum of the " << numint << " intervals is: [" << a << "," << b << ]";
you are not adding the values but instead just printing them
instead use some accumulator variable and do the math before printing it, like:
int main()
{
int numint,sum,a,b,ctr;
int result = 0;
cout << "Enter the number of intervals: ";
cin >> numint;
ctr=0;
while (ctr!=numint)
{
cout << "Enter the lower and upper bounds of the interval: ";
cin >> a;
cin >> b;
ctr++;
}
result = a + b ;
cout << "The sum of the " << numint << " is: " << result;
return 0;
}
As for what I understood from the question, you first need a int resultA=0 and another for b, and keep adding in the loop:
#include <iostream>
using namespace std;
int main()
{
int numint,sum,a,b,ctr;
int result = 0;
int resultA=0;
int resultB=0;
cout << "Enter the number of intervals: ";
cin >> numint;
ctr=0;
while (ctr!=numint)
{
cout << "Enter the lower and upper bounds of the interval: ";
cin >> a;
cin >> b;
resultA+=a;
resultB+=b;
ctr++;
}
result = resultA + resultB ;
cout << "The sum of the " << numint << " is: " << result;
return 0;
}
I think this is what you are looking for:
#include <iostream>
using namespace std;
int main() {
int numberOfIntervals;
int lowerBound;
int upperBound;
int counter;
int lowerBoundSum = 0;
int upperBoundSum = 0;
cout << "Enter the number of intervals: ";
cin >> numberOfIntervals;
counter = 0;
while (counter != numberOfIntervals) {
cout << "Enter the lower and upper bounds of interval "
<< counter << ": ";
cin >> lowerBound;
cin >> upperBound;
lowerBoundSum += lowerBound;
upperBoundSum += upperBound;
counter++;
}
cout << "The sum of the lower bounds is " << lowerBoundSum
<< endl;
cout << "The sum of the upper bounds is " << upperBoundSum
<< endl;
return 0;
}

single dice roll simulation with C++

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...

How to ask for user input in nested loops?

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.
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;
}
Xsami is absolutely right. You only need to include one more line like:
cin>>NumstoCal;
Though it won't be bad to change the way you output stuff for a bit more clarity.
Here is my code:
https://ideone.com/BXREP9
The only thing that you have to do is read NumsToCal again, and you have to do something like this after cin >> WantMore;
if ( WantMore == 'y' )
{
Num1 = 0;
Num2 = 1;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << Num1 << " " << Num2 << " " ;
}
This is my code: http://ideone.com/a8um5Z