C++ :: Operations On Enumerations

Mar 12, 2014

I'm used to writing enums for enumerated types. However I have this:

enum Colour
{
BLUE = 0x01,
BLACK = 0x02,
WHITE = 0x04,
GREEN = 0x08,
RED = 0x10
};

Now each of the enumerated types are not exclusive, so I'd like to do this:

Colour c = BLUE | BLACK;

Of course that doesn't work. gcc gives "invalid conversion from 'int' to 'Colour' [-fpermissive]".

typedef int Colour;
static const Colour BLUE = 0x01;
static const Colour BLACK = 0x02;
static const Colour WHITE = 0x04;
static const Colour GREEN = 0x08;
static const Colour RED = 0x10;

View 2 Replies


ADVERTISEMENT

C++ :: Using Enumerations Types In Loops And Conditions - Ignored In Program

Jan 22, 2014

I am trying to use enumeration types in my conditions to make a simple program that calculates area of a square or circle depending on users choice then updates the total area and outputs the total area.

The program builds fine. But it ignores all of my loop conditions and i'm not sure why.

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

[Code] ....

View 4 Replies View Related

C++ :: Nested Conditional Operations

Feb 4, 2014

So I think I am having syntactical problem with my code.

Code:
int main() {
vector<int> ivec;
int score;
[Code] ....

I get an error from my compiler on the ?10th? line (Nested condition line) that says |19|error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'|

The purpose of the program is to take input and store it in a vector and then change the value to be between 1-6. I made this for the purpose of learning about nested conditional operations.

View 3 Replies View Related

C :: Floating Point Operations

Mar 16, 2014

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float i;
i=0.7;

[Code] ....

If i do run the above program in turbo C/C++ complier, it outputs "h". But,if i change the code as i=0.6 and if (i<0.6), it outputs "w". Even if i change it to i=0.8 and if(i<0.8), then also it outputs "w".

View 4 Replies View Related

C++ ::  bitwise Operations On Numbers

Dec 8, 2014

Trying to write 4 bytes ints in a binary file and extract them after... I'm using the exclusive or (^) to isolate single bytes to write to and extract from the file since the write() function accepts only chars, only the beginning and end results are not the same...

#include <iostream>
#include <ctime>
#include <fstream>
#include <cstdlib>
using namespace std;

[Code] .....

View 3 Replies View Related

C++ ::  how To Handle Async Operations

May 29, 2013

I've been thinking over this for long time... For example, when a button is clicked, print a string "clicked" after 5 seconds. (the button won't be blocked)

Thread should be the only way to do this:

btn.on_click = []() {
thread thrd([]() { sleep_5_seconds(); cout << "clicked" << endl; });
thrd.start();
};

the local variable thrd is destructed right after it starts, which may cause a crash, so use new operator:

btn.on_click = []() {
thread* thrd = new thread([]() { sleep_5_seconds << "clicked" << endl; });
thrd->start();
};

The thrd never get deleted

How would you solve problem like this?

View 2 Replies View Related

C++ :: Logical Operations Not Working?

Oct 7, 2014

