Cannot to print in for loop - c++

Below is my code:
#include <iostream>
using namespace std;
int main()
{
int numboftimes, n, sumvalue = 0, strvalue = 0;
cout << "Enter Number of Times You Want The Loop to Run: ";
cin >> numboftimes;
for (n = 1; n <= numboftimes; n++)
{
sumvalue += n;
strvalue += 1;
}
cout << "You ran the for loop: " << numboftimes << " times. \n";
}
I can't get a cout in the for loop to print out an iteration. i.e., 1+2+3+4+5 = 15.

You need to print each iteration in the loop:
for (n = 1; n <= numboftimes; n++) {
sumvalue += n;
strvalue += 1;
cout << "This is the " << numboftimes << " iteration to print" << endl;
cout << "The sum is " << sumvalue << endl;
}
In this way you can print each iteration.

When you are printing the variable numboftimes you are just printing a constant value
for(int n=0; n < numboftimes ; n++)
cout << n << endl;
You put the cout anyways outside the for loop which will output only once, and only the integer numb of times

Related

If statement inside of a for cycle

first of all, sorry if my title isn't appropriate. English isn't my first language so I didn't really know how to phrase it correctly and understandably.
So, I'm learning some C++ and I got stuck on a problem. Right now my code looks like this:
#include <iostream>
using namespace std;
int main()
{
int start, finish;
cout << "Enter range start" << endl;
cin >> start;
cout << "Enter the range end" << endl;
cin >> finish;
if (start < finish)
{
for (int i = start; i <= finish; i++)
{
cout << "Number " << i << " is divisible by ";
for (int j = 2; j <= 10; j++)
{
if (i % j == 0)
{
cout << j << " ";
}
}
cout << endl;
}
}
return 0;
}
And the output I get is:
Number i is divisible by j1, j2 and etc.
Number i is divisible by
Number i is divisible by j1, j2 and etc.
You get the idea.
What I'm trying to get is this
Number i is divisible by j1, j2 and etc.
Number i isn't divisible by any numbers
Number i is divisible by j1, j2 and etc.
I tried doing this:
#include <iostream>
using namespace std;
int main()
{
int start, finish;
cout << "Enter range start" << endl;
cin >> start;
cout << "Enter the range end" << endl;
cin >> finish;
if (start < finish)
{
for (int i = start; i <= finish; i++)
{
cout << "Number " << i;
for (int j = 2; j <= 10; j++)
{
if (i % j == 0)
{
cout << " is divisible by " << j << " ";
}
else
{
cout << " isn't divisible by any numbers";
}
}
cout << endl;
}
}
return 0;
}
But the output is just a mess. Sorry if this is something straight forward and something I could've looked up on the net, but I just didn't know how to phrase my problem correctly and what to look for. I'll appreciate any help.
You should
create a flag that indicates if any j that divides i.
after the loop, check the flag and print the message if applicable.
#include <iostream>
using namespace std;
int main()
{
int start, finish;
cout << "Enter range start" << endl;
cin >> start;
cout << "Enter the range end" << endl;
cin >> finish;
if (start < finish)
{
for (int i = start; i <= finish; i++)
{
cout << "Number " << i;
bool divisorFound = false;
for (int j = 2; j <= 10; j++)
{
if (i % j == 0)
{
if (!divisorFound)
{
cout << " is divisible by ";
}
cout << j << " ";
divisorFound = true;
}
}
if (!divisorFound)
{
cout << " isn't divisible by any numbers";
}
cout << endl;
}
}
return 0;
}

Showing values in an array

