Where is the Error in my C++ code? - c++

Here is the error screenshot: http://prntscr.com/9n6ybt
Here is the code:
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin>>a>>b;
for(int i=a;i<=b;i++)
{
if (b%i==0)
{
cout << i << " ";
}
}
return 0;
}

for(int i=a;i<=b;i++)
{
if (b%i==0)
{
cout << i << " ";
}
}
Will give a division by zero if i == 0.
You'll have to check the input, or the value of i, for example:
for(int i=a; i<=b; i++)
{
if (i > 0 && b%i==0)
{
cout << i << " ";
}
}
If i == 0, b%i==0 will not be evaluated.

You are not handling the case where i might be 0 (division by 0) so b % i is indetermined. You can solve it by going this way:
if (i==0) continue;

You should handle the case division by "zero". When the value of i = 0 then the code fails and produce an exception.
You should do like this:
#include <iostream>
using namespace std;
int main()
{ int a, b;
cin>>a>>b;
for(int i=a;i<=b;i++)
{ if (i == 0)
continue;
else if (b%i == 0)
cout << i << " ";
}
return 0;
}

Related

How to show only the last value of a for loop?

I'm working on building a loop right now that only shows the end value. However, the code is showing all the integers though.
Here's my code:
#include <iostream>
using namespace std;
int sum = 0;
void findMultiples(int n){
cout <<"Enter a positive integer:"<<endl;
for(int i = 0; i <= n; i++)
if (i % 3 == 0)
cout << "Sum: "<<(sum =sum+i)<<endl;
}
int main() {
int num;
cin>>num;
findMultiples(num);
return 0;
}
You are using for then if then showing output. The for and if scope area is one line without { }, so you are printing and summing at the same time and it is each time in the scope of if statement.
#include<iostream>
using namespace std;
int sum = 0;
void findMultiples(int n){
cout <<"Enter a positive integer:"<<endl;
for(int i = 0; i <= n; i++)
if (i % 3 == 0)
sum =sum+i;
cout << "Sum: "<<sum<<endl;
}
int main() {
int num;
cin>>num;
findMultiples(num);
return 0;
}
Your cout statements are in the wrong places. Try this instead:
#include <iostream>
using namespace std;
int findMultiples(int n){
int sum = 0;
for(int i = 0; i <= n; i++){
if (i % 3 == 0)
sum += i;
}
return sum;
}
int main() {
cout << "Enter a positive integer:" << endl;
int num;
cin >> num;
cout << "Sum: " << findMultiples(num) << endl;
return 0;
}

How do I output the max int from user input?

I wrote a program that should output the maximum int from user input:
15 20 0 5 -1
should output Max: 20
There seems to be an infinite loop because it never gets to the final cout statement. The while loop should stop with a negative number. It's not outputting anything right now.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int currV;
int maxSoFar;
// 15 20 0 5 -1
// should output 20
cin >> currV;
if (currV > 0)
{
maxSoFar = currV;
}
while (currV > 0)
{
if (currV > maxSoFar)
{
maxSoFar = currV;
}
cin >> currV;
}
cout << "Max: " << maxSoFar;
}
Firstly, I recommend formatting your code better.
Furthermore, your code looks quite confusing and I recommend something like this for what you are trying to do:
#include <iostream>
using namespace std;
int main() {
int currV = 0;
int maxSoFar = 0;
while (cin >> currV)
{
if(currV < 0)
{
cout << "Max: " << maxSoFar;
return 0;
}
if(currV > maxSoFar)
{
maxSoFar = currV;
}
}
}
You need to initialize maxSoFar. Reading/using an uninitialized variable will cause undefined behaviour, then afterward anything could happened. This could be solve by initializing maxSoFar = -1; beforehand.
You can use max() to get maximum value of 2 integers as well.
#include <iostream>
using namespace std;
int main()
{
int currV, maxSoFar = -1; //initializing maxSoFar
while(cin >> currV)
{
if (currV < 0) {break;}
maxSoFar = max(maxSoFar, currV);
}
cout << "Max : " << maxSoFar; return 0;
}
Result:
5 20 0 5 -1
Max : 20
Also, see Why is "using namespace std;" considered bad practice?
Related : What happens to a declared, uninitialized variable in C? Does it have a value?
#include <iostream>
using namespace std;
int main()
{
int currV, maxSoFar = -1;
if (cin >> currV && currV >= 0)
{
maxSoFar = currV;
while (cin >> currV && currV >= 0)
{
if (currV > maxSoFar)
{
maxSoFar = currV;
}
}
}
cout << "Max: " << maxSoFar;
}
Online Demo
int currV;
int maxSoFar = 0; // if all integers are negetive than `maxSoFar` should be 0
while (cin >> currV)
{
if (currV < 0) break;
maxSoFar = max(maxSoFar, currV);
}
cout << "Max: " << maxSoFar;
see this - https://isocpp.org/wiki/faq/input-output#stream-input-failure

Why is my code stopping prematurely? what have i done wrong?

