C++ do while loop not working - c++

I'm trying to make this code work:
#include <iostream>
using namespace std;
int main()
{
int i;
do
{
cout << ("please enter a number between 1 and 10");
cin >> i;
} while(i > 10 && i < 1)
cout << "the square of the number you have entered is " << i*i;
}
Basically, the idea is that a user enters a number between 1 and 10. While the number is not between 1 and 10, it keeps asking the user to enter a number between the values. Then, when the number is between the values, it is squared and returned to the user.
I can't see why this isn't working
Any help is appreciated

You have:
while (i > 10 && i < 1)
You want:
while (i > 10 || i < 1)

while (i > 10 && i < 1)
Your condition is logically faulty; if reinterpreted, it says:
while i is greater than 10 AND i is less than 1
Judging from your code, the || operator should be used:
} while (i > 10 || i < 1);

As others mentioned, your condition is faulty.
a number can't obviously be under 1 AND above 10 at the same time, so the while loop exits immediately after the do statement.
#include <iostream>
using namespace std;
int main()
{
int i;
do
{
cout << ("please enter a number between 1 and 10");
cin >> i;
} while (i < 1 || i > 10)
cout << "the square of the number you have entered is " << i*i;
}

You should use an Or ||, that condition with && will never be true.

The loop condition is wrong and will never loop, as i cannot be less than 1 && greater than 10 at the same time. You should use the logical OR (||) operator instead. In addition, there must be a semicolon placed after the do-while statement. And you probably want and end of line placed after the prompt. Also, you don't want to start the bad habit of polluting the global namespace, even with the awesomeness of std. So:
#include <iostream>
int main()
{
int i;
do {
std::cout << "please enter a number between 1 and 10\n";
std::cin >> i;
} while (i > 10 || i < 1);
std::cout << "the square of the number you have entered is " << i*i << std::endl;
}

Related

Code gives desired output but keeps on running

This code I wrote is supposed to subtract one from the number inputed, or divide by 2 based on whether it is a multiple of 3 or not. However, every time I try to run the code, It outputs the numbers I want but then doesn't stop running. I am new to coding and not sure how to fix this.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
int n;
cout << "Enter a positive number: " << endl;
cin >> n;
if (n < 0) {
cout << "Invalid input." << endl;
}
while (n >= 1) {
if (n % 3 == 0) {
n = n-1;
cout << n << endl;
}
else if (n % 3 != 0) {
n = n / 2;
cout << n << endl;
}
}
return 0;
}
This is a screenshot of the output I get. Instead of giving me the opportunity to run the code again it just stays like this:
I may be misunderstanding what you're asking, however, traversing through the code you can identify that nothing is being done to make the code run again. You would need add what you have inside another while loop. This new while loop would be something like while (input != 0) then run everything you have. In your input statement you could say "Please enter a positive number or enter 0 to exit". This is just an example of an approach, but the premise is that you need something to keep this loop running.

I need help solve my problem of my activity

So I need my help to correct my code, which is given to my programming class assignment. My activity is to print numbers divisible by 5 for the integers from 1 to 99. So I tried to code like this:
#include <iostream>
using namespace std;
int main () {
int num, min, max;
cout << "Enter Number: ";
cin >> num;
min = 1;
max = 99;
if (num > min || num < max) {
if (num % 5 == 0) {
cout << "Divisible.";}
else {
cout << "Not Divisible";}
}
else {
if (num % 5 == 0) {
cout << "Error Input.";}
else {
cout << "Error input.";}
}
return 0;
}
So when I compile and run, I test to enter a divisible number by 5 or not. When I put 0, it says "Error input," that's correct. However, when I put 100, it says "divisible" instead of "error input." What is the correct input of my code?
The input is an integer from 1 to 99, which means that it should be >= 1 AND <= 99.
So, simply change
if (num > min || num < max)
to
if (num >= min && num <= max)
You made a mistake in the first if statement. When you are giving the OR operator, it'll return true even if only one of the conditions are satisfied.
You should use the AND operator for your code to work as expected.
Moreover, you don't have to use the min and max variables also, it is making the program unnecessarily big (only 2 lines though, but still).

A program that counts how many prime numbers are between 2 and a given number

I've just had one of my exams at the college
one of the questions were to program a function that counts how many prime numbers are between 2 and a given number from the user(including the number).
I wrote this algorithm which works for me but they still deducted all of the points of the question as if it was completely wrong.
Can please someone tell me what's wrong with the code?
Thanks a lot.
#include <iostream>
using namespace std;
void main()
{
int count = 0;
int num;
cout << "Please enter a natural number " << endl;
cin >> num;
for (int i = 2; i <= num; i++)
{
if ((i == 2 || i == 3 || i == 5||i == 7) || (i % 3 != 0 & i % 5 != 0 & i % 7 != 0 & i % 2 != 0))
count++;
}
cout << "There are " << count << " prime numbers beteween 2 amd " << num << endl;
}
Your code only checks if a number is a multiple of 2, 3, 5, or 7. That's missing a lot of multiples, meaning your code likely gives many false positives.
That condition is also quite difficult to read, so you probably would have lost points for that as well.
You would have been much better off replacing that condition in the loop with another for loop, and checking every odd number from 3 to the square root of the test number.

Why I am getting this output from this loop?

