Error using For loop - c++

I have problem running my loops.
I have to run a program where user enter 4 digit number, and it will show how many zero appear. This program will run depends on how many times user want it to run. so at the beginning I prompt user to enter the number of times the program will run, then the actual program will run in For loop. but so far, I can only run it once. Can someone help me? Thank you. Here is my code
#include <iostream>
using namespace std;
int main()
{
int numberTimes;
cout << "How many times do you want to run this check?\n";
cin >> numberTimes;
for (int counter = 0; counter < numberTimes; counter++);
{
int positiveInteger;
//prompt user to enter a positive integer
cout << "Please enter a positive integer value.\n";
cin >> positiveInteger;
//conditional statement
while((positiveInteger <=0) || (positiveInteger > 9999))
{
cout << "Invalid Value!!! Please try again.\n";
cout << "Please enter a positive integer value.\n";
cin >> positiveInteger;
}
cout << "Processing the value " << positiveInteger << ".\n";
int zeroCount = 0;
int firstNumber = positiveInteger%10; //separate the first number
if (firstNumber == 0) //determine if the first digit is zero
{
zeroCount = zeroCount + 1;
}
int digitOne = positiveInteger/10; //omitted first digit number
int secondNumber = digitOne%10; //separate the second number
if (secondNumber == 0) //determine if the second digit is zero
{
zeroCount = zeroCount + 1;
}
int digitTwo = digitOne/10; //omitted the second number
int thirdNumber = digitTwo%10; //separate the third number
if (thirdNumber == 0) //determine if the third digit is zero
{
zeroCount = zeroCount + 1;
}
int digitThree = digitTwo/10; //omitted the third number
int fourthNumber = digitThree%10; //separate the fourth number
if (fourthNumber == 0) //determine if the fourth digit is zero
{
zeroCount = zeroCount + 1;
}
cout << "Your first digit number is " << firstNumber << ".\n";
cout << "Your second digit number is " << secondNumber << ".\n";
cout << "Your third digit number is " << thirdNumber << ".\n";
cout << "Your fourth digit number is " << fourthNumber << ".\n";
cout << "Number of zero appear in your integer is " << zeroCount << ".\n";
if (zeroCount % 2 == 0) //determine if the number is even or odd
{
cout << "Your number zero appear even times.\n";
} else
{
cout << "Your number zero appear odd times.\n";
}
}
cout << "You have run this program " << numberTimes << ".\n";
cout << "Thank you and good bye.";
return 0;
}

