What's wrong with this c++ code - c++

I'm a beginner learning code, and I was copying what a YouTube video for programming teaching said. But when I wrote the code, it resulted in some errors.
Here's the code:
#include <iostream>
using namespace std;
int main()
{
int num1 , num2;
cout<< " Enter number 1 and number 2 \n";
cin>> num1 >> num2;
if (num1 == num2);
cout<< "The both numbers are equal \n";
else if (num1> num2)
cout<< "Number 1 is greater than number 2 \n";
else (num1< num2)
cout<< "Number 2 is greater than number 1 \n";
return 0;
}

Note that ; means the expression ends, so you should change
if (num1 == num2);
to
if (num1 == num2)
And else doesn't need condition, so change
else (num1< num2)
to
else

; is not placed after `if` condition
Moreover, else does not get a condition... it always checks the negation of its corresponding if.
In fact if the condition of if does not hold the code in the block of else is executed.... by changing
else (num1< num2)
cout<< "Number 2 is greater than number 1 \n";
to
else
cout<< "Number 2 is greater than number 1 \n";
your problem will get solved.

You dont need ; after if condition check
If you want to do a condition check, you should use else if, in that case else is not enough:
#include <iostream>
using namespace std;
int main()
{
int num1 , num2;
cout<< " Enter number 1 and number 2 \n";
cin>> num1 >> num2;
if (num1 == num2)
cout<< "The both numbers are equal \n";
else if (num1> num2)
cout<< "Number 1 is greater than number 2 \n";
else if (num1< num2)
cout<< "Number 2 is greater than number 1 \n";
return 0;
}

Related

I don`t understand why it adds another 0 in my output. I input 100 and 20 and got 100 and 200 , what can i do?

#include <iostream>
using namespace std;
int add (int num1, int num2)
{
cout << num1 << " " << num2;
return 0;
}
int main ()
{
int num1, num2;
cin >> num1;
cin >> num2;
cout << add (num1, num2);
}
I tried printing two numbers form input and it doesn`t work. i expectet to print the numbers entered but it adds a 0 to the second number.
You are printing the two numbers inside your function add and then in main() you are printing the return value of add() which is 0 - i.e. return 0;
Change cout << add (num1, num2); to add (num1, num2);
or more realistically:
int add (int num1, int num2)
{
return num1 + num2;
}
if you actually want to add the numbers and return the result

c++ to the top of the program

I have to write a program in C++ and am unsure on how to return to the top after an error. For example, I have the user input 2 integers, if the 2nd integer is smaller than the first i have an error stating pls enter in a number larger than the first, but from here I do not know what code to enter to have the question be asked again / send to the beginning / top of the code?
if (num1 > num2)
cout << "You second number must be larger than your first number." << endl;
Problem
I do not know what code to enter to have the question be asked again / send to the beginning / top of the code?
Well almost always when you have that situation, you will use a while loop. This loops over the block if the condition is true.
#include <iostream>
#include <string>
int main()
{
int num1;
int num2;
do {
std::cout << "What is first num? ";
std::cin >> num1;
std::cout << "What is second num? ";
std::cin >> num2;
} while (num1 < num2);
}
Basically, what happens is first you have to declare the integers num1 and num2. Then you have a do while loop! Well this executes the code in the do block before checking for the condition! First we ask for the two user inputs, then we check for the condition. Let's look at the condition carefully:
while(num1<num2)
This means if the first number the user entered is less than the second number, loop through the while block. The while block does the same thing until num1 becomes greater than num2!
Here is a compiled version (GCC).
Additional Exercises
icodecool
Tutorial
CS_PDF
References
cpprefrence
MSDN
Flow Control Tutorial
Glossary:
do-while loop:
Executes a statement repeatedly until the value of the expression becomes false. The test takes place after each iteration.
Syntax
attr(optional) do statement while ( expression ) ;
attr(C++11) - any number of attributes
expression - any expression which is contextually convertible to bool. This expression is evaluated after each iteration, and if it yields false, the loop is exited.
statement - any statement, typically a compound statement, which is the body of the loop
Try:
cout << "Enter number 2: ";
cin >> num2;
while (num1 > num2) {
cout << "You second number must be larger than your first number." << endl;
cout << "Enter number 2: ";
cin >> num2;
}
int num1 = 0, num2 = 0;
do
{
cout << "num1: ";
cin >> num1;
cout << "num2: ";
cin >> num2;
if(num2 < num1)
cout << "error num2 is smaler than num1" << endl;
}while(num2 < num1);

Strange behavior when finding range of values between two numbers

