C/C++ :: Convert Enum To Text String
Jan 25, 2015I want to convert an enum to a text string.
enum BREED {YORKIE,CAIRN,DANDIE,SHETLAND,DOBERMAN,LAB};
I want to convert an enum to a text string.
enum BREED {YORKIE,CAIRN,DANDIE,SHETLAND,DOBERMAN,LAB};
I defined
enum boundaryType_t {inside, inlet, outlet, wall, periodic};
now I have file written like this:
inside outlet
I want my program read the file and when encounter any enum type, instead of treating it as a string, I want the program store it in memory as a enum value.
How can I do that?
string s;
ifs>>s;
s // how to convert it to enum???
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]?
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>>.
I need to do this simple Battleship game project for my C++ class. Here is my current code so far:
#include <iostream>
#include <array>
#include <string>
using namespace std;
enum BShip{ EMPTY, BTLSHIP, HIT, MISS };
const int BSIZE = 6;
const int BTLSHIP_SIZE = 4;
class Battleship {
private:
array<array<Bship, BSIZE>, BSIZE> sea, hits;
[code]....
For some reason, I'm getting errors on the string and enum declarations.
I am writing a program where I need to read a byte of char data and convert it into a text string of binary data that represents the hex value...
i.e. The char byte is 0x42 so I need a string that has 01000010 in it. I've written the following subroutine....
------------- My Subroutine ----------------------------------------------------------------------
void charbytetostring(char input, char *output){
int i, remainder;
char BASE=0x2;
int DIGITS=8;
char digitsArray[3] = "01";
[Code] ....
When I submitted the byte 0x42 to the subroutine, the subroutine returned to the output variable 01000010... Life is good.
The next byte that came in was 0x91. When I submit this to the subroutine I get garbage out.
I am using a debugger and stepped through the subroutine a line at a time. When I feed it 0x42 I get what I expect for all variables at all points in the execution.
When I submit 0x91 When the line remainder = input % BASE; gets executed the remainder variable gets set to 0xFFFF (I expected 1). Also, when the next line gets executed..
input = input / BASE; I get C9 where I expected to get 48.
My question is, are there data limits on what can be used with the mod (%) operator? Or am I doing something more fundamentally incorrect?
Program that reads in a normal text file and converts it into mobile phone text. that is if the word is 3 characters or less then ther is no changes to the word and if the word is four or more letters then remove all the vowels from the word except for vowels that are capitals.
View 5 Replies View RelatedOk, so I'm writing this code and when I build it keeps saying cannot implicitely convert type int to string even though I declared my variables as string. Why is it giving me this error?
private static string Repair()
{
string result="";
string beep;
string spin;
Console.WriteLine("Does your computer beep on startup?:(y,n)");
[Code]...
heres the function:
string ToString ( const char * format, ... )
{
char buffer[256];
[Code]....
these function do the same of the sprintf() function. but instead we use a variable for add the result, i want to return the result. when i use it:
string f;
f=ToString("hello world");
gives me several errors:
"error: crosses initialization of 'std::string f'"
"error: jump to case label [-fpermissive]"
I am using Visual Studio 2008. I just wonder if there are any library function in Windows SDK or MFC, or from third-parties, that can convert a UTF-8 string into Windows Unicode string(used in CString object).
Can MultiByteToWideChar or ATL String conversion macro like A2W to the conversion?
How to change an enum type variable to a string type variable?
View 2 Replies View Relatedconvert this file to an excel or ms access file?
View 10 Replies View Relatedprotected void btnUpload_Click(object sender, EventArgs e){
string pic = ASCIIEncoding.ASCII.GetString(fileUpload1.FileBytes);
TextBox1.Text = value.ToString();
//Here i get pic[40385]
[Code] ....
On two different event i get two different value which cause file not open due to file corrupted
I am trying to read an array values from Tmin.txt file. I take array size from user viz. number of raw & column and then, read the following data from Tmin.txt file which look like this:
20 40 20 25 30
20 30 40 20 25
20 40 20 25 30
20 30 40 20 25
30 40 40 30 40
I am using getline, and then converting it to int by using atoi. But my code is not working.
Code :
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
[Code] ....
The problem is that I want to write a C++ program that converts an ordinary text file into binary and then reads that binary file and converts it to text file so that this text file equals to first text file. I have wrote this code for it.
int main() {
string name1 = "first", name2 = "sec", name3 = "third";
int j = 0, k = 0;
ifstream ifs(name1.c_str()); // Here I want to read from the ordinary text file (name1).
[Code] .....
Now what the ofs.write(as_bytes(j), sizeof(int)); or ifs.read(as_bytes(k), sizeof(int)); exactly mean?
In practice, the file name1 contains digit 5 and its size is 1 byte. The name2 contains some character/sign like [] and its size is 4 bytes and name3 contains digit 0 and its size is 1 byte, why? I before have created the name1 file in ordinary text file mode.
My machine is Windows 7 32-bit. My compiler is MVS 2012.
How to convert an ordinary text file into binary and how to convert that binary file back to a text file so that the first text file equals with the last text file?
View 8 Replies View RelatedUsing C . I have been tasked (for a University assignment) to create a program that will enable a user to upload an image and convert that image into ASCII text using SDL on a Linux system. I've managed to open a window, display an image (non-selected) and convert it into grayscale using
case SDLK_g:
for (int y = 0; y < image->h; y++) {
for (int x = 0; x < image->w; x++) {
Uint32 pixel = pixels[y * image->w + x];
Uint8 r = pixel >> 16 & 0xFF;
Uint8 g = pixel >> 8 & 0xFF;
Uint8 b = pixel & 0xFF;
Uint8 v = (0.212671*r)+(0.715160*g)+(0.072169*B)/>;
pixel = (0xFF << 24) | (v << 16) | (v << 8) | v;
pixels[y * image->w + x] = pixel;
I am absolutely clueless as to how I can get the user to upload an image to the program and then begin the image -> ASCII conversion. I've found seem to be written in C++ or C# to make the conversion I should be using the GetPixelColour command?
I would like to convert a value into a string, and extract a value from a string. And then call these functions in the main.
template<class T>
string toString (T value) // convert a value into a string {
stringstream ss;
[Code[ .....
Are above functions correct? And how should I call in main?
int main() {
const int SIZE=10;
toString<int> intValue(SIZE); //seems not right
toString<double> doubleValue(SIZE); // seems not right
}
I've got this string: Code: char * string = "2+2"; I want to get an integer value = 4 from this. How would I go about doing this.
View 1 Replies View RelatedI want to convert string to double. I refered this strtod() - C Library Function Example
Code:
#include <stdio.h>
#include <string.h>
int main() {
const char string[] = "$$GPRMC,013732.000,A,3150.7238,N,11711.7278,E,0.00,0.00,220413,,,A*68";
char term;
const char delims[] = ",";
}
[code]....
Is there anyway to convert std::string to char*?
Like:
std::string x="hello world";
char* y=x;
I need to convert a string IP to an unsigned int (uint32), but however solutions I've found elsewhere have not worked (such as `atoi`).
When using `(uint32)"54.171.82.217 ";` : [URL] ....
When using `atoi("54.171.82.217");`: [URL] .....
How can I correctly convert the string version of the IP to the uint32 type?
I'm trying to get two numbers longer than long long int and add them together and multiply. I'm not sure how to convert the numbers to a string.
#ifndef BIG_INTEGER_H
#define BIG_INTEGER_H
#include "BigIntegerException.h"
class BigInteger {
private:
int biginteger[500];
int bigintegertwo[500];
[Code] .....
I have this code working:
char tmp_wku[3];
tmp_wku[0]=0x01;
tmp_wku[1]=0x9D;
tmp_wku[2]=0x62;
char tmp_com[11];
[Code] ....
This sends the buffer to a LIN modem. My question is: can this be done better. If I have a astring of hex numbers like "09 98 88 55 42 FF 00 00 FF BD 89". How could I send this without manually makng a char with hex numbers?
I am C++ newbie.
I want to ask how can i convert string to int and split it?
Like given string input is 00;
I want it to become 2 int which is 0 and 0...
My teacher has done a very poor job of teaching us anything this year. When he taught us for loops, he wrote one on the board, didn't explain any of it, then said now that you know for loops we can implement them in a code. but anyway, we need to write a code for converting a string to an int and all the examples i find on the internet use pointers but we aren't allowed to use those yet.
View 2 Replies View Related