This question already has answers here:
Why do some experienced programmers write comparisons with the value before the variable? [duplicate]
(12 answers)
Closed 2 years ago.
Out of the two versions (below) of condition check which version is better? and Why?
Version 1:
#include<iostream>
using namespace std;
int main()
{
int x;
cin >> x;
if (x == 1) // version 1
{ cout << "Hello world!" << endl;
}
return 0 ;
}
Version 2:
#include<iostream>
using namespace std;
int main()
{
int x;
cin >> x;
if (1 == x) // version 2
{ cout << "Hello world!" << endl;
}
return 0 ;
}
These are the same, the only advantage I can see relates do error detection.
Lets say you mistakenly write 1 = x, you will have a compilation error.
If you write x = 1 then the condition will evaluate to true, x will be assigned the value 1, the program will compile fine but it will not have the expected ouptput and it might be hard to detect this kind of error, though you have compiler warnings that you can turn on for this kind of situation.
Related
This question already has answers here:
Getting a weird percent sign in printf output in terminal with C
(3 answers)
Closed last month.
I am writing code and its output is a little different from the regular ones.
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
cout << "Value of x: " << x;
return 0;
}
I expected only a integer but i got an output like this:Output of the above code
I would say that's you shell's prompt. You didn't print a new-line character ('\n').
This question already has answers here:
How to handle wrong data type input
(4 answers)
Closed 2 years ago.
I'm sorry if this question is stupid, but it's been kind of bugging me. I have written a program that is supposed to accept user input 5 times and then print out the result each time (i am using a while loop.) Here is the code I wrote:
#include <iostream>
int main()
{
int x = 1;
int number;
while (x <= 5)
{
std::cin >> number;
std::cout << number << std::endl;
x++;
}
return 0;
}
However, after compiling and running (i'm using clang) the program only lets me insert user input once and then it just prints a bunch of 0's:
jakdfjaksdfjk
0
0
0
0
0
I am really confused why this behavior happens. Shouldn't you be able to pass in user input 5 times? Why does this behavior happen? Help would really be appreciated.
You are trying to read an integer and "jakdfjaksdfjk" would be a string, that's why that happens. Type something like 1 4 8 35 42 and it'll work as you expect
You should consider checking the validation of std::cin:
#include <iostream>
int main(void) {
int x = 1;
int number;
while (x++ <= 5) {
std::cin >> number;
// If the input isn't an integer, the breaks the loop and quit
if (!std::cin.good()) {
std::cout << "Numbers only please.\n";
break;
}
// Otherwise, simply print...
std::cout << number << std::endl;
}
return 0;
}
Honestly am shocked I am getting an error. I'm a junior CS major and I can't get this simple program to work. Clion says these two lines are unreachable, yet my test cases seem to work.
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "";
while(s != "|") {
int val1 = 0;
int val2 = 0;
cin >> val1;
cin >> val2;
if(val1 == val2) {
cout << "the numbers are equal.\n";
} else {
cout << "the smaller value is: " << min(val1, val2) << '\n'; // Says these two
cout << "the larger value is: " << max(val1, val2) << '\n'; // lines are unreachable
}
cin >> s;
}
return 0;
}
Test Cases:
3 3
the numbers are equal.
f
4 5
the smaller value is: 4
the larger value is: 5
|
Process finished with exit code 0
If this code is so unreachable than how come my program reached it?
There may be a few problems wrong with CLion
This is the one which caught my attention:
You check whether the string is equal to a chat array this may get resolved at runtime but the code checker doesn’t like it. Try using :
char s;
while(s!='|') {...}
Other than that I have no idea...
It may not have predicted the change to the variables, try using the volatile keyword? This may help... That is still a bug.
This question already has answers here:
Factorial does not work for all values
(2 answers)
Closed 4 years ago.
I'm new at c++, I'm trying to create a factorial calculator, but I'm having trouble calculating n! when n > 15.
Here's the Code:
#include <iostream>
using namespace std;
int calculate_factorial(int num)
{
int fact = num;
for (int i = fact - 1; i > 0; i--)
{
cout << i << " " << fact << endl;
fact *= i;
}
return fact;
}
int main()
{
int num = 16;
cout << calculate_factorial(num) << endl;
system("pause");
}
Apart from the fact that calling it a sum is just... no. Anyway, int can only go up to 2^31 - 1, and that's only good for 12!. Even changing it to unsigned int won't change that, and even using unsigned long long int would only allow you to go up to 20!.
Your result is overflowing. You might wanna look into the GNU Multiple Precision library.
This question already has answers here:
unexpected output in cout and printf [duplicate]
(4 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
I have the following code:
#include<iostream>
using namespace std;
int main()
{
int x=3;
cout<<x++<<++x<<x++<<++x<<endl;
return 0;
}
the output should be 3557
but it is 6747. why???
Also:
#include<iostream>
using namespace std;
int main()
{
int x=3;
cout <<x++<<endl;
cout<<++x<< endl;
cout<<x++<< endl;
cout<<++x<< endl;
return 0;
}
The above code gives:
3
5
5
7
(every digit in new line)
Can anyone explain why?
int x=3;
cout<<x++<<++x<<x++<<++x<<endl;
This is undefined behavior, so you could easily get any result.
int x=3;
cout <<x++<<endl; // x = 3, post incremented to 4...
cout<<++x<< endl; // here x = 4 and preincremented to x = 5
cout<<x++<< endl; // x = 5 , postincremented to 6...
cout<<++x<< endl; // so here x = 6 but preincremented to 7
Undefined behavior and sequence points
unexpected output in cout and printf