473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Read in enum types

Hi,

I want to read in enum types from standard input. Unfortunately it does not
work. The code is given as follows:

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

enum TYP { eins, zwei, drei};

int main()
{
Â*Â*Â*Â*stringÂ*input("datIn");
Â*Â*Â*Â*ifstreamÂ*IN(input.c_str());
Â*Â*Â*Â*TYPÂ*t;

Â*Â*Â*Â*cinÂ*>>Â*t;Â*Â*//Â*Fehlermeldung

Â*Â*Â*Â*returnÂ*0;
}

Do I have to define an input operator ?

Thank you for your help.

Jan 16 '07 #1
4 3722

Alex wrote:
Hi,

I want to read in enum types from standard input. Unfortunately it does not
work. The code is given as follows:

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

enum TYP { eins, zwei, drei};

int main()
{
string input("datIn");
ifstream IN(input.c_str());
TYP t;

cin >t; // Fehlermeldung

return 0;
}

Do I have to define an input operator ?
You could read an int and convert it to TYP

Jan 16 '07 #2
Alex wrote:
Hi,

I want to read in enum types from standard input. Unfortunately it does not
work. The code is given as follows:

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

enum TYP { eins, zwei, drei};

int main()
{
string input("datIn");
ifstream IN(input.c_str());
As a side note, you can directly do this, instead of above 2 lines
ifstream IN("datIn);
TYP t;

cin >t; // Fehlermeldung

return 0;
}

Do I have to define an input operator ?

Thank you for your help.
Jan 16 '07 #3
Alex napsal:
Hi,

I want to read in enum types from standard input. Unfortunately it does not
work. The code is given as follows:

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

enum TYP { eins, zwei, drei};

int main()
{
string input("datIn");
ifstream IN(input.c_str());
TYP t;

cin >t; // Fehlermeldung

return 0;
}

Do I have to define an input operator ?

Thank you for your help.
It depends on format in which is your enum written in the stream.
Anyway you should overload input operator>>:

template<typename T, typename TRAITS>
std::basic_istream<T, TRAITS>& operator>>(std::basic_istream<T,
TRAITS>& is, TYP& data)
{
// Here read your type from is any way you want.
// One possible (of many) example follows:

std::basic_string<Ts;
is >s;

// Here would be better some searching mechanism or table for
bigger enums
if (s == "eins") data = eins;
else if (s == "zwei") data = zwei;
else if (s == "drei") data = drei;
else
{
// Wrong input - do what ever you want, but you should switch
is into bad state
}

return is;
}

It is written from scratch, so there may be some typos.

Jan 16 '07 #4
Thank you for your suggestions. I'll try them out.

Jan 18 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: slot | last post by:
Is it possible to compare "enum" types to basic types like "unsigned short" type at all? Thanks!
4
by: Popoxinhxan | last post by:
Hi guy, i just wondering that is it posible to convert the array of string to enum types dynamically, for example i get the list of countries name from database and would like to convert all of...
1
by: arzewski | last post by:
I don't know what was the reason from a language design point-of-view to allow this, but VB.NET compiler does not flag you (when C# compiler does) when passing an integer value as a parameter to a...
5
by: Alan Cobb | last post by:
Hi, In the managed C++ class below I get compile warning C4677 from VS2003. "signature of non-private function contains assembly private type", even though the managed enum is public. I have...
2
by: Jeremy | last post by:
I would like to limit a field in my datagrid to only accept values in an enum that I have (gDataTypes). To add my column I use: columns.Add("Type", System.Type.GetType("System.String")) ...
2
by: Jeremy | last post by:
I would like to limit a field in my datagrid to only accept values in an enum that I have (gDataTypes). To add my column I use: columns.Add("Type", System.Type.GetType("System.String")) ...
4
by: Pohihihi | last post by:
I read on MSDN that enum is a ref type http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemenumclasstopic.asp but in one of the MS publications enum is said...
0
by: IR | last post by:
Hi, I'm trying to store values of an enum type in a std::bitset container, in a contiguous manner. eg. typedef enum { meValue1 = 0xbaad, meValue2 = 0xbeef,
10
by: Charlie | last post by:
I tried to post this before and I apologize if I am repeating myself, but I do not see the post anywhere. But anyway, I have a file, data.c, where I define all of my global variables. I then...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.