#include <iostream>
using namespace std;
I am very new to cpp and programming and I am trying to find the factors of a number, max, why is my code output coming out the way it is?
int max;
cout << "Enter a number you'd like to see the divisors of: " << endl;
cin >> max;
//I am trying to find all divisors for the number max
//I know this isn't the most efficienct way but I thought that it would work.
//Instead of 50, 25, 20, 10, 5 ,1 for output it looks like 50, 25, 25, 25 25, 5
for (int t=1; t <= max; t++) {
if (max % t == 0) {
int m = max/t;
}
}
cout << m << endl;
Your output is misplaced. Move the cout << m << endl; statement into your if statement block:
if (max % t == 0) { // start of a block
int m = max / t;
std::cout << m << '\n';
} // end of a block
Make sure you properly mark the block of statements using braces {}. Now for a given input of 50 the output is:
50 25 10 5 2 1
Live example on Coliru
using namespace std;
As BO41 said, you should never use the namespace, here are some reasons: Why is "using namespace std" considered bad practice?
Instead of using the namespace, you should write only what you are using, for example:
using std::cout;
using std::endl;
Now back to the question:
for(int t=1; t <= max; t++){
if(max % t == 0)
int m = max/t;
} cout << m << endl;
Note that you are defining m inside the if and using it outside of it. also, if it wasn't for that, you would print only the last divisor you find. You should do something more like:
for(int t = 0; t <= max; t++){
if(max % t == 0){
int m = max/t
cout << m << endl;
}
}
here you will print every divisor of max.
Personally, i would always open a block for if statements, even if there is only one line in the block, for me it's much more organized and may prevent errors.
Is this your entire program? The variable
int m
is out of scope at the line
cout << m << endl;
which leads me to believe you have another variable named "m" declared earlier in the program that is being shadowed by the newly declared int also named "m" inside the if-block. If this is the case, then the previously declared variable "m" outside the if-block would get printed to cout.

C++ For-Loop Gets stuck when entering new variables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm new to C++ and trying to create a lottery game for a college project.
I have a for loop to check that there are no duplicate numbers in the array entered. This works absolutely fine when you take out the section of code to produce the random numbers.
As soon as I add the random number section back in, the for loop just gets stuck. It will continuously tell me that i have already entered the number when its trying to store the first number.
I have attached all of my code, apologies if you don't need it all.
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
//int loto[6];
int main()
{
int numbers[6];
//void rand_num(int loto[6], int count);
int loto[6]; //used to store the loto numbers
//int james = 0;
//int l,j; //used in checking any duplicated
srand(time(0));
for(int count=0; count<6; count++)
{
loto[count] = (rand()%49)+1;
cout << loto[count] << endl;
}
//declares the variable i to increase each time a number is entered.
//this will only go as high as 6
for(int i=0;i<6;i++)
{
cout<<" " << i<<" : Please enter your lottery numbers: "<<endl;
cin>>numbers[i];
if ((numbers[i] >= 50) | (numbers[i] == 0))
do
{
{
//checks to see if the first number entered is above 50 or = to 0 and rejects it
cout << "The Number must be between 1-49, please select again. " << endl;
cin >> numbers[i];
}
}
while ((numbers[i] >= 50) | (numbers[i] == 0));
//----------------------------------------------------------------------------
//this section of code is a loop within a loop to check the number entered against all numbers already stored.
//makes l the same as i effectively
for(int l=0;l<6;l++)
{
//makes j one more than l
for(int j=l+1;j<7;j++)
{
if( numbers[l] == numbers[j] )
do
{
{
cout << "Number has already been chosen, please re-enter number " << endl;
cout << endl;
cin >>numbers[i];
//checks the number that is re-entered is not <50 or = 0
//if so it rejects it and asks for another as above.
if ((numbers[i] >= 50) | (numbers[i] == 0))
do
{
{
cout << "The Number must be between 1-49, please select again. " << endl;
cin >> numbers[i];
}
}
while ((numbers[i] >= 50) | (numbers[i] == 0));
}
}
while (numbers[l] == numbers[j]);
}
}
}
//----------------------------------------------------------------------------
//this displays the numbers that have been chosen.
cout << "Your Numbers are: " << endl;
for (int i = 0; i<6; i++)
{
cout << " " << numbers[i];
}
return 0;
}
I'm not sure this is the real problem but it is a bug. Try to correct it and see if it helps.
for(int l=0;l<6;l++)
{
//makes j one more than l
for(int j=l+1;j<7;j++)
{
if( numbers[l] == numbers[j] )
The inner-loop will reach j==6 so you will access outside the array. The outer-loop shall have 5 as the limit and the inner-loop shall have 6 as the limit.
EDIT:
After looking a bit more at your code I can see that you are using numbers[] without initializing it. The two nested for-loops will compare all elements in numbers. But if the user have only entered 2 numbers, the rest is unitialized and can give unintended results.
Further - you don't need to check all elements againt all elements every time. Just check the newly entered number (index by i) with all previous numbers.
Finally you will probably need something like:
if (!(cin >> numbers[i])) {
cout << "Please enter numbers only." << endl;
cin.clear();
cin.ignore(10000,'\n');
}
to handle input not being integer, e.g. "text"
And to minor things:
You should also check for negative numbers.
You are using | instead of ||. It will work fine but || seems more correct as it is the logical OR (while | is a binary OR).