I've recently started working through the "c++ primer 5th Edition". I'm currently on the following exercise:
Exercise 1.11:
Write a program that prompts the user for two integers.
Print each number in the range specified by those two integers.
I have written the following code as a solution :
#include <iostream>
int main(){
int num1 = 0, num2 = 0;
std::cout << std::endl << "Please enter two numbers to find a range between" << std::endl;
std::cin >> num1 >> num2;
if (num1 < num2)
while (num1 <= num2){
std::cout << std::endl << num1;
++num1;
}
if (num2 < num1)
while (num2 <= num1){
std::cout << std::endl << num2;
++num2;
}
if (num1 == num2)
std::cout << std::endl << num1;
std::cout << std::endl;
However when I input the numbers, the output is not quite correct;
Sample input:
Please enter two numbers to find a range betweem
>> 1 5
Sample output:
1
2
3
4
5
5
6
What I don't understand, is that if the first number i input is larger than the first (e.g. num1 > num2), then the program produces the desired result, eg:
Please enter two numbers to find a range between
>> 5 1
1
2
3
4
5
What is particularly confusing is that, when I swap the order of the conditional statements, the first example will work correctly and the second will produce incorrect output.
Just to be clear, I'm aware of a cleaner, correct solution to this exercise. I would just like to know the reason that my program functions this way.
I'd very much appreciate an explanation!
Its so simple.
Just add 'else' for the bottom two if loops
Basically you need to go through only 1 'if' loop, but your program goes through each 'if' loop
Actually you are increamenting your num1 in the first condition when all iterations of while loop completed your second if condition gets true because its the loop exit condition of first if(i.e. num1 <= num2), so your second if triggered and print the last 5 6 .
While in second case your first if not triggered and you reach the second if and hence your flow works properly
As Technoid said, add else statements. Your code should now become:
#include <iostream>
int main(){
int num1 = 0, num2 = 0;
std::cout << std::endl << "Please enter two numbers to find a range between" << std::endl;
std::cin >> num1 >> num2;
if (num1 < num2)
while (num1 <= num2){
std::cout << std::endl << num1;
++num1;
}
else if (num2 < num1)
while (num2 <= num1){
std::cout << std::endl << num2;
++num2;
}
else if (num1 == num2)
std::cout << std::endl << num1;
/* Alternatively for (num1 ==num2) use:
else
std::cout << std::endl << num1;
*/
std::cout << std::endl;
}

Order numbers in c++ and recognize the repeated ones

I was trying to make a program in c++ that should order 3 random numbers (the user will write them) and then print which is the higher and the lower, but if two numbers or more are equal should print it.
New code:
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
cout << "num1" << endl;
cin >> num1;
cout << "num2" << endl;
cin >> num2;
cout << "num3" << endl;
cin >> num3;
if(num1 == num2 && num3==num2 && num1==num3){
cout << "all numbers are equal";
}
else if (num1 == num2){
cout << "num1 and num2 are equal";
}
else if (num2 == num3){
cout << "num2 and num3 are equal";
}
else if(num3 == num1){
cout << "num1 and num3 are equal";
}
else{
if (num1 != num2 && num2 != num3 && num3 != num1){
if (num1 > num2 && num1 > num3){
cout << "higher is num1";
}
else if(num2 > num1 && num2 > num3){
cout << "higher is num2";
}
else if(num3 > num1 && num3 > num2){
cout << "higher is num3";
}
}
}
return 0;
}
New problem:
The programm needs to know which is the lowest too, so how can I do that?
Old code:
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
cout << "num1" << endl;
cin >> num1;
cout << "num2" << endl;
cin >> num2;
cout << "num3" << endl;
cin >> num3;
if(num1 == num2 && num3==num2 && num1==num3){
cout << "all your numbers are equal";
}
if (num1 != num2 && num2 != num3 && num3 != num1){
if (num1 > num2 && num1 > num3){
cout << "num1";
}
else if(num2 > num1 && num2 > num3){
cout << "num2";
}
else /*(num3 > num1 && num3 > num2)*/{//Here I tried to use and else if
cout << "num3";
}
}
return 0;
}
Old problem
This code is all wrong, but I don't know what I'm doing wrong, please help me.
And I have a last question, do I have a limit of if's into a if sentence? or I just can't write two else if or..? Thanks.
#include <iostream>
#include <set>
int main()
{
std::set<int> numbers;
int input;
for (int i=1; i<=3; ++i) {
std::cout << "Enter number " << i << ": ";
std::cin >> input;
numbers.insert(input);
}
if (numbers.size() < 3) {
std::cout << "You entered the same number more than once, silly!" << std::endl;
std::cout << "Nevertheless, ";
}
std::cout << "the maximum number is " << *numbers.rbegin() << std::endl;
}
The important feature of this code is that it takes advantage of the properties of std::set which are:
It keeps all its elements in sorted order
It does not allow duplicate elements
std::set::rbegin is used to get the last element, which is the largest number (because the numbers are automatically sorted).
This code does not display which numbers are the largest and smallest, but this can easily be added. For example, std::set::insert returns information that can allow you to determine which insertion failed in the set. An std::set can contain only single copies of its contained objects. Therefore, if the user enters a number more than once the calls to insert will fail.
The following piece of code solves your purpose. It has three functions findLargest, findSmallest and checkEqual. This is a very basic program and can be modified to your needs on how to display and what to return etc etc.
#include <iostream>
using namespace std;
void findLargest(int n1,int n2,int n3)
{
if(n1>n2 && n1>n3)
{
cout<<"Largest number is :"<<n1;
cout<<"\n";
}
else if((n2>n1) && (n2>n3))
{
cout<<"Largest number is :"<<n2;
cout<<"\n";
}
else
{
cout<<"Largest number is :"<<n3;
cout<<"\n";
}
}
void findSmallest(int n1,int n2,int n3)
{
if(n1<=n2 && n1<=n3)
{
cout<<"Smallest number is :"<<n1;
cout<<"\n";
}
else if((n2<=n1) && (n2<=n3))
{
cout<<"Smallest number is :"<<n2;
cout<<"\n";
}
else
{
cout<<"Smallest number is :"<<n3;
cout<<"\n";
}
}
int checkEqual(int n1,int n2,int n3)
{
if(n1==n2 && n2==n3 && n3==n1)
{
cout<<"All three are equal";
cout<<"\n";
}
else if(n1==n2||n2==n3||n3==n1)
{
cout<<"Two numbers are equal";
cout<<"\n";
}
else
{
cout<<"None are equal.. Finding Largest and Smallest....!!";
cout<<"\n";
}
}
int main() {
int num1,num2,num3;
cout<<"Enter the numbers";
cin>>num1>>num2>>num3;
cout<<"\n";
checkEqual(num1,num2,num3);
findSmallest(num1,num2,num3);
findLargest(num1,num2,num3);
return 0;
}
Ideone link: http://ideone.com/hz4keQ
Hope it helps. :)