The problem is using ; at the end of for line.
for (int counter = 0; counter < numberTimes; counter++);
In such a case that you used, it iterate the loop but do nothing.
In the other word,it is equal to the below program now :
for (int counter = 0; counter < numberTimes; counter++)
{
}
You can also use this program : (Add complementary lines)
#include <iostream>
using namespace std;
int main()
{
int positiveInteger;
int numberTimes;
int digit;
cout << "How many times do you want to run this check?\n";
cin >> numberTimes;
while(numberTimes)
{
int zeroRepitation=0;
cout << "Please enter a positive integer value.\n";
cin >> positiveInteger;
if ((positiveInteger <=0) || (positiveInteger > 9999))
{
cout << "Invalid Value!!! Try again!\n";
continue;
}
for (int i=1;i<5;i++)
{
digit = positiveInteger % 10;
if (digit=0)
{
++zeroRepitation;
}
positiveInteger = positiveInteger / 10;
}
cout<<"Number of zeros in this number= "<<zeroRepitation;
--numberTimes;
}

You put a semicolon at the end of the for statement. This:
for (int counter = 0; counter < numberTimes; counter++);
should be this:
for (int counter = 0; counter < numberTimes; counter++)

Related

Why is the while loop for input validation either skipping the loop entirely or not accounting for wrong inputs?

The loop is not reacting properly when an input outside the parameters is put in. It should only accept 0-9, but I can put any number positive or negative in and it outputs it as is.
I've tried setting the int count to null and 0 on the 4th line, thinking null might allow the while loop which originally said 'while count < 0 or count > 9'. Even at null it skipped the whole loop entirely and didn't allow for any inputs. I set it this way thinking it would loop through as the count is set to 0, but it seems to skip the conditions inside. This is a small function inside a larger lottery program.
void human(int user[], int size) {
const int SIZEOF = 5;
cout << "Enter your 5 lottery number picks.\n";
int count = 0;
while (count == 0) {
if (count >= 0 and count <= 9)
for (count = 0; count < SIZEOF; count++)
{
cout << "Number " << (count + 1) << ": ";
cin >> user[count];
}
else
{
cout << "You must enter 0-9: ";
cin >> user[count];
}
}
}
//EDIT: Here is the code I used based on it being pointed out I was using the //counter
void human(int user[], int size) {
const int SIZEOF = 5;
cout << "Enter your 5 lottery number picks.\n";
for (int count = 0; count < SIZEOF; count++)
{
cout << "Number " << (count + 1) << ": ";
cin >> user[count];
while (user[count] < 0 || user[count] > 9)
{
cout << "You must enter a number between 0 and 9: ";
cin >> user[count];
}
}
}
problems:
your function is missing an output/return value
your outer loop makes no sense
you check count instead of input
you read into a (potentially) out of bounds array
solutions:
use std::vector instead of array
think about input and output of your function
fix input and loops
example:
#include <iostream>
#include <string>
#include <vector>
std::vector<int> lotteryInput(uint32_t count)
{
std::vector<int> numbersPicked;
std::cout << "Enter your " << count << "lottery number picks.\n";
while(numbersPicked.size() < count)
{
int pick;
std::cout << "Number " << numbersPicked.size()+1 << ": ";
std::cin >> pick;
if ((pick >= 0) && (pick <= 9))
{
numbersPicked.push_back(pick);
}
else
{
std::cout << "You must enter 0-9: \n";
}
}
return numbersPicked;
}
int main()
{
auto numbersChosen = lotteryInput(5);
std::cout << "picked numbers: ";
for(const auto& num : numbersChosen)
{
std::cout << num << ", ";
}
}

C++ ,Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers

"Count positive and negative numbers and compute the average of numbers Write a program that reads an unspecified number of integers , determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a double. where did i go wrong??
#include <iostream>
using namespace std;
int main()
{
int num= 0;
int sum=0;
int pos=0;
int neg=0;
double ave=0;
cout << "Enter an integer, the input ends if it is 0: " ;
cin >> num ;
if (num%10==10) {
while (num!=0) {
num/=10;
if (num%10>0) {
pos++;
}
else if (num%10<0) {
neg++;
}
sum+=num;
}
ave= (double)sum/(pos+neg);
}
cout <<"The number of positives are " << pos <<endl;
cout <<"The number of negatives are " << neg <<endl;
cout <<"The total is " << sum << endl;
cout <<"The average is "<< ave << endl;
return 0;
}
You can use char[] to read input.
I have modified your program as follows;
int main()
{
int sum=0;
int pos=0;
int neg=0;
double ave=0;
char arr[100] = {'\0',};
std::cout << "Enter an integer, the input ends if it is 0: " ;
gets(arr);
int index = 0;
char ch[1];
bool negativeNumber = false;
while(true)
{
ch[0] = arr[index++];
if(ch[0] == ' ') // Check space and continue;
{
continue;
}
else if(ch[0] == '0' || ch[0] == '\0') // check for 0 or NULL and break;
{
break;
}
if(ch[0] == '-') // Set flag if "-ve"
{
negativeNumber = true;
continue;
}
int digit = atoi(ch);
if(negativeNumber)
{
digit *= -1;
negativeNumber = false;
}
if(digit > 0)
{
pos++;
}
else if(digit < 0)
{
neg++;
}
sum += digit;
}
ave= (double)sum/(pos+neg);
cout <<"The number of positives are " << pos <<endl;
cout <<"The number of negatives are " << neg <<endl;
cout <<"The total is " << sum << endl;
cout <<"The sverage is "<< ave << endl;
return 0;
}
Hope this helps.

Use while loop to print series of even and odd numbers

I know I'm missing something real simple but I can't seem to get the numbers to print out in rows of just odd or just even numbers using a while loop or loops. Also It keeps printing out "the even numbers are:"/ "the odd numbers are:" for every number.
#include<stdio.h>
#include <iostream>
using namespace std;
int main()
{
//declare variables
int number;
int n;
cout << "Enter value less than 100: ";
cin >> n; //take user input
while (n <= 100) //loop only if n equals 100 or less
{
for(number = n; number <= n; number++) //for loop to increment int value
{
if(number % 2 !=0) //determines if odd
{
cout << "The odd numbers are:" <<number << endl; //prints odd values
}
}
for(number = n;number <= n; number++) // for loop to increment int value
{
if(number % 2 ==0) //determines if even
{
cout <<"The even numbers are:" <<number <<endl; //prints even values
}
}
n++;
}
return 0; //end of program
}
You may want this:
#include <iostream>
using namespace std;
int main()
{
//declare variables
int number;
int n;
cout << "Enter value less than 100: ";
cin >> n; //take user input
// print odd values
cout << "The odd numbers are:";
for (number = n + 1 - (n % 2); number <= 100; number += 2)
{
cout << " " << number;
}
cout << endl;
// print even values
cout << "The even numbers are:";
for (number = n + (n % 2); number <= 100; number += 2)
{
cout << " " << number;
}
cout << endl;
return 0; //end of program
}

How can I get the array to show the previous list of array content before the next input?

I need the user input to be saved into my array and then output the array before the user inputs the next time. I have been moving things around different ways but cannot seem to get them to perform properly. I tried to cut down the code to the two functions I am having issues with.
void PlayGame()
{
const int HighestNum = 50;
const int LowestNum = 1;
int RandomNumber = LowestNum + rand() % HighestNum; //set for better random results
cout << "Guess the random number between " << LowestNum << " and " << HighestNum << "!\n\n";
const int attempts = 15;// limits the attempts to guess the random number to 15
int Guess [attempts] = {};
cout << "Enter your guess " << endl;
for (int count = 0; count < attempts; count++)
{
cin >> Guess[count];
int z = RandomNumber, y = Guess[count], r;
r = reviewGuess (z,y);//calling the function that determines the results
switch (r)//switch statement for function results, letting the user know if they matched the number, if the number is higher, or lower
{
case 0:
cout << "You Win!!" << endl;
cout << "\n";
cin.get();
return;
case 1:
cout << "The number is higher than your guess" << endl;
break;
case -1:
cout << "The number is lower than your guess" <<endl;
break;
}
if (count == 15)
{
cout << "Sorry, no guesses remain. The random number was... " << RandomNumber << "!";//so the user can see the random number at the end of their attempts
cout << "\n";
cin.get();
Again();
}
}
return;
}
int DisplayGuess(int member[])
{
for(int i = 0; i < 15; ++i)
cout << "\nGuess " << i + 1 << ": " << member[i];
cout << endl;
return;
}
Try this inside your loop
if(count > 0)
{
for (int j= 0; j < count; j++)
{
cout<<Guess[j];
}
}
Call DisplayGuess() in the first line of the for loop. Since the first you time you call it your array is empty, it shouldn't output anything.
So,
for (int count = 0; count < attempts; count++)
{
DisplayGuess(Guess[count]);
cin >> Guess[count];
int z = RandomNumber, y = Guess[count], r;
r = reviewGuess (z,y);//calling the function that determines the
results
. . . . . .

Separating Digits in C++

I am working on an exercise from my C++ book and I'm not sure how to fix it. I am supposed to get an int from the user and display the individual digits in the order they were entered. For instance 12345 would be displayed 1 2 3 4 5. 7365 would be displayed 7 3 6 5. I have most of the code written but there is a logical error and I can't figure it out. Here is my code:
int main()
{
int number = 0;
int digit = 0;
int temp = 0;
int counter = 0;
int sum = 0;
int divisor = 0;
cout << "Please enter a nonzero number.";
cin >> number;
cout << "\nThe number you entered was " << number;
// Determine the number of digits
temp = number;
while (temp != 0)
{
temp = temp / 10;
counter++;
}
cout << "\nThere are " << counter << " digits in your number.";
// Separate the digits
temp = number;
cout << "\nSeparating the digits\n";
do
{
divisor = (pow(10.0, --counter));
digit = temp / divisor;
temp = temp % divisor;
cout << digit << " ";
sum = sum + digit;
}
while (counter != 0);
cout << "\nThe sum of the number is " << sum;
return 0;
}
When I enter 5555 the output is 5560. When I enter 1234 the output is 1236. Can anyone help me find my error?
Here's one version:
// If the number is only one digit, print it.
// Otherwise, print all digits except the last, then print the last.
void digits(int x)
{
if (x < 10){
cout << x;
}
else{
digits(x / 10);
cout << " " << x % 10;
}
}
Thank you all for your help :-) Turns out my code works fine in another compiler so I guess it's just a netbeans glitch.