C Sharp :: Test Enum Without Converting To Array First?
May 7, 2013
Is there a quick way to determine if a value is in an enum? Consider for example,
public enum GenderEnum
{
Unknown = 0,
[Code].....
I don't want to convert to an array if possible, just check the enum and get a yes/no answer.
View 1 Replies
ADVERTISEMENT
Feb 25, 2014
It is working:
#include <iostream>
#include <string>
#include <vector>
#include <map>
const int ENUM_NOT_FOUND = -1; const std::string NEW = " ";
enum Day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
[code]....
Ouput with GCC 4.8.1:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Name a day: Friday
day = Friday
But the problem is that whenever I define a new enum, I have to define the << and >> overloads for the new enum again. Isn't there a way to template that as well, so that the << and >> overload needs to be defined just once? My atttempt:
template<typename Enum>
std::ostream& operator << (std::ostream& os, Enum en) {
return os << EnumConversions<Enum>::toString (en);
[Code] .....
fails to compile. I guess the problem is Enum is not known at compile time, even though it should be deducible during run time? Error mentions ambiguous overload for operator>>.
View 6 Replies
View Related
Aug 22, 2012
I'm having some trouble with a variable from a database. That variable in that database is of type double.
To read from the database, I'm using a variable dr of type OleDbDataReader.
So far, I've been using dr["variablename"].ToString() to work with the variables from this database - without the ToString part it wasn't working.
However, with this specific variable (I'll call it "var"), I have to do numerical comparisons of >= and <=, so obviously, just using dr["var"].ToString() doesn't work.
Therefore, I tried creating a variable of type double called varConv and setting it as
varConv = Convert.ToDouble(drPen["var"].ToString()).
That doesn't work, gives me an error message saying "Input string was not in a correct format". If I remove the ToString part, it also doesn't work, with a different error message saying "Object cannot be cast from DBNull to other types".
Trying to do a direct comparison, as in dr["var"] >= x also doesn't work, says "Operator '>=' cannot be applied to operands of type 'object' and 'int' ".
View 3 Replies
View Related
Nov 30, 2014
I was doing a side programming challenge in my workbook that asked me to dynamically allocate and array for test scores. So far I have an array that will accept an integer to allocate the amount of test scores and then will accept integers to populate the array, however when I try to cout the contents of the array the numbers are absurdly different than what was put in. Here's my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
void main() {
cout << "How many test scores?" << endl;
int scores(0);
[Code] ....
And this is the output screen:
How many test scores?
4
Enter test score 1:
22
Enter test score 2:
33
Enter test score 3:
44
Enter test score 4:
55
-33686019
18472
55656634
201345063
Press any key to continue . . .
Why am I getting these crazy numbers? I've looked back and forth from examples in my book and it doesn't look like I'm doing anything wrong.
View 5 Replies
View Related
Apr 8, 2013
I am trying to write a loop that will iterate through a char array in C and pull the IP addresses and test them to see if they respond. My ultimate goal is to have the first one that responds returned in the function, but the loop is not running correctly. It will run through right the first time, but then will start over again.
Code:
const char * getServer(char *svrList) {
char *srList;
srList = strtok(svrList, " ," );
printf("Contents of srList b4 loop: %s
", srList);
printf("Server List: %s
", svrList);
[Code] ....
Code output:
Contents of srList b4 loop: 1.1.1.1
Server List: 1.1.1.1
result is "1.1.1.1"
Hitting else loop
Contents of srList in else: 2.2.2.2
result is "2.2.2.2"
result is "2.2.2.2"
Contents of srList b4 loop: 1.1.1.1
Server List: 1.1.1.1
result is "1.1.1.1"
Hitting else loop
Contents of srList in else: (null)
View 14 Replies
View Related
Jan 28, 2014
Using the array to accept 10 testscore. Calculate and print the highest, lowest, fifth test score entered and all the test score entered in reverse order.
How i would get it to print the highest,and lowest and in reverse order. I'm confused as to what to do...
View 4 Replies
View Related
Mar 24, 2014
Version 1 (works fine)
Creating array in main and then calling a sorting function from sorting class.
In main:
const size_t SIZE = 100;
int *array = new int [SIZE];
//fill array with ints, not shown here
quickSort(array, SIZE); //calling the sorting function in the sorting class
In the sorting class, quickSort is declared as such:
void quickSort(int arr[], int num);
Everything works great.
Version 2 (issues)
Instead of creating the array in main, I have set up a class for the array, MyArrayClass, where I create an object containing the array of ints. So far so good. The issues come when I write a member function to call quickSort. Even though my MyArrayClass object contains an array of ints, the code calling for quickSort() won't compile as the data type isn't ints but MyArrayClass (which in turn holds ints though). The compiler (using VS 2013 btw) complains that quickSort can’t convert the first argument from 'const MyArrayClass' to 'int[]'.
How do I cast my class object array of ints as an int[] in order to be able to call the quickSort function? Or should I solve this issue in some other way? I tried altering the sorting function to accept the object as it is, but that only created an avalanche of new errors, so thinking that converting/casting the object array --> int[] might be easier...
View 6 Replies
View Related
Jan 12, 2015
Q.Write a program using user-defined function which accepts an integer array and its size as arguments and assign the elements into a two dimensional array of integers in the following format: If the array is 1,2,3,4,5,6, the resultant 2D array is
1 2 3 4 5 6
1 2 3 4 5 0
1 2 3 4 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0
Ans.
#include<iostream>
using namespace std;
int two_print(int x, int one[999]) {
int two[999][999], q=x, z=x;
for(int k=0; k<x; k++) {
q--;
[code].....
View 2 Replies
View Related
Mar 27, 2015
I am trying to convert a 1d string array to a 2d but i couldnt do it. Heres my code so far,
for(i=0;str2d[i][j]='';i++)
{
for(j=0;str2d[i][j]=' ';j++)
{
str2d[i][j] = str1d[k];
k++;
}
}
str1d is a paragraph btw.I tried using NULL instead of '' but didnt work either.
View 5 Replies
View Related
Mar 30, 2014
In this program, I have to ask the user for an employee, then the program will check to see if the file for that employee exist, if it doesnt then it will automatically create the file.
ReadNew function reads the file....check to see if it exist
CreateNew function creates a new file.
In my code I have no problem with the first part of reading file.. and my createnew function works in other programs where I am asking for input of file name to create the file name. However in this code I cannot figure how to automatically pass the input filename from the ReadNew function to the CreateNew function. I can't ask the user to enter the name a second time, so I have to pass the input filename into both functions. Here is my code.
Code:
//Create a file, append to it, and read it.
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
string filelist;
void CreateNew(ofstream & FileNew);
[Code]...
View 1 Replies
View Related
Apr 4, 2013
How can I change this function so it stores each of the even integers into an array of integers?
Code:
void sumIntegers ()
{
int num = 0;
int i = 0;
[Code]....
View 1 Replies
View Related
Apr 17, 2014
For Example, it the entered string is: 0324152397 I want it to get stored in an array like-[0] [3] ...[7]. Secondly the string entered may be of any length that is defined only at run time. So, I also need to calculate string length. How could I do that.
View 2 Replies
View Related
May 18, 2014
I am having a lot of trouble extracting values from a string into a double array.
I have a string that looks something like this: "asdf 1.320 1 234.43 5.00000 3"
All I want to do is extract the doubles and store them in an array of doubles.
View 8 Replies
View Related
Jun 1, 2014
I am looking for the correct syntax to typecast a character array as an int. So I would be able to do something like this:
char ch[4];
//...Read characters from file into ch or something, etc...
int i = ( int ) &ch;
or
char ch[4];
//...Read characters from file into ch or something, etc...
int i;
strncpy ( ( char* ) &i, ch, 4 )
I want all the 4 characters ( or bytes ) of ch to be classed as an int. So we are taking the 4 bytes of the character array and placing them in 4 bytes of the integer? How would i go about this?
View 2 Replies
View Related
Mar 2, 2015
I made a program that adds two matrices and displays their sum with a max dimension of 100.
/* This program asks the user for 2 matrices called A and B, as integers,
and displays their sum, C. The max dimension of each matrix is 100. */
#include <stdio.h>
// Construct function
void construct() {
int m, n, i, j; // Variables
int first[100][100], second[100][100], sum[100][100]; // Matrices variables
[Code] ....
Now I need to change it so there is no max size for each matrix. The arrays will be larger than 100x100 so I need to use malloc to create my arrays. So I cant use int A[rows][cols]. This is what I did to covert arrays to malloc. It compiles but it crashes after I entered all the integers.
/* This program asks the user for 2 matrices called A and B, as integers,
and displays their sum, C. The max dimension of each matrix is 100. */
#include <stdio.h>
#include <stdlib.h>
// Construct function
void construct() {
int m, n, i, j; // Variables
int *first = NULL;
[Code] .....
View 8 Replies
View Related
May 29, 2013
I really confused with constructor (default constructor and constructor with parameters)
I coded this problem
and I worked almost, but I stock in constructor
Code:
class Tier {
public:
enum TIER_MAKE
[Code] ....
This is tier class and I have to finish constructor in class car (for simple, I skip detail code) -red things are the parts from class Tier
Code: Car()
: make(NULL), passengers(0), fuelcap(0.0), efficiency(0.0), tier(Tier::nexen)
{ }
[Code] ....
And someone said default constructor part has to be this
Code:
car( Tier::TIER_MAKE p_tiermaker = Tier::nexen )
//after i skip
but default constructor should be no parameter...? isn't it?
View 1 Replies
View Related
Jan 19, 2014
I'm trying to make a simple C++ program in which the user must try to guess a number, if they guess too high it says "too high" and if they guess too low it says "too low".
I also decided to add a feature which allows them to select how many tries they would like to guess the number. I tried to make "tries" type an enum so if the user could not pick an invalid number but for some reason i cannot use it in an if statement.
here is the code and i am getting the first error on line 27:
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int guess;
[Code]....
View 7 Replies
View Related
Apr 1, 2013
I am trying to write a game in C++ with SDL, and I have a class that allows me to handle events. The class is actually really simple: It takes the SDL_Event, then 2 variables from 2 different enum to determine for which Event and which Key should be checked, and then a variable that will be modified if the event happens. Here is the class
EventParser.h
#include "SDL.h"
#include "SDL_opengl.h"
template<class T>
[Code]...
As of yet the variable only changes if the Left key has been released, it will be extended if the error has been solved.
Then, in my main.cpp file I define the Event and the EventParser as
SDL_Event event;
EventParser<float> ep;
And in a loop, the parseEvent function is called like this:
ep.parseEvent(event, ep.KeyUp, ep.LEFT, &xVariable);
However I get a linker error (not the first one I got when programming this game)
error LNK2019: unresolved external symbol "public: void __thiscall EventParser<float>::parseEvent(union SDL_Event,enum EventParser<float>::EventType,enum EventParser<float>::KbdKey,float *)" (?parseEvent@?$EventParser@M@@QAEXTSDL_Event@@W4EventType@1@W4KbdKey@1@PAM@Z) referenced in function _SDL_mainC:UsersPrideRageDocumentsVisual Studio 2012ProjectsSDL_TestSDL_Testmain.objSDL_Template
View 6 Replies
View Related
Apr 18, 2013
I am trying to convert a char buffer[1024] with raw data to int 16 buffer2[1024] which is supposed to have hex data (0x01, 0x02 kind of format).
Code:
char temp_buffer[1024];
block_size = sizeof(temp_buffer);
num_blocks = sizeof(char);
Status = mount(0, &myfsObject);
if(Status != DAVEApp_SUCCESS)
[Code] .....
I am verifying the data of resultant buffer by creating a new file and writing data to it. The result which i got is:
Code: H uy H uy H uy H uy H uy H uy H uy H uy
View 2 Replies
View Related
Feb 14, 2013
Any function of simply converting character array to a short int?For example. char array[4]={'0','2','f','f'}; to short int 767?
View 3 Replies
View Related
Sep 30, 2014
I have an enum like:
Code:
typedef enum mac_type_e{
STATIC_MAC,
BLACKLIST_MAC
} mac_type_t; and I want to use this type in a structure that's declared like:
Code:
typedef struct lan_mac_s {
UINT16 lanmacid;
enum mac_type_t lan_mac_type_pp;// user mac type per port, 20 mac_type_t array, 0 = static, 1 = blacklisted now,
The compiler tells me:
Code:
incomplete type is not allowed
enum mac_type_t lan_mac_type_pp;// user mac type per port, 20 mac_type_t array, 0 = static, 1 = blacklisted
But if I remove the preceeding "enum" keyword, it compiles fine.
View 2 Replies
View Related
Feb 3, 2014
I defined
enum symbol {A, B, C};
in file A.h
and in file B.h, I #include "A.h" , but still get error of A , B and C unresolved when using them. WHy?
View 15 Replies
View Related
Jul 4, 2014
I'm trying to convert the enum type {PG, R, G, PG-13, etc.} to strings so i can use it in cout statement but no matter what i put inside switch(), the compiler keeps saying Error: expression must have integral or enum type. What am I doing wrong exactly?
Movie covert_rating(Movie r) {
switch (r) {
case PG:
return "PG";
break;
case R:
return "R";
[code]....
View 4 Replies
View Related
Jan 25, 2015
I want to convert an enum to a text string.
enum BREED {YORKIE,CAIRN,DANDIE,SHETLAND,DOBERMAN,LAB};
View 4 Replies
View Related
Jul 25, 2012
I am learning OOP Console Programming and i got some error in my code;
Here is my code
#include <iostream>
using namespace std;
enum ch {CD,DVD}
class disk {
private :
ch choice;
[Code] ....
I want to compare the enum variable to char c, and cout CD when user press 'c' and cout DVD when user press 'd'. I got error like this :
error : expected ')' before 'c' at first line after public
Note : I must use enum to store value.
View 7 Replies
View Related
Apr 2, 2012
Any way to use a string to access a specific item in a matrix of int[X].
I have a program which uses enums as iterators to reference a large amount of data. To select an item in the matrix, the user will enter a string, which is also an enum, which also must serve as an iterator for something in the matrix. Here is a toybox example:
#include <iostream>
#include <string>
using namespace std;
enum NyNumbers { First, Second, Third, Forth, LAST_VALUE };
int main(int argc, char* argv[]) {
int Matrix[LAST_VALUE] = { 1, 3, 7, 12 };
[Code] .....
The idea is the user executes the program by typing "./RUN First" to print out the first element in the MyNumbers array, "./RUN Second" to access the second, and so on. I can't use static numbers for the iterator. (i.e., "./RUN 1") I must reference by enum/string.
When run, the output of this problem is thus:
====================================================================
user@debian$ ./RUN Second
Matrix[ atoi(Second) ]: 1
user@debian$
====================================================================
BTW, if I change line 12 to this
====================================================================
cout<<Matrix[ argv[1] ]<<"
";
====================================================================
The error message is this
====================================================================
user@debian$ make
g++ -g -Wall -ansi -pg -D_DEBUG_ Main.cpp -o exe
Main.cpp: In function `int main(int, char**)':
Main.cpp:12: error: invalid types `int[4][char*]' for array subscript
make: *** [exe] Error 1
user@debian$
====================================================================
Which isn't unexpected. How to input the enum as argv[1]?
View 2 Replies
View Related