I'm just starting so I'm trying to write a program which determine if a number is positive or negative.
#include <iostream>;
int step_function(int x) {
int result = 0;
if (x > 0)
result = 1;
else if (x < 0)
result = -1;
else
result = 0;
return result;
}
using namespace std;
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
int a = step_function(num);
if (a == 1)
printf("%d is positive", num);
else if (a == -1)
printf("%d is negative", num);
else
printf(" it is zero");
return 0;
}
There is a few things you should do:
First things first you should get yourself a Good Book for C++.
Second thing is read why using namespace std; is a bad idea.
Lastly here is your code fixed. You needed to remove the semicolon as well as removing the printf(). I also removed the using namespace std; which made it more readable.
#include <iostream>
int step_function(int); //Function prototype
int main() {
int num;
std::cout << "please enter number : ";
std::cin >> num;
int a = step_function(num);
if (a == 1)
std::cout << num << " is postive";
else if (a == -1)
std::cout << num << " is negative";
else std::cout <<" it is zero";
return 0;
}
int step_function(int x)
{
int result = 0;
if (x > 0) result = 1;
else if (x < 0) result = -1;
else result = 0;
return result;
}
Don't use semicolon after #include <iostream>.
I think for C++ cout is more standard whereas printf is from C.
You can also include printing of the text in the step_function. Also, it's better to use braces {} after if and else statements for clarity especially if the code becomes complex.
#include <iostream>
using namespace std;
void step_function(int x) {
if (x > 0) {
cout << x << " is positive" << endl;
}
else if (x < 0) {
cout << x << " is negative" << endl;
}
else {
cout << "it is zero" << endl;
}
}
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
step_function(num);
return 0;
}

How to countdown to 0 C++

I need to count down to 0. I am only printing 0 to the screen. How can I print all the count-down characters to the screen? Below is the code I am using right now.
#include <stdio.h>
#include <iostream>
using namespace std;
class Solution {
public:
int num;
int numberOfSteps (int num)
{
while (num != 0)
{
if (num % 2 == 0)
{
num = num / 2;
cout << num;
}
else
{
num = num - 1;
cout << num;
}
}
}
};
int main () {
int num;
Solution myObj;
cin >> num;
cout << myObj.num;
}
You're passing the num to std::cout. You are also not calling numberOfSteps(...) anywhere in your code.
Replacing the line with cout << myObj.numberOfSteps(num); fixes the problem, but a tidier solution would be as follows:
#include <stdio.h>
#include <iostream>
void countDown (int num) {
while (num != 0) {
if (num % 2 == 0) {
num = num / 2;
std::cout << num << std::endl;
} else {
num = num - 1;
std::cout << num << std::endl;
}
}
}
int main () {
int num;
std::cin >> num;
countDown(num);
}
Class is not necessary as there is no state and the function is void since it does not return anything.
I am revisiting this question and have created a simpler solution than my original post:
#include <iostream>
using namespace std;
int num;
int main()
{
cout << "Please enter the number you would like to count down to zero : ";
cin >> num;
while (num > 0)
{
cout << num << endl;
num--;
}
cout << "The number is now zero.";
return 0;
}

Input crash (C++)

Hello i've written a code, but if i want to run the code after i've inputted the code it just crashes.
The program should pick the first 10 out of a vector, pick the highest one and destroy everything before that and refill it until it's ten long again.
I've used a for loop to do this. I've just started with this like 2 months.
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
int Aantal;
cin >> Aantal;
vector<int> F(Aantal);
for(int i=0; i<Aantal; i++)
{
cin >> F[i];
if(Aantal>1000)
{
cerr << "You're above 1000" << endl;
return -1;
}
if(Aantal<20)
{
cerr << "You're below 20" << endl;
return -1;
}
if(i>Aantal)
{
cerr << "Not above 'Aantal'" << endl;
return -1;
}
if(i<0)
{
cerr << "Not smaller than 0!" << endl;
return -1;
}
}
for(int z=0; z<Aantal; z++)
{
if(z == 0){
int a = 0;
}
int a;
vector<int> arr(F.begin()+a, F.begin()+10+a);
int c = *max_element(arr.begin(), arr.end());
cout << c << endl;
vector<int>::iterator q;
q = find(arr.begin(), arr.end(), c);
int pos = distance(arr.begin(), q);
arr.erase(arr.begin(),arr.begin()+pos);
int a1 = 10-arr.size();
int b = a+a1;
a = b;
if(a+10>Aantal)
{
break;
}
arr.erase(arr.begin(), arr.end());
Your problem is here:
int a;
vector<int> arr(F.begin()+a, F.begin()+10+a);
You create a variable a, never initialize it and then you use it to construct the vector. Since a has a garbage value this is going to mess up the vector creation as more than likely you will be getting the wrong positions.
I am not sure what you are doing right before that with:
for(int z=0; z<Aantal; z++)
{
if(z == 0){
int a = 0;
}
As all this does is set a to zero and then you discard a as it was declared in the if block.