I have to write a program to test an integer value to determine if it is odd or even, and make sure my output is clear and complete. In other words, I have to write the output like "the value 4 is an even integer". I was also hinted that I have to check the value using the remainder modulo.
The issue I have is with the scanf() function. I get a syntax error:
'%=' expected a ')'
How do I fix this?
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
int scanf(%=2 , &number);
if (number == 0)
cout << "the value" << number << "is even";
else
cout << "the value" << number << "is odd";
return 0;
}
You are using scanf() incorrectly (read the scanf() documentation on cppreference.com). The first parameter expects a null-terminated string containing the format to scan, but you are not passing in anything that even resembles a string. What you are passing in is not valid string syntax, per the C++ language standard. That is why you are getting a syntax error.
You need to change this line:
int scanf(%=2 , &number);
To this instead:
scanf("%d", &number);
Though, in C++ you really should be using std::cin instead for input (you are already using std::cout for output):
std::cin >> number;
Try this:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
if (cin >> number)
{
if ((number % 2) == 0)
cout << "the value " << number << " is even";
else
cout << "the value " << number << " is odd";
}
else
cout << "the value is invalid";
return 0;
}
I know this question is a little dated, however, if you are able to use modern C++ features. You can write a constexpr helper function such as this:
#include <cstdint>
constexpr bool isEven(uint32_t value) {
return ((value%2) == 0);
}
Then in your main function, you can traverse through a loop of N integers and output your display such as:
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isEven(i) ? "even" : "odd") << '\n';
}
return 0;
}
It's literally that simple. Here's another nice feature of using the constexpr helper function... You can also format your output as such:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isEven(i) << '\n';
}
return true;
}
If you are looking for something that is more efficient than using the modulo operator you can bitwise & with the least significant digit... The code above would then become:
#include <cstdint>
constexpr bool isOdd(uint32_t value) {
return (value&1);
}
And using it would be very similar as above, just make sure you reverse the wording in your output to match that from the function being used...
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isOdd(i) ? "odd" : "even") << '\n';
}
return 0;
}
Again you can use the std::boolalpha manipulator to get this kind of output:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isOdd(i) << '\n';
}
return true;
}
Question #1: How can I build a constructor set the value for (R,PoF,PoR)? I am trying to understand how constructor works but I guess I don't quite get it.
Question #2: Can I build destructor in this way, instead of the way I used in my program?
Circle::~Circle()
{
std::cout << "The fence would cost " << SwimmingPool.PerimeterP(r) << std::endl;
std::cout << "The road would cost " << SwimmingPool.AreaP(r) << std::endl;
std::cout << "Present by FF" << std::endl;
}
I just want the cost to come out by itself, but I don't know how should I create destructor to do so.
Here is my full code:
#include "stdafx.h"
#include "iostream"
const double PI = 3.1415926;
class Circle
{
public:
Circle();
double AreaP(int r);
double PerimeterP(int r);
~Circle();
private:
int R;
int PoF;
int PoR;
};
double Circle::AreaP(int r)
{
return ((r + R)*(r + R) - r*r)*PI*PoR;
}
double Circle::PerimeterP(int r)
{
return (r + R) * 2 * PI*PoF;
}
Circle::Circle()
{
int R = 3;
int PoF = 35;
int PoR = 20;
}
Circle::~Circle()
{
std::cout << "Present by FF" << std::endl;
}
int main()
{
int r;
Circle SwimmingPool;
std::cout << "Please input the radius of the Swimming Pool." << std::endl;
std::cin >> r;
std::cout << "The fence would cost " << SwimmingPool.PerimeterP(r) << std::endl;
std::cout << "The road would cost " << SwimmingPool.AreaP(r) << std::endl;
return 0;
}
You have:
Circle::Circle()
{
int R = 3;
int PoF = 35;
int PoR = 20;
}
That function creates three function local variables and sets their values. It does not initialize the members of the class. Change it to:
Circle::Circle() : R(30), PoF(35), PoR(20) {}
Always prefer to initialize in the initializer list instead of setting the values in the body of the constructor.
No, you may not use:
Circle::~Circle()
{
std::cout << "The fence would cost " << SwimmingPool.PerimeterP(r) << std::endl;
std::cout << "The road would cost " << SwimmingPool.AreaP(r) << std::endl;
std::cout << "Present by FF" << std::endl;
}
SwimmingPool is a variable in main. It cannot be used in the destructor. Besides, it does not make sense to print those messages in the destructor. It should simply be
Circle::~Circle()
{
}
Circle::Circle() : R(3), PoF(3), PoR(3) {};
Define the R, PoF, PoR as const int
The destructor must not throw an exception and generally you want it to release resources acquired by the object. Usually not the best place to be outputting stuff to stdout.
Don't use std::endl unless you want to flush the stream. Use '\n' instead.
I have never experienced anything like this. I was using a cout statement to help me debug a small program, and once I was satisfied with my code I commented out the cout. Now, the code no longer works. Below is the code with the cout commented out.
The intent of this program is to test the two hard coded boolean two dimensional arrays for having an odd number of true statements on each row. Thus, the first array should return true and the second array should return false. With the cout statement commented out both instead return false.
#include <iostream>
using namespace std;
template <size_t size_y>
bool findEvenDegrees(bool mapArray[][size_y])
{
bool returnValue;
for (int x=0; x<size_y; x++)
{
int result = 0;
for (int y=0; y<size_y; y++)
{
result = result + mapArray[x][y];
//the line below causes the problem
cout << mapArray[x][y] << "\t" << result << "\t" << x << endl;
}
if (result%2 == 1)
{
returnValue = false;
break;
}
}
if (returnValue== false)
{
return returnValue;
}
else
{
return true;
}
}
int main()
{
bool array1[][6] =
{
{false,true,true,false,false,false},
{true,false,false,true,false,false},
{true,false,false,true,false,false},
{false,true,true,false,true,true},
{false,false,false,true,false,true},
{false,false,false,true,true,false}
};
bool array2[][8] =
{
{false,true,true,false,false,false,false,false},
{true,false,false,true,false,false,false,false},
{true,false,false,true,false,false,false,false},
{false,true,true,false,true,false,false,false},
{false,false,false,true,false,true,true,false},
{false,false,false,false,false,true,false,true},
{false,false,false,false,true,false,false,true},
{false,false,false,false,false,true,true,false}
};
bool answer1 = findEvenDegrees(array1);
bool answer2 = findEvenDegrees(array2);
if (answer1 == true)
{
cout << "Array 1 has a even degree for every switch." << endl;
}
else
{
cout << "Array 1 has a odd degree for at least one switch." << endl;
}
if (answer2 == true)
{
cout << "Array 2 has a even degree for every switch.";
}
else
{
cout << "Array 2 has a odd degree for at least one switch.";
}
return 0;
}
You never initialize returnValue. If it happens to start out as false it will stay that way and the function will return false.
First, I cleaned up your code a little, and arrived at:
#include <iostream>
template <size_t S>
bool findEvenDegrees(bool (&themap)[S][S]) {
for( bool(&row)[S]: themap ) {
bool is_degree_odd = false;
for( auto col: row )
is_degree_odd ^= col;
if( is_degree_odd )
return false;
}
return true;
}
int main()
{
using std::cout;
using std::endl;
bool array1[6][6] = {
{false,true,true,false,false,false},
{true,false,false,true,false,false},
{true,false,false,true,false,false},
{false,true,true,false,true,true},
{false,false,false,true,false,true},
{false,false,false,true,true,false}
};
cout << "Array 1 has an "
<< (findEvenDegrees(array1) ? "even degree for every" : "odd degree for at least one")
<< " switch." << endl;
bool array2[8][8]= {
{false,true,true,false,false,false,false,false},
{true,false,false,true,false,false,false,false},
{true,false,false,true,false,false,false,false},
{false,true,true,false,true,false,false,false},
{false,false,false,true,false,true,true,false},
{false,false,false,false,false,true,false,true},
{false,false,false,false,true,false,false,true},
{false,false,false,false,false,true,true,false}
};
cout << "Array 2 has an "
<< (findEvenDegrees(array2) ? "even degree for every" : "odd degree for at least one")
<< " switch." << endl;
return 0;
}
In the process of cleaning it up, I eliminated the if(result%2 == 1) { resultValue = true; break; }, by effectively returning when I found the first odd-degree row. As I eliminated the resultValue variable, I also killed the "unitialized" bug mentioned by #sth.