Writing program to replace numbers divisible by 3 with the word fizz.(Actually trying to setup with a function but could not get that to work)
This adds the word to the to every third line. How to fix please.
#include <iostream>
#include <cmath>
#include <string>
int value = 0;
int three = 3;
int zero = 0;
int five = 5;
#include <iostream>
usingnamespace std;
int main()
{
int value = 1;
int max = 100;
while (value <= max)
{
cout << value << endl;
value++;
if (value % 3 == 0)
{
cout << "Fizz ";
}
}
system("PAUSE");
return(0);
}
If it's not - i'm sure you could work on it to find what you want.
P.S. You don't really like functions like int three = 3; and int 0 = 0 nor int five = 5;. You didnt even use 0 or 5 in the code - so it's better not to waste valuable lines of code.
Well, sort of. constint basically makes a variable that cannot be changed, whereas #define will go through your code and replace every instance of the #define with whatever is specificed, i.e.:
1 2 3 4 5 6 7 8 9
#define NUMBER 0
constint number = 0;
int main() {
cout << NUMBER; //will cout out 0
cout << "NUMBER"; //will cout out 0
cout << number; //will cout out 0
cout << "number"; // will cout out number
}
@firedraco
In your code above, wy do you think cout<< "NUMBER" would cout out 0? I have tested it and it displays NUMBER.
@magusdknight
You forgot brackets "{}" afther the if-statement: now only the first line falls under it, wich makes the second line will always be executed. And what exactly is the else-statement doing around there? You dont have to use else after if.
Hmm, so it does, (I don't use defines that much >.>) although you still shouldn't use defines to prevent this:
1 2 3 4 5 6
#define NUM 0;
int main() {
int NUMBER; //will create an error, since variables can't start with a number (becomes int 0BER;)
//which is large code is harder to debug then just a redefinition error
}
constint num = 3; works very similarly to int num = 3;, except that the compiler gets upset when you decide to modify the value of num. It does exist as a variable, it's considered by the compiler (and maybe even the operating system) as a nono to change it... This is a *compiler* directive, meaning this declaration changes the way this code is interacted with during *at compile time*, after it has decided exactly what your code says.
#define num 3 will replace the code identifier num with the value 3. It's a *pre-compiler directive*, meaning it changes code interaction *before compile time*, while it's still deciding on the exact wording of your code.
Also, I want to make a couple comments about the code that magusdknight posted (see the comments)
#include <iostream>
#include <cmath>
#include <string>
// don't let yourself make a habit of making global variables.
// they lead to sloppy code that you'll have difficulty expanding
// and porting later. these should be declared in main.
int value = 0;
int three = 3;
int zero = 0;
int five = 5;
usingnamespace std;
int main()
{
int max = 100;
do
{
value = value++; // reduntant.... "value = value++" could be simply "value++"
cout << value << "\n";
if(value % three == 0)
// missing "{" here.
string fizz = "Fizz";
cout << fizz;
// missing "}" here.
else // this else has no code attached to it... this throws a compiler error?
}
while ( value < max);
system ("PAUSE");
return 0;
}