Random Insanity Alliance Forum, Mark V
Cactuar Zone => Random lnsanity => Topic started by: SWAT128 on February 22, 2012, 01:02:46 am
-
#include <iostream>
using namespace std;
void divisibleBy (int x){
void divisibleBy (int x){
if (x % 5){
cout << "is divisible by five " << endl;
}else{
cout << "is not divisible by five " << endl;
}
if (x % 10){
cout << "and divisible by ten" << endl;
}else{
cout << "but not divisible by ten" << endl;
}
}
int main()
{
int b;
char Answer;
cout << "Please enter a number" << endl;
cin >> Answer;
divisibleBy(Answer);
return 0;
}
I'm making a function to see if a number the user inputs is divisible by five and ten. The problem I'm having is that no matter what number I put in, it says it's divisible by both five and ten.
Edit: Made that change and now it's telling me numbers that are divisible by five aren't.
-
#include <iostream>
#include <cmath>
using namespace std;
void DivisibleBy(int);
void main()
{
int A=0;
cout << "Please enter a number" << endl;
cin >> A;
DivisibleBy(A);
System("Pause");
}
void DivisibleBy(int A)
{
if (A%5==0)
{
cout << endl<< A << " is divisible by 5"<<endl;
if (A%10==0)
cout << endl<< A << " is also divisible by 10"<<endl;
}
}
Gimmie a min and I'll show corrections on yours
-
#include <iostream>
using namespace std;
void divisibleBy (int x){ You should only have prototypes above the main
if (x % 5){ Using modulus is good, but you need to compare it to something, x%5==0 should be the condition. Basically, you will use modulus to get the remainder, and compare it to 0, if it's 0, then it's divisible by 5.
cout << "is divisible by five " << endl;
}else if (x%!5){ A bit repetitive, you don't need an else if here, you can use just an else.
cout << "is not divisible by five " << endl;
}
if (x % 10){ This should be within the If of the check to see if it is divisible by 5
cout << "and divisible by ten" << endl;
}else if (x%!10){
cout << "but not divisible by ten" << endl; This is unnecessary, but you could put it into the else where it says not divisbile by 5 because if it's not divisible by 5, it's definatly not divisible by 10
}
}
int main()
{
int b;
char Answer;
cout << "Please enter a number" << endl;
cin >> Answer; you need them to input a number, so this sould be cin >> b; Thts because your using b as your variable. Answer is a char, which would mean that it would take th enumber, and convert it to an ascii value.
divisibleBy(Answer);
return 0;
}
-
Write a C++ function called divisibleBy that takes an integer as a parameter, and prints out the integer and then "is divisible by five" if the integer is evenly divisible by 5, otherwise it prints "is not divisible by five". If the number IS evenly divisible by 5, also print " and divisible by ten" if it is also divisible by 10, otherwise print " but not divisible by ten".
I have to make it say that it's not divisible by five or ten if it isn't. That's where I'm having trouble, it keeps telling me that everything is divisible by five and ten.
-
#include <iostream>
using namespace std;
void divisibleBy (int x){
if (x % 5==0){
cout << "is divisible by five " << endl;
}else{
cout << "is not divisible by five " << endl;
}
if (x % 10==0){
cout << "and divisible by ten" << endl;
}else{
cout << "but not divisible by ten" << endl;
}
}
int main()
{
int b;
char Answer;
cout << "Please enter a number" << endl;
cin >> Answer;
divisibleBy(Answer);
return 0;
}
It's still giving me fucked up answers.
-
This is how I would do it, personally:
#include <iostream>
using namespace std;
int main()
{
int input(0);
bool five(0), ten(0);
cout << "Please enter a number: ";
cin >> input;
cout << endl;
if (input%5==0) {five = true;}
if (input%10==0) {ten = true;}
if (five==false && ten==false) {cout << "This number is not divisible by either five or ten.\n";}
if (five==true && ten==false) {cout << "This number is divisible by five, but not by ten.\n";}
if (five==false && ten==true) {cout << "This number is not divisible by five, but is by ten.\n";}
if (five==true && ten==true) {cout << "This number is divisible by both five and ten.\n";}
return 0;
}
Though I don't see anything wrong with Apophis' code.
-
Swat, read the corrections i did to your code. Your asking for a number, taking that number in as a character. which means that when you try to use it as a number, you get a different value.
-
In other words, change
char Answer;
to
int Answer;
-
Crap...and here i was, about to regal you all with my Indian coding prowess, effectively reinforcing one of the most widely recognised stereotypes against indian society, when Pterrydactyl and locke came along to steeeeel all mah gloreh...
sorry swat, i promise ill be first to reply, the next time you're blind and stupid.
-
Everything is divisible by five and ten.
It just won't always be a whole number.
-
Damn it. Thanks for making my brain hurt. I learned C in school, not C++, so the syntax still fucks with me. My roommate took a C++ class a couple years later. I helped him out with one of his assignments. Problem was, he didn't tell me it was C++ and not C, so he was less than happy when the teacher asked him who did his assignment for him.
But yeah, char != int.
-
Im learning python, learnt a bit of c in coll, this shite is easy.
-
Im learning python, learnt a bit of c in coll, this shite is easy.
A lot of the software I use now uses python scripting. That would be very useful to know, but I've never gotten into it.
-
Python is by far, the simplest language i've read.
Go learn some.
-
Python is simple as balls.
-
Focus on c++. Bitches love C++.
-
you guys.
sorcery.
stop it.
-
The correct term is wizardry. Sorcerers just polish their ass to level, while wizards actually research and learn shit.
-
There is so much old code out there that no one is willing/able to upgrade, that learning Fortran is still a marketable skill. If nothing else, get paid to convert it to C++. Though Fortran reads just like C, so it's pretty understandable without too much actual knowledge. You could probably convert it anyway.
-
The correct term is wizardry. Sorcerers just polish their ass to level, while wizards actually research and learn shit.
BY THAT DEFINITION, I DO MEAN SORCERER FOR YOU
-
C++ is great for some things. Python for others. Love em both, throw in Java and you got my main three. :wub:
-
java...
-
Java....................
-
The correct term is wizardry. Sorcerers just polish their ass to level, while wizards actually research and learn shit.
I prefer "sourcerery."
That hurt.
java...
Java....................
Java............................................
-
RAWKS BUT IS DYING11!!!!11!!!!!!
-
I cast level 6 array sort