int main()
{
char rORc, choice;
int sizerc;

[Code]....

when I input the character which is the underscore, and the row # it should display the table and sort that specific row. Why is the if statement skipped? This is not the complete program but has everything needed.

View 2 Replies View Related

C++ :: Validate Unsigned Operations?

Oct 12, 2012

I've sometimes encountered unexpected runtime issues caused by unsigned values being decremented below zero.

Example 1: "unsigned_value += negative_integer_value;"
Example 2: "for( size_t i = size - 1; i >= 0; --i )"

My compiler doesn't provide any compile-time or run-time warnings.

As far as I know, it's not possible to overload operators of primitive data types to check if the unsigned value is decremented below zero.

Any clever strategy to trace such cases at debug runtime, without having to add asserts all over the code? It's important that it does not affect performance in release mode.

View 6 Replies View Related

C++ :: Variable Length Integer Operations?

Aug 25, 2013

I intend to reference this thread from another thread so I do not clutter that thread with code

/* This code is relied on by some of the functions I will post */
typedef unsigned long long zxumax;
typedef signed long long zxsmax;

[Code]....

View 13 Replies View Related

C++ :: Mathematical Operations With Digits In Strings?

Feb 11, 2015

I'm working on a class project, and I'm having a difficulty. Suppose I have: string a = "21" and string b = "30"; normally, a+b=2130 (i.e concatenation of the characters in the string) but suppose I want a+b=51 (i.e. numerical addition) how do I go about this?

View 1 Replies View Related

C/C++ :: Console Input / Output Operations

Oct 27, 2012

How we can write coloured text to a text file in c? Can I use cprintf funtion with any kind editing

View 1 Replies View Related

C++ :: Incorrect Result In Bitwise Operations?

Oct 30, 2014

I'm doing a bitwise operations on 2 bytes in a buffer, then storing the result in a variable. However, I sometimes get a non-zero value for the variable even though I'm expecting a zero value.

The relevant portion of the code is as follows.

unsigned int result = 0;
long j = 0, length;
unsigned char *data;
data = (unsigned char *)malloc(sizeof(unsigned char)*800000);

[Code] ......

I'm expecting result to be zero when my data[j] and data[j+1] are 0xb6 and 0xab respectively, which is the case for most of the time. However, for certain values of j, my result is strangely not zero.

j = 62910, result = 64
j = 78670, result = 64
j = 100594, result = 64
j = 165658, result = 512
j = 247990, result = 128
j = 268330, result = 512
j = 326754, result = 1
j = 415874, result = 256
j = 456654, result = 1024
j = 477366, result = 512

It appears that these strange result values are all powers of 2, with a 1 bit appearing somewhere in the unsigned int.

I'm not changing the value of result anywhere else in the code, and when I print out (unsigned int)(((data[j]^0xb6)<<8)|(data[j+1]^0xab)), I get 0, but somehow when it gets stored in result, it's no longer zero.

View 3 Replies View Related

C :: Losing Contents Of A String Between File I/O Operations

Oct 24, 2013

Here's the code I'm writing:

Code:
#include <stdio.h>
#include <stdlib.h>
char* getTeamCode(char* team);

[Code]....

Why the placement of the code on line 21 above matters.

It grabs the correct string just fine. If I write a printf just below it, it prints the string it should correctly. However, if I do a printf of the string OSUteamCode below the fopen call on line 23, it prints blank.

So the first thing I did was move it below the fopen line. It worked, finding and outputting the first game in "game.csv" just fine, but not the other 11. Debugging with printf shows that the contents of OSUteamCode again disappear after the fopen call in the addLineCSV function.

I'm not understanding why that happens. The only thing I can figure is there's something going on with the file I/O commands that I just don't understand, but I can't find anything online that explains what that might be.

View 3 Replies View Related

C++ :: Limited Purpose Calculator Which Finds Value Of One Of Five Operations

Jan 9, 2015

I just started learning C++ a week ago and have been stuck on a project for the past 2 days now. I am building a limited purpose calculator which finds the value of one of five operations. Visual studio doesn't underline any errors in my program but every time I try to run it I get an error message. I believe it has something to do with the if/else but Im not sure.

#include <iostream>
using namespace std;
int main(){
int a;
int b;
int sum = a + b;

[Code] ....

View 5 Replies View Related

C++ :: Program To Complete Mathematical Operations Using Matrices

Jul 10, 2014

I've been assigned to build a program which completes mathematical operations using matrices. I have to use dynamically allocated 2d arrays, and can only use * to dereference and not []. I have this code so far, but the multiply_matrix function does not work with most values. I've looked at other similar posts, which have a similar algorithm to mine, but mine does not work for some reason.

/*
*(*matrix(matrix+i)+j)
*/

#include <iostream>
//for sleep() which allows user to see messages before screen is cleared
#include <unistd.h>
using namespace std;

[Code] .....

View 1 Replies View Related

C++ :: Perform Arithmetic Operations With Template Parameters?

Jul 13, 2013

how to use template parameters to perform arithmetic operations on objects.

I feel that it would best to demonstrate my issue rather than try and explain it.

Sample:

// Fundamental object structure
template<int T> struct myInt
{
myInt() { value = T; };

[Code]....

What I don't know is how to get a hold of the T variable to add them through the 'add' structure. Also, might any of this have to do with sequence wrappers?

seq_c<T,c1,c2,... cn> is essentially what I'm thinking of. Where T in this case is the type and c to the nth c are the values.

View 4 Replies View Related

C :: Point Operations On Elliptic Curve In A Prime Field

Apr 21, 2013

I am trying to write a program to perform point operations on a elliptic curve in a prime field I am using the standard formulaes for point additions and doubling in my code and these operations are performed by functions that are called but I am getting output for certain points but not all.

Code:

structure point_multiply(int x, int y, int k ) {
int xk;
int yk,m;
xk=x;
yk=y;
m=1;
int xL,yL,s,e;

[code].....

s1, s2, s3 etc are structures which hold a 2 integers which act as x and y co-ordinates.I am getting output by entering k=3,g=4, h=5 and many other cases mostly with small numbers but not for larger numbers.

View 3 Replies View Related

C :: Binary Game - Using Values With Some Basic Logical Operations

Apr 24, 2014

When things at work get overwhelming, it's not unusual for me to briefly "escape" by writing small programs simply for fun. A few days ago, I had an idea for a "binary game". I completed the first draft of it yesterday.

The idea is simple. The game uses 8-bit values. At the start of the game, a random "target" value is generated. The player is dealt a "hand" of seven values. The object is to use the values in your hand, along with some basic logical operations, to create the "target" value. While the idea is simple, the game itself can be quite difficult.

Here is an example of the output:

Code: --------------------
T: 1111 1100 (0xFC)
--------------------
0: 0000 0000 (0x00)
--------------------
1: 1100 1110 (0xCE)
2: 1010 0011 (0xA3)
3: 1100 0101 (0xC5)
4: 1011 1111 (0xBF)
5: 1010 1011 (0xAB)
6: 0001 1011 (0x1B)
7: 0001 1011 (0x1B)
--------------------

: Looking at the first column:
- 'T' is the "target" value
- '0' can be thought of as the "game board" - this is the value that needs to match the target value for a win
- '1' - '7' are the values in your "hand"

You can apply logic AND ('A'), OR ('O'), or XOR ('X') to a value in your hand with the value on the "game board". You are also allowed to apply logic to two values in your hand to create a new value for your hand. When a value from your hand is used, it is removed.

Some examples of the commands:

A30 --- apply logic AND to value #3 in your hand, and the "game board" value
O23 --- apply logic OR to value #2 in your hand and value #3 in your hand
X70 --- apply logic XOR to value #7 in your hand, and the "game board" value

You can also be dealt new values (as long as there's room in your hand) with the '+' command. 'H' or 'h' prints the help, and 'Q' or 'q' exits the game.

I haven't thoroughly tested it yet, since I just finished it a yesterday, but so far it looks good. The program itself uses only standard C.

I was dickering with the idea of supporting more logical operators (NOT, NAND, NOR, XNOR, shift), but I like the simplicity and resulting difficulty of the current implementation.

During initial testing, I realized it's possible to have doubles in your hand. Also, it's quite possible to be dealt the target value directly, which means that you could potentially win with one move, chance permitting. At first I thought about defending against these conditions, but came to the conclusion that it is fine as is - chances of an instant win are small, and if it does occur, would still be an enjoyable experience. Besides, if you're dealt the target value after the "game board" value has been modified.

Also, I did not allow a value of "zero" in the players hand. This was originally because I thought it would be of little use (though I've been reconsidering this). This also means that if two values in the hand are combined and result in zero, both values are removed and no new value is added. This was originally a bug, but I think I'll just reclassify it as a feature

So far, I found that the best strategy is to avoid modifying the "game board" value, and just play with the values in your hand. If you can get the target value in your hand, you just OR it with the "game board" and you're done.

View 2 Replies View Related

C++ :: Why Are Arithmetic Operations Like Division And Multiplication Invalid On Pointers

Feb 15, 2013

why are the arithmetic operations like division(p/q) and multiplication(p*q) invalid on pointers?.here p and q both are pointers .

View 6 Replies View Related

C++ :: Play Audio File As Background While Executing Other Operations

Jun 6, 2013

I need to make a program which can play a audio file as background while executing other operations .

View 4 Replies View Related

Visual C++ :: A Class For Operations With Large Integer Numbers?

Nov 27, 2012

Why this class doesn't work for Subtraction, Division and Square Root. When I try to get results for Subtraction, it output right answer, but with any trash. And When I try to get answer for Division and Square Root, it just brakes and don't output anything. For Addition and Multiplication class work correctly.

Its the link to this class [URL]

View 2 Replies View Related

C :: Linked List Or Hash Table For Matrix Operations And Representation

Jul 21, 2014

I have matrix in C with size m x n. Size n isn't known. I want to have operations on matrix such as : delete first element and find i-th element. (where size m woudn't be too big , from 10 to 50 columns).

What is more efficient to use, linked list or hash table? How can I map each column of matrix to different element of linked list or hash table; depends what I choose to use?

View 5 Replies View Related

C :: Bitwise Operations On 2 Bytes In A Buffer Then Storing Result In Variable

Oct 30, 2014

I'm doing a bitwise operations on 2 bytes in a buffer, then storing the result in a variable. However, I sometimes get a non-zero value for the variable even though I'm expecting a zero value. The relevant portion of the code is as follows.

Code:

unsigned int result = 0;
long j = 0, length;
unsigned char *data;
data = (unsigned char *)malloc(sizeof(unsigned char)*800000);

[Code] ....

I'm expecting result to be zero when my data[j] and data[j+1] are 0xb6 and 0xab respectively, which is the case for most of the time. However, for certain values of j, my result is strangely not zero.

Code:

j = 62910, result = 64
j = 78670, result = 64
j = 100594, result = 64
j = 165658, result = 512
j = 247990, result = 128
j = 268330, result = 512
j = 326754, result = 1
j = 415874, result = 256
j = 456654, result = 1024
j = 477366, result = 512

It appears that these strange result values are all powers of 2, with a 1 bit appearing somewhere in the unsigned int.

I'm not changing the value of result anywhere else in the code, and when I print out

Code: (unsigned int)(((data[j]^0xb6)<<8)|(data[j+1]^0xab))

I get 0, but somehow when it gets stored in result, it's no longer zero. I really don't understand what could be wrong.

View 1 Replies View Related

C++ :: Using OOP To Implement Matrix Class That Provide Basic Matrix Operations

Mar 27, 2013

i want to know how i can solve this question? do i need to create a class or write the program codes.

View 12 Replies View Related

C++ :: Create Class Vector As Template And Define Operations On Vector?

May 13, 2013

I need to create a class vector as a template and define operations on vectors.

And this is what I made.

#include<iostream>
using namespace std;
template<class T>

[Code].....

View 2 Replies View Related

C++ :: Changing Integer Into New Integer With Simple Mathematical Operations?

Jun 15, 2014

changing a 9 digit integer into a new 9 digit integer through simple mathematical operations. For example, I need to change 123456789 into the new digit 456123789. Sometimes I need to change a 9 digit integer into an 8 digit integer. An example is 789062456 into 62789456. I can ONLY use simple mathematical operations (addition, subtraction, multiplication, division and modulo).

View 4 Replies View Related







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