First: I'm begginer. :) I've got a problem with my c++ code.
#include <iostream>
using namespace std;
int main()
{
int a,b;
do {
cout << "a= ";
cin >> a;
if (a<=0) {
cout << "This number is not positive!\n";
}
}
while (a<=0);
do {
cout << "b= ";
cin >> b;
if (b<=0) {
cout << "This number is not positive!\n";
}
}
while ((a==b) and (b<=0));
}
Have you got any ideas?
Thanks!
It is not possible for that condition to be true. We already know that a is positive, so it can't both equal b and b be negative.
Sounds to me like you want or instead. This would mean that b also has to be positive and must not be the same as a. Note that it is typical to use && instead of and and || instead of or:
while ((a==b) || (b<=0));
Think about it like this: we can't to continue asking for b if they input is negative or if the input is the same as a.
Related
This is the script of it, I can't find any issue with it, I tried a lot of things like adding a equality sign after the multiplying one, but i didn't change anything I also get a lot of blue lines in logs that say: "suggest parentheses around assignment used as truth value"
#include <iostream>
using namespace std;
int main()
{
int attackfirst;
int zycieofiary;
int tarczaofiary;
int efektywnosctypu;
cout << "Podaj atak atakujacego: ";
cin >> attackfirst;
cout << "Podaj tarcze: ";
cin >> tarczaofiary;
cout << "Podaj zycie ofiary: ";
cin >> zycieofiary;
cout << "Podaj efektywnosc typu: ";
cin >> efektywnosctypu;
tarczaofiary=tarczaofiary/2;
attackfirst=attackfirst-tarczaofiary;
if(efektywnosctypu=1) {
attackfirst=0;}
if(efektywnosctypu=2) {
attackfirst=attackfirst*=0.75;}
if(efektywnosctypu=3) {
attackfirst=attackfirst;}
if(efektywnosctypu=4) {
attackfirst=attackfirst*=1.25;}
if(efektywnosctypu=5) {
attackfirst=attackfirst*=1.5;}
zycieofiary=zycieofiary-attackfirst;
cout << "Zycie ofiary to: ";
cout << zycieofiary;
return 0;
}
if(efektywnosctypu=1) {
attackfirst=0;}
if(efektywnosctypu=2) {
attackfirst=attackfirst*=0.75;}
if(efektywnosctypu=3) {
attackfirst=attackfirst;}
if(efektywnosctypu=4) {
attackfirst=attackfirst*=1.25;}
if(efektywnosctypu=5) {
attackfirst=attackfirst*=1.5;}
I'm quite sure the "="s in the if statements are supposed to be "=="s. In most languages "=" is only used for assignment.
And also, the "*="s should be "*"s since you aren't trying to assign a value, you are trying to multiply. :)
I made here a program to do 3x+1 math problem. So I am asking, if I could write in a c++ code something like x/2;, or x*3+1. These stuff what i put here are with mistakes. Then, is it possible in c++ to do that? If yes, how?
Here's the code:
#include <iostream>
using namespace std;
int main() {
cout << "Write an integer.\n"; int x; cin >> x;
// I made here a program to do 3x+1 math problem.
while (x==1) {
if ((x%2)==0) {
x/2; cout << x << ", ";
} else if ((x%2)==1) {
x*3+1; cout << x << ", ";
}
}
return 0;
}
The output there was:
/tmp/GjudkYOaE4.o
Write an integer.
9
But I was waiting it to write the number 28, 14, and more, but it did nothing.
I can see that you are new to coding, I would suggest you to please read a good amount of information about if-elseif-else and while loop that you are using
I can show you a few corrections here
#include <iostream>
using namespace std;
int main() {
cout << "Write an integer.\n";
int x; cin >> x;
// I made here a program to do 3x+1 math problem.
while (x!=1) { // x!=1 could be one of the terminating condition so when your x becomes 1, the loop would end. But of course the terminating condition depends on what you are trying to achieve
if (x % 2 == 0) {
x = x/2; //you need to assign the value back to x
cout << x << ", ";
} else { // you dont need else-if here because there could only be 2 possibilities for %2 operaion here
x = x*3+1;
cout << x << ", ";
}
}
return 0;
}
Give value to the same variable with assignment operator ('='):
x = x*3+1;
or
x = x/2;
I have those two pieces of code as my home assignment. The code looks all fine to me, but it won't print out what I want, no matter what. In fact, the console output remains completely empty.
The first program is supposed to print out all numbers that fulfil the ladna() function requirements and are between 1 and a:
#include <iostream>
using namespace std;
int a;
int i = 1;
bool ladna(int a)
{
if((((a>>4)*5+a*2)%3)==1)
return true;
else
return false;
}
int main()
{
cerr << "Podaj liczbe: " << endl;
cin >> a;
while (i <= a){
if (ladna(a)){
cout << i << " ";
}
i++;
}
}
the ladna() function is premade and I have to use it as is.
I tried changing while into do...while and for, didn't help. Doesn;t work with cerr either.
The second code has to print out all the natural divisors of number a.
#include <iostream>
using namespace std;
int main()
{
int a;
cerr << "Podaj liczbe" << endl;
cin >> a;
for (int i = 0; i >= a; i++){
if (a % i == 0){
cout << i << endl;
}
}
return 0;
}
Doesn't work either.
To me it looks like both pieces of code have the same issue, because they are written in the same way, based on the same principle, and the error is the same. Hence my assumption, that the cause is the same as well.
Unfortunately, for the love of me, I simply can't see what said error is...
For the first code:
I think you should call ladna function with i, like ladna(i)
For the second code:
In for it should be i<=a
'%' is the modulo operator, during the execution of (a%i) you divide a with i and take the remainder, since i start with zero you will get "Floating point exception (core dumped)" due to division by zero. So, for should start with 1. This should work:
for (int i = 1; i <= a; i++){
if (a%i == 0){
cout << i << endl;
}
}
As of late, I've been doing a complete review of C++ and came across a code snippet containing the following:
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a)
{
b+=a;
}
cout << b << endl;
return 0;
}
The code snippet seems very straightforward: it puts input from the console into a and adds this onto b, for as long as valid input is presented. However, whenever I try to run this program with an input of integers, seperated with spaces, it gives a very large negative integer (-1218019327 being the most recent result, when the entire input only consisted of the number '1'). Only when I modify the code does it give correct output:
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a)
{
cout << a << endl;
b+=a;
}
cout << b << endl;
return 0;
}
Why does adding a cout statement change the result of my code so thouroughly (yet positively)?
Both programs result in undefined behavior, you did not initialize b. Try:
int b = 0;
You have to initialize b=0;. Or b will give you garbage value.
#include <iostream>
using namespace std;
int main()
{
int a, b=0;
while (cin >> a)
{
cout << a << endl;
b+=a;
}
cout << b << endl;
return 0;
}
By pressing ctrl-z you will get the value of b.
The loop in the function require() takes 3 conditions, a > b or "a" or "b" aren't digits. Even when I don't satisfy the conditions and put 2 integers in, it just loops once again.
Also when I put in a character then it just endlessly loops "Enter minimum number Enter maximum number" ignoring the cins. Anyone know why? I'm a beginner so this is probably really obvious
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int random(int minN, int maxN) //generates random number within specified range
{
srand (time(NULL));
int x = (maxN - minN);
int y = minN + (rand() % (x+1));
return y;
}
int require() //makes sure a < b and both are digits
{
int a,b;
do {
cout << "Enter minimum number" << endl;
cin >> a;
cout << "Enter maximum number. Note: Has to be greater or equal to minimum." << endl;
cin >> b;
} while (a > b || !isdigit(a) || !isdigit(b));
return random(a,b);
}
int main()
{
cout << require() << endl;
}
You should not use isdigit as this relates to a particular character is a digiti. Instead the loop should look like this:
int require() //makes sure a < b and both are digits
{
validNumbers = true;
do
{
cout << "Enter minimum number" << endl;
cin.clear();
cin >> a;
} while (cin.fail());
do
{
cout << "Enter maximum number. Note: Has to be greater or equal to minimum."
<< endl;
cin.clear();
cin >> b;
} while (cin.fail() || a > b);
return random(a,b);
}
PS: You only need to call srand (time(NULL)); once at the start of the program.
You are reading the numbers as, well, numbers not as characters as the isdigit function expects. If you are using a C++11 compliant standard library, the values of a and b will actually be zero if the input is not valid integer numbers, which means that e.g. !isdigit(a) will be true. If you are using a non-C++11 library, then the value of a and b will be random, and will most likely cause !isdigit(a) to be true as well as the amount of valid digit ASCII values in a full 32-bit integer range is quite small.
If you read a reference about the input operator, like this one you will see that if extraction fails, then the streams failbit will be set. This can either be tested "inline" like this:
if (!(std::cin >> a))
{
std::cout << "Not a valid number, try again: ";
continue;
}
Or it can be tested using the streams fail function.