-
March 2nd, 2009, 12:27 PM
#1
[RESOLVED] Invalid Operand for the Type?
||In function `int main()':| C:\Documents and Settings\bleas\My Documents\Backup\Projects\Accelerated C++ Projects\Chapter 2\Quartiles_1.0.cpp|32|error: invalid operands of types `double' and `double' to binary `operator%'|
This error specifically refers to:
Code:
if(quarter_d % 2 == 0)
{
quarter = size/4;
}
{
quarter = size/4 + 1;
}
I'm trying to write a program that shows a group of numbers in quartiles, depending on the size. Since some numbers are not evenly divided by 4, and I can't show half a number, I decided I would try to round any such number up by one. So something like 75 numbers (divided by four is 18 and some change) would instead show 75-19, 17-19*2, etc... however, the error above is what I get for trying the module. Looking for a bit of help please.
Code:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <string>
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to Quartiles 1.0!"
<< endl
<< "Please enter your numbers...";
vector<int> numbers;
int run = 1, current_number;
while(cin >> current_number)
{
numbers.push_back(current_number);
}
sort(numbers.begin(), numbers.end());
typedef vector<int>::size_type vec_sz;
vec_sz size = numbers.size();
int quarter;
double quarter_d = size/4;
if(quarter_d % 2 == 0)
{
quarter = size/4;
}
{
quarter = size/4 + 1;
}
int counter = 0, current_quarter = 1;
while(counter != quarter)
{
if(current_quarter == 1)
{
cout << numbers[quarter*3+counter] << endl;
}
else if(current_number == 2)
{
cout << numbers[quarter*2+counter] << endl;
}
else if(current_number == 3)
{
cout << numbers[quarter+counter] << endl;
}
else if(current_number == 4)
{
cout << numbers[counter] << endl;
}
++counter;
if(counter == quarter && current_number < 4)
{
cout << endl << "Next Set" << endl << endl;
++current_number;
counter = 0;
}
}
return 0;
}
Last edited by BleaS; March 2nd, 2009 at 06:24 PM.
-
March 2nd, 2009, 12:48 PM
#2
Re: Invalid Operand for the Type?
The problem is that operator% is not defined for double. You could use std::fmod() from <cmath>, but in this case you probably should just stick to using int instead of double.
-
March 2nd, 2009, 12:54 PM
#3
Re: Invalid Operand for the Type?
The problem with using int is that it will not retain the decimals. I need the decimals to determine whether or not it needs to go up one number. As you can see, it eventually becomes an int, but it has to be a double to determine the true value I need to sort with.
Edit:
Oops, I did change it to
Code:
int quarter = size/4 == 0 ? quarter = size/4
: quarter = size/4 + 1;
Seems to work, but my end output is not what I expected, so I have some more looking to do.
Last edited by BleaS; March 2nd, 2009 at 12:58 PM.
-
March 2nd, 2009, 01:01 PM
#4
Re: Invalid Operand for the Type?
Originally Posted by BleaS
The problem with using int is that it will not retain the decimals. I need the decimals to determine whether or not it needs to go up one number.
Notice that 75 % 18 == 3. Basically, you can use operator% on int to decide if your range is perfectly divisible by 4, and based on that you can adjust the sub-ranges.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|