C++ Nested for loop and break

I am new to computer science and had a quick question. I was trying to make a program that would take two integer inputs and print out all of the prime numbers in between them.
The problem I am having is that when I use break in the nested for loop. After it finds a prime number, it does not hit the nested for loop on the next go around of the outside for loop. Therefore, when searching for prime numbers between say 8 and 15, it will print out "11 12 13 14 15." It is correct at first but after finding a prime, it states that the rest of the bounded numbers are also primes.
#include <iostream>
using namespace std;
int main()
{
// Prime number finder:
cout << "Enter two numbers and I will find the prime numbers between them.\n\n";
int num1, num2, i = 2;
bool valid;
cout << "Enter the lower limit: ";
cin >> num1;
cout << endl << "Enter the higher limit: ";
cin >> num2;
if (num2 <= num1)
{
cout << "Enter a number that is larger than the lower limit./n";
}
if (num1 <= 1)
{
cout << "1 2 ";
num1 = 3;
}
else if (num1 == 2)
{
cout << "2 ";
num1 = 3;
}
for (num1; num1 <= num2; num1++)
{
valid = true;
for (i; i < num1; i++)
{
if ((num1 % i) == 0)
{
valid = false;
break;
}
}
if (valid == true)
cout << num1 << " ";
}
return 0;
}
The problem is that you don't reset the value of i. Change your loop to:
for (int i = 2; i < num1; i++)
And delete your earlier definition of i, since it's not needed at that point.
Even better, change the whole check of whether the number is prime to a separate funcion which returns a bool.
This should teach you to write functions with a single responsibility. Your function loops through a bunch of numbers, and for each of them it checks whether it is a prime. This latter part belongs to a separate function. What is interesting is that your bug is highly unlikely to appear if you write the separate functions in the first place.
In the for loop below i is uninitialized, you have to set i=2, also set the upper bound as sqrt(num1).
for (i=2; i <= sqrt(num1); i++)
{
if ((num1 % i) == 0)
{
valid = false;
break;
}
}
To use sqrt() also include the following header file as follows,
#include<cmath.h>