I have been coding another programming challenge from a book. It is about asking a user to input n numbers inside an array. Every time the user inputs, the current numbers entered should show up. Numbers less than or equal to zero should not be accepted. I have managed to do the first condition. However, it even shows the "empty" slots. I have tMy code will explain it. Here:
#include <iostream>
using namespace std;
int main() {
int size = 0, input_num [100], i, j;
cout << "Enter an array size: ";
cin >> size;
if (size <= 0) {
while (size <= 0) {
cout << "Enter an array size again: ";
cin >> size;
}
}
cout << "\n\nYou may now enter " << size << " numbers ";
cout << "\n------ ------ ------ ------\n";
for (i = 0; i < size; i++) {
cout << "Enter a number: ";
cin >> input_num [i];
cout << "\nEntered Numbers: ";
for (j = 0; j < size; j++) {
cout << input_num [j] << " " ;
}
cout << "\n";
}
}
In your output section, it should be j <= i, not j < size.
cout << "\nEntered Numbers: ";
for (j = 0; j <= i; j++) {
cout << input_num [j] << " " ;
}
cout << "\n";

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?

Program hangs and does not loop

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

Why does my code perform the later cout first?

The following code
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
bool checkPerm(unsigned long long x){
vector<unsigned long long> tester;
string strx = to_string(x);
int sizestrx = strx.size();
int counter = 1;
cout << "x is " << strx << " and its permutations are ";
while (next_permutation(strx.begin(), strx.end())){
cout << strx << " ";
unsigned long long stoipermstrx = stoi(strx);
tester.push_back(stoipermstrx);
}
cout << endl;
int sizetester = tester.size();
for (int j = 2; j <= 6; j++){
cout << "j is " << j << ' ';
for (int k = 0; k < sizetester; k++){
if (j*x == tester[k]){
cout << "counter increased because x, counter " << x << " " << counter << endl;
counter++;
if (counter == 6){
cout << "Number is " << x << endl;
return true;
}
break;
}
}
//cout << "Number " << x << " failed" << endl;
return false;
}
return true;
}
int main(){
unsigned long long x = 1;
for (double i = 0; ; i++){
cout << i << endl;
while (x < 1.67*pow(10, i)){
if (i == 5)
cout << x << endl;
if (checkPerm(x)){
cin.get();
}
x++;
}
x = pow(10, (i + 1));
}
cin.get();
}
has the following problems in this piece of code:
cout << "x is " << strx << " and its permutations are ";
while (next_permutation(strx.begin(), strx.end())){
cout << strx << " ";
unsigned long long stoipermstrx = stoi(strx);
tester.push_back(stoipermstrx);
}
cout << endl;
int sizetester = tester.size();
for (int j = 2; j <= 6; j++){
cout << "j is " << j << ' ';
for (int k = 0; k < sizetester; k++){
if (j*x == tester[k]){
cout << "counter increased because x, counter " << x << " " << counter << endl;
counter++;
if (counter == 6){
cout << "Number is " << x << endl;
return true;
}
break;
}
}
//cout << "Number " << x << " failed" << endl;
return false;
}
Here the output will be "j is j x is x and its permutations are (permutations of x)". HOWEVER, the console should print "x is x and its permutations are (permutations) j is j". The following sample output is given:
j is 2 x is 1355 and its permutations are 1535 1553 3155 3515 3551 5135 5153 531
5 5351 5513 5531
j is 2 x is 1356 and its permutations are 1365 1536 1563 1635 1653 3156 3165 351
6 3561 3615 3651 5136 5163 5316 5361 5613
It appears there are two (minor) things about this. One, you are not looking at the value of sizetester before printing the value of j, and you are not printing a newline after the value of j. This means you are displaying the value of j for the previous loop at the beginning of your line for the current 'x'. If I understand what your code is supposed to be doing, it seems to be doing it correctly -- it's just the way that the output is getting displayed that makes it confusing.
Try this:
int sizetester = tester.size();
for (int j = 2; j <= 6; j++){
if (sizetester){ // <-- added test (see below)
cout << "j is " << j << '\n'; // <-- added newline
} // <--
The test against sizetester suppresses spurious printings of values for j - you later test that (k < sizetester) anyway. The newline just prevents values of j from starting the line for the next values of x, which appears to be the cause of the confusing output.