C/C++ :: Double Comparison For Range Is Not Working Correctly

Mar 17, 2015

I am trying to compare a double to be within various ranges, but the comparison is not working correctly. Rounding precision is not a concern, because if the value will match the boundaries so rarely, and it is rounding up to the higher range is acceptable. Below is the doe at issue:

if (kq == 2) {
if (fundt[kq-1][ky-1][2-1] < 450,000,000.00) ktti[ky-1][0] = 1;
else if ((fundt[kq-1][ky-1][2-1] >= 450,000,000.00) && (fundt[kq-1][ky-1][2-1] < 525,000,000.00)) ktti[ky-1][0] = 2;
else if ((fundt[kq-1][ky-1][2-1] >= 525,000,000.00) && (fundt[kq-1][ky-1][2-1] < 650,000,000.00)) ktti[ky-1][0] = 3;
else if ((fundt[kq-1][ky-1][2-1] >= 650,000,000.00) && (fundt[kq-1][ky-1][2-1] < 750,000,000.00)) ktti[ky-1][0] = 4;
else if ((fundt[kq-1][ky-1][2-1] >= 750,000,000.00) && (fundt[kq-1][ky-1][2-1] < 850,000,000.00)) ktti[ky-1][0] = 5;

[Code] ....

I know that a triple-dimension array looks complex, but I can guarantee that is a double. The literal constants I am using should automatically be doubles. The subscript of [2-1] looks odd, but this is code I was given to maintain, and that is how it is written elsewhere, so I kept it for consistency. The problem I am having is that when I run this code, all the data I input is less than the 450 million value, but the run falls through all the if and if-else conditions, and the else code is what is actually executed, i.e., I always get six as my result.

I am running on a Sun Sparc, and the compiler used is SunStudio 12. I have tried using variables, with the values listed above assigned to each variable, but it does work either. When I use variables, if the input value is negative, the comparison for less than 450 million works, but any positive input values will through to the else and give me six.

View 1 Replies


ADVERTISEMENT

C++ :: Vector Comparison Out Of Range?

Jul 31, 2013

I'm making a simple game and I'm having trouble creating boundaries for the players movements. The map is a 2D vector. When I compare the vector with the players current position sometimes I get an error during run. When the error isn't occurring the behavior of the boundaries is bizarre. The error tells me that the vector is out of range.

Here is the where the map is created. It's within its own class constructor.

vector< vector<char> > map_parts;
map_parts.resize(25);
for ( int i = 0; i < 25; i++ )
{

[Code].....

View 1 Replies View Related

Visual C++ :: Comparison Of 2 Double Values

Dec 27, 2013

I have std::vector of players and each player have its own value. The value is of the type double, but the original value are of the "XX.00".

Now what I'm trying to do is to insert a player into this vector. For that I need to find a place and to find a place I need to compare player value.

So lets say I have a player in the vector whose value is 24.00 and I am trying to insert a player whose value is 25.00. To my surprise MSVC 2010 tells me that:

24.0000000000000 == 25.00000000000

I do aware that comparing double type is not going to work like this, but I would expect not to have such big of a difference. ;-)

Why this comparison is true and how to eliminate such thing?

View 3 Replies View Related

C/C++ :: Do-while Loops Not Working Correctly

Oct 28, 2014

I have an assignment for class .. It works, the do-while loop isn't working correctly. Once I am doing inputting information for any employee It should ask to continue. It doesn't, It skips that loop and prompts to enter the type of employee again.

#include <iostream>
#include <iomanip>
using namespace std;
int main( ) {
char empInput;
char continueResponse;

[code].....

View 3 Replies View Related

C++ :: While Loop Condition Not Working Correctly

Dec 8, 2014

The while loop part of my program isn't working right. stringOriginal is an array. If a large amount of characters passed the max 80 im processing is typed, it goes into a infinite loop. Also the condition doesn't work i type in quit and the loop continues.

while(stringOriginal != "quit"){
std::cout << "Enter a string of characters you would like to reverse
";
cin.getline(stringOriginal,79,'
');
}

View 1 Replies View Related

C :: While Loop Not Working - Not Sure Whether Fflush Or Scanf Is Used Correctly

Jan 17, 2015

I just started learning programming. I tried using this but the loop is not working ....

Code:
#include<stdio.h>
#include<ctype.h>
#define PI 3.1416
void main(void) {
char choice;
float r, area;

[Code] ....

However when i removed this portion below, and replacing it with choice='Y';, the loop works just fine although my intention is to continue the loop only when 'y' is entered.

Code:
printf("Continue? Y/N:");
fflush(stdin);
scanf_s("%c", &choice);
choice = toupper(choice);

I think that I'm using the code wrongly but i'm not sure where the mistake lies in

View 8 Replies View Related

C++ :: Passing Lambda As Template Parameter Not Working Correctly

May 30, 2013

I've was trying out a function template to automatically get the type of a lambda, but it seems that it won't compile

I've tried two different ways:

1.
template<class HASHER>
auto make_unordered_map(size_t bucketCount, HASHER const && hf)
-> unordered_map<string const, HASHER>&& {
return unordered_map<string const, int, HASHER>(bucketCount, hf);
} auto x = make_unordered_map(1, [](string const& key)->size_t { return key[0]; });

2.
template<class HASHER>
auto make_unordered_map(size_t bucketCount, HASHER const && hf2)
-> unordered_map<string const, int, decltype(hf2)> {
return unordered_map<string const, int, decltype(hf2)>(bucketCount, hf2);
} auto x = make_unordered_map(1, [](string const& key)->size_t { return key[0]; });

The test code are located here:

1. [URL] ....
2. [URL] ....

They are both based on the code that is stated to work in those examples. I.e.:

auto hf = [](string const& key)->size_t { return key[0]; };
unordered_map<string const, int, decltype(hf)> m (1, hf);

View 13 Replies View Related

C/C++ :: Get Double Random Numbers In Range From 0 To 1?

Apr 19, 2012

How to get double random numbers in the range from 0 to 1?

View 2 Replies View Related

C++ :: Random Number Generator Isn't Working Correctly - Ignoring Conditions

Apr 12, 2014

My random number generator isn't working properly. It seems to be ignoring my conditions when it compiles and I enter the input. The program is supposed to accomplish the following.

1. Let the user input how many digits he or she would like to have in the random numbers.

2. Let the user type in how many of the numbers that he or she wants.

3. It will then generate the numbers.

4. It will display the minimum or maximum number it can be with the number of digits the user entered. And display the number of numbers that the user wanted. It also is supposed to check and output only up to the max of that digit range.

so if someone entered they wanted a digit of 1(1-9) but said they wanted 300 numbers it would only output 9

Example:
the user says that she would like 3 digits to be in the numbers generated. So it will output numbers between 100 and 999. then the user says that they would like only 3 random numbers. So it will output three random numbers in between 100 and 999. Also all of the numbers need to be unique so they can't output more then one of the same number.

I am not sure why but it ignores my conditions. Ill type that I want 7 but it just outputs a bunch of random numbers. it dosen't stay in the ranges.

Class file.

Code:
#include "stdafx.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <conio.h>
#include "TargetGen.h"
using namespace std;

[Code] .....

View 6 Replies View Related

C++ :: Save Top 10 Scores In Simon Game - Filling Array Not Working Correctly

May 17, 2013

I wrote a Simon game, and wanted to save the top 10 scores. I was working right, until I decided I wanted to still be able to read the file if someone enters a name containing spaces. Now, the results aren't right.

void FillScoreList(string Simon_Names[], int Simon_Scores[]) {
ifstream Simon_HiScores("Simon_Data.txt");

if (Simon_HiScores.is_open()) {
for( int x=0;x<10;x++){

[Code] ....

Even without trying to read names with spaces, I'm getting

dad 1
0
340176
0
... either a long number or a zero. No names

View 4 Replies View Related

C/C++ :: Built In Formula For Calculating The Range Of Float And Double?

Oct 26, 2013

I want to get the direct formula for calculating the range of double and float datatypes in c,if any.

View 1 Replies View Related

C++ :: Printing Double Triangle - Working Fine Up To Input 7

Feb 22, 2013

I wrote code for printing a double triangle. Its woking fine upto input is 7. but when the input is 8 the output is disturbed. the code is this.

#include<iostream>
using namespace std;
int main() {
int a;
cout<<"Enter a number"<<endl;

[Code] ....

View 3 Replies View Related

C++ :: Simulation Of Motion Of Magnets On A Rod - Long Double Not Working

Apr 25, 2013

I'm currently working on a simulation of the motion of magnets on a rod. As part of it, there are arrays of the properties of the magnets:

long double *accelerations; // These will later be dynamically allocated depending on the number
long double *velocities; // of magnets
long double *positions;

However, when I go to compile this, the compiler gives me these error for the pointers:

error: two or more data types in declaration of 'accelerations'
error: two or more data types in declaration of 'velocities'
error: two or more data types in declaration of 'positions'

Apparently, the compiler isn't recognising long double* as a type and is instead reading is as the two types long and double*.

My compiler is MinGW 4.4.3

View 4 Replies View Related

C++ :: Comparison Of Two XML Files?

Feb 2, 2015

I need a program that can compare two xml files for equivalency using any XML Parser.

View 2 Replies View Related

C/C++ :: Comparison Of Excel And CSV

Mar 6, 2012

I have to compare data in excel and csv based on the file name and update the status of file from CSV into excel file by creating a new column status on weekly basis.

View 1 Replies View Related

C++ :: Overloading Comparison Operators For Using In A Set

Oct 30, 2014

I have a small piece of code that used the set::insert function on a set of myClass. For that, I need to overload the < and > operators, which I have, but I still get a huge error saying it can't compare.

set<MyClass> mySet;
MyClass myClass

All the class information gets filled in. Then, I try to insert...
mySet.insert(myClass);

bool operator<(MyClass &lhs, MyClass &rhs) {
return lhs.name < rhs.name; //name is a string
}

The error says
...stl_function.h:230:22: error: no match for 'operator<' in '__x < __y'
MyFile.h:80:6: note: candidate is bool operator<(MyClass&, MyClass&)

View 5 Replies View Related

C++ :: Comparison With Uppercase / Lowercase?

Jul 28, 2014

I have this statement - while(strcmp(x[i],"Stop")!=0); - is there anyway to compare x[i] to all the possible forms of "stop" (Stop, STOP, sTop, etc)?

View 4 Replies View Related

C/C++ :: Overloading Comparison Operators

Aug 28, 2014

I made a program that allows the user to enter information of credit cards on an array of size 5, however I need to allow the user to compare the five credit cards with each other and I am having problems with this particular part. I made my bool operator functions for the operator< and the operator> but how to make the user be able to select which cards he wants to compare and how to compare them. My code is the following:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int SIZE = 5;
enum OPCIONES {CARGAR=1, ABONAR, NADA};

[Code] ......

View 2 Replies View Related

C/C++ :: ISO Forbids Comparison Between Ptr And Int For Types

Jul 14, 2014

This is some code that simulates files and directories the same way an operating system does so. I commented out every std::string occurrence because I got the :

terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create

View 3 Replies View Related

C++ :: One Letter Of A String Comparison

Jan 21, 2013

I just would like to turn this into cpp:

string eg("azertyFTW");
if(eg[one of the letters contained in this string] == 'c') {
cout << "eg has the letter c in it";
} else {
cout << "not this time :(";
}

View 4 Replies View Related

C++ :: Sorting A Map By Custom Comparison?

Jan 8, 2012

so i have a std::map like this:

map<DWORD,DWORD> mymap;

now i want to sort it by the second DWORD value, and thus i have to implement my own comparison function and use sort() from <alogirthm>:

Code:
bool y(const map<DWORD,DWORD>::iterator& a, const map<DWORD,DWORD>::iterator& b)
{
if(a->second < b->second)
return 1;
else
return 0;
}
...
sort(mymap.begin(), mymap.end(),y);
...

but this throws thousands of errors at me, including C2784.

View 14 Replies View Related

C++ :: Comparison Of Two Strings (case Insensitive)

Apr 27, 2013

bool simpleQuestion::checkAnswer(string guess) const {
for (int i=0;i<qAnswer.length();i++)
if (qAnswer==guess)
return true;
else
return false;
}

This is my code and what im trying to accomplish here is making the comparison of the two strings (qAnswer & guess) case insensitive. I know i need to loop through each character of each string and for each character i can use toupper and every char in each string will become uppercase and therefore case insensitive. However, im not sure how to go about this; if any technique used to loop through each character of the string and how to use to upper.

View 2 Replies View Related

C++ :: ISO Forbids Comparison Between Pointer And Integer

Apr 21, 2013

I am trying to make a program that prints out an 8 by 8 board with 1 queen on each row.

For example, if the user enters: 0,3,4,0,0,7,6,7

the program should create following board and print:

Q.......
...Q....
....Q...
Q.......
Q.......
.......Q
......Q.
.......Q

I think I am doing it right but im getting a couple of errors that i cant fix ...

#include <iostream>
using namespace std;
int main(){
int x [8] [8], r, c;
for(c = 0; c <= 8; c++){
cout << " Enter a number from 1 to 7 " << endl;

[Code] .....

these are the errors :

assignment15_meem.cpp: In function âint main()â:
assignment15_meem.cpp:7: error: no match for âoperator>>â in âstd::cin >> x[c]â
assignment15_meem.cpp:10: error: ISO C++ forbids comparison between pointer and integer

View 7 Replies View Related

C/C++ :: Sorting Algorithms And Comparison Count

Jan 30, 2014

I am trying to count the number of comparisons for each of the following sorting algorithms, Selection, Insertion, Bubble, Merge, and Quick sort. This is using an array of numbers that are sorted, reversed, and randomly arranged.

I currently increment the "number of comparison" variable before every conditional statement that compares two numbers and am getting the following results:

Number of Items in Array = 100
Selection: random = 5049; reverse = 5049; sorted = 5049;
Insertion: random = 2640; reverse = 5049; sorted = 99;
Bubble: random = 9207; reverse = 9900; sorted = 99;
Merge: random = 1221; revere = 988; sorted = 1028;
Quick: random = 690; revere = 587; sorted = 636;

View 1 Replies View Related

C/C++ :: ISO Forbids Comparison Between Pointer And Integer

May 4, 2014

template <class ST>
bool OrderedSet<ST>::IsIn (const ST & value) const {
for (LNode * np = first; np != NULL; np = np -> next)
if (np -> next == value)
return true;
return false;
}

View 1 Replies View Related

C++ :: Map Comparison Fails In Function Const

Jan 27, 2012

Why I'm getting an error here. I've dumbed down my code for simplicity and removed irrelevant code.

PHP Code:
class Foo {
    public:
        bool IsNull() const;
    private:
        std::map<int, int*> test;

[Code] ....

I'm getting a "passing...discards qualifiers" error on my if statement and not sure why because I'm not changing anything. I know removing const or making test mutable fixes the issue. I've been taught to always make a function const if it doesn't change anything, in which case, have I finally come across an acceptable time to use mutable?

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved