C++ :: Map Storing Multiple Data Types
Jul 11, 2014
I am trying to create a generic map container which shell contain data of different datatypes. I already found a solution in this forum here:
[URL]...
Introducing a container with a Base Class as content type and inserting objectes of Derived Class types from that Base Class suites my implementation very well. But it is not really working. I implemented it this way:
class MyType {
public:
MyType() {}
virtual ~MyType() {}
}; template <class PT> class ParseType : public MyType
[Code]...
Then I insert one element.
// index being an object of type Parser<string>
ParseType<string>* test = new ParseType<string>( index );
// and index.val(0) = "-n"
iMap.insert( pair< string, MyType* >( index.id(0), test ) );
Now I think I should be able to call
const string key("-n");
iMap.at(key)->content->val(n);
Or
iMap.at(key)->get_val(n);
But neither one compiles with the error that "class MyType" (as the map container is pointing to a MyClass object) has no member/member function "content"/"get_val".
I also tried introducing member and member function "content" and "get_val" in "class MyType", which I thought should be hidden while constructing ParseType<string>. This would compile but it does not call the member "content or member function "get_val" from class ParseType.
A second try was to remove templatization of "class ParseType" and introduce a specific, let's say, "class ParseString" for objects of type Parser<string>. But the problems remain the same either the compiler complains due to missing member/member function or retreiving the map content will not call the derived class member/member function.
View 4 Replies
ADVERTISEMENT
Jun 17, 2014
Basically I do not want to use a menu, instead just accept either an float or a single character. Then send the data to the appropriate spot based on the user input. I have been unable to convert the char to a float, and even if I did the char would probably only accept the first digit, say user enters '15' it would only read the '1'. I've tried strings instead of char but then unable to use the isalpha function. Do I need a char[] and then iterate through to get the numeric data? Then how do i make '1' and '5' become 15. There is probably a solution. I've also tried to use a loop waiting for the correct data while(!(cin >> letter)) which works but how do I get out if the user enters number.
#include <iostream>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;
[code].....
View 3 Replies
View Related
Aug 10, 2013
I need a translate (in both directions) all primitive types, into char[] (will be stored in string)
I understand how to manipulate integral types with bits and I cant just cut them down and shift them, but float and double don't work with this manipulation. So, how I can create a perfect bit copy of float and double?
int i = 0xFCED03A4; //Random number
char c[4];
c[0] = ((i >> 3) & 0xFF);
c[1] = ((i >> 2) & 0xFF);
c[2] = ((i >> 1) & 0xFF);
c[1] = (i & 0xFF);
[Code]...
This is basic stuff but I need an equivalent for float and double types, and it needs to be a perfect BIT copy, not value copy.
View 7 Replies
View Related
Dec 10, 2014
I have a function, int teleport_to_game(int,float,float); in my class. My question is should I change the int to define a function to a different type?
View 2 Replies
View Related
Oct 3, 2013
So I'm supposed to write a code that asks a user for a string and then displays the hex, decimal, and binary code for each individual letter and then tells the user how many bits in binary were 1. For example: Enter a line of text: Hello
The ASCII code for 'H' is 0x48 in hex, 72 in decimal, or 01001000 in binary, 2 bits were set.
The ASCII code for 'e' is 0x65 in hex, 101 in decimal, or 01100101 in binary, 4 bits were set.
The ASCII code for 'l' is 0x6c in hex, 108 in decimal, or 01101100 in binary, 4 bits were set.
The ASCII code for 'l' is 0x6c in hex, 108 in decimal, or 01101100 in binary, 4 bits were set.
The ASCII code for 'o' is 0x6f in hex, 111 in decimal, or 01101111 in binary, 6 bits were set.
So far I've got a code that will display the binary bit pattern by shifting a mask and testing for a 1 or 0. The problem is I can't figure out how to make it so the 1's and 0's get put into a single integer rather than just printing out. I hope that makes sense. Here's my whole code.
Code:
#include<stdio.h>
main ()
{
int i;
char input;
printf ("Enter ........: ");
scanf ("%c", &input);
for (i = 1; i <= 8; i++)
[Code]...
View 9 Replies
View Related
May 17, 2014
#include <iostream>
#include<fstream>
int decryption(int);
int multiply(int,int[][2]);
using namespace std;
main(){
int n;
ifstream inFile;
inFile.open ("out.txt");
[Code] .....
I was trying to store numbers read from a text file into 2D array but I am getting the error above.here is where the error occurs:
line 33: cout<<m[i][j]<<" ";
View 4 Replies
View Related
Feb 7, 2014
I'm trying to create a function where it allows the user to type in multiple amounts of integers, so if the user wanted to have 3 different storages that hold different integers, the input would look something like this:
5
97 12 31 2 1 //let's say this is held in variable "a"
1 3 284 3 8 // "b"
2 3 482 3 4 // "c"
2 3 4 2 3 // "d"
99 0 2 3 42 // "e"
Since we don't know what number the user will input every time, I'm not sure how to create a dynamically allocated array that will create an x amount of arrays every time.. I want to be able to access each index of a, b, c, d, e or however many arrays there are.
So far, this is what I have, but I'm having trouble creating the arrays since it's unpredictable. I'm purposely not using vectors because I don't really get how pointers work so I'm trying to play around with it.
int* x;
int length, numbers;
cin >> length;
x = new int[length]
for (int i=0;i<length;i++)
{
cin >> numbers; //this doesn't work because it only grabs the first line for some reason
x[i] = numbers
}
View 3 Replies
View Related
Dec 12, 2014
int* count;
count = new int(1); // what???
Is this on the heap?? do i have to delete it now?
So is 'new' on a primitive data type just a way for me to allocate primitive data types (int, char, etc.) on the heap instead of the stack?
And, out of curiosity, can you do that in Java?
View 4 Replies
View Related
Jan 9, 2015
How to convert these data types? i have an array of bytes in unsigned char[] array, and need to convert to const void* pointer.
View 3 Replies
View Related
May 24, 2014
How do you create an object (like in the title) something more simple than a struct? I wanna know that cuz I'm writing a function that could return a boolean and an integer at the same time.
View 2 Replies
View Related
Mar 26, 2013
What are the possible problems if I declare a bunch of data types and never use them? Do they take up a lot of memory? Will they slow run time? If it is an array do I have to delete it at the end of the program? What if the array is defined inside a class and never used? Do I still have to delete it?
i.e.
Code: class declarearrays{
public:
double **darray;
double **darray2;
void function1();//function that initializes darray
void function2();//function that initializes darray2 with different parameters, may not be used.
};
View 1 Replies
View Related
Apr 10, 2013
I'm fairly new to C++ programming. I wanted to accept a datatype as an input. I've seen something similar while working with the <queue> library.
When defining a new object of queue
For example:
queue<int> Queue;
How is <int> handled ??
View 1 Replies
View Related
Feb 18, 2014
I am doing a programming assignment. This program asks you to collect statistics on precipitation and temperatures from the four quarters of a year and print the calculated results. It is an exercise in using enumerated types and arrays. The measurements are entered at the end of every quarter.
Major variables (there are other variables) in the program:
Variable called: month of type Summary_Month (the enumerated type)
Arrays of integers called: low_temp, high_temp, precip
Array of doubles called: avg_temp
You will ask the user to enter the precipitation, low temperature and high temperature for each quarter. As you gather this data, you will calculate the average temperature (using avg_temp) for each quarter by averaging the low and high temperature for that quarter.
After you gather the information you will calculate and output : Total Precipitation for Year, Average Quarterly Precipitation, Average Quarterly Temperature, Highest Temperature for any quarter, Lowest Temperature for any quarter.
I am not getting the right output for average precipitation and temperature and I am not sure how to determine the highest and lowest temperature.
# include <iostream>
# include <iomanip>
using namespace std;
enum Quarters { March, June, September, December};
int main() {
const int NUM_QUARTERS = 4;
[Code] ....
View 9 Replies
View Related
Mar 7, 2014
how to correctly use pointers within relation to function parameters and main source file.
I noticed that char types, for example char myVariable[50]; which is an array, does not seem to require a pointer as if it already has one built in? as opposed to char *myVariable; which seems to need one - i assume this is because char has different ways to store memory in relation to pointers, because of there being multiple ways to store a string, and memory allocation as a part of that. - i stared C a few weeks ago and feel that it is difficult to progress without nailing down pointers. Also address operators provide confusion for me and written tutorials are not so clear because there are different ways to use these operators.
View 4 Replies
View Related
Jan 22, 2014
What is the maximum and minimum of int, long int, long long int, double, float, and anything bigger than that? And also how to calculate this?
View 4 Replies
View Related
Jul 11, 2013
Is there any way to make a switch in C. That I can choose between different data types? For example, at the moment I need to know how to read one parameter. It must recognize if it is a boolean or uint32.
View 4 Replies
View Related
May 8, 2014
In my platform (Windows 7 Ultimate 64 bits with Service Pack 1 over a compatible PC with a AMD x86 microprocessor), the next sample C++ code,
#include <iostream>
#include <limits>
using std::cout;
using std::endl;
using std::hex;
using std::showbase;
using std::numeric_limits;
[Code] ....
Compiled with Microsoft Visual C++ 2010 Express, prints this output:
ui = 0xffffffff
ull = 0xffffffff
sll = 0xffffffff
si = 0xffffffff
ull = 0xffffffffffffffff
sll = 0xffffffffffffffff
So, in my platform, conversion from an unsigend integer primitive data type to any bigger integer primitive data type never extends the most significant bit of the former integer and conversion from an signed integer primitive data type to any bigger integer primitive data type always extends the most significant bit of the former integer. This is convenient to mantain the same value when converting between integer primitive data types of the same signedness (i.e, signed integers or unsigned integers).
View 4 Replies
View Related
Mar 28, 2015
How to properly read data from a .txt file.
If I have data stored in a .txt file, which is formatted/stored like this:
Code:
Apples and Strawberrys
10
Cherrys
12
Pears
16
Grapes, Melons, and Peaches
20
I know that if I read/extract, and print the data like this;
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream dataFile("test.txt");
string textData;
[Code] ....
Each line of data is stored in the string "textData" and printed to the screen, exactly as it was stored in the .txt file. So, all is clear to me up to that point.
But, what if I wanted to store each line of text in the string "textData", and store the numbers/integers into a separate variable called "numberData"? How would I retrieve and store the numbers (in the above example .txt file, every 2nd line) separately from the text?
For now, to keep things simple, let's assume that the data in the .txt file is stored/formatted as in my example (1 line of text, 1 line containing a number/integer, ...repeat) so, there is no need to test if the retrieved data is actually text or an integer, before it is stored in the appropriate variable type.
View 14 Replies
View Related
Nov 24, 2014
i have the following error defines.h:14:23: error: two or more data types in declaration specifiers, the begining define.h source code is (the line 14 is in red):
Code:
/* $Id: defines.h 3492 2011-09-18 20:44:09Z nekral-guest $ */
/* some useful defines */
#ifndef _DEFINES_H_
#define _DEFINES_H_
[Code]....
View 8 Replies
View Related
Feb 18, 2014
I am doing a programming assignment. This program asks you to collect statistics on precipitation and temperatures from the four quarters of a year and print the calculated results. It is an exercise in using enumerated types and arrays. The measurements are entered at the end of every quarter.
Major variables (there are other variables) in the program:
Variable called: month of type Summary_Month (the enumerated type)
Arrays of integers called: low_temp, high_temp, precip
Array of doubles called: avg_temp
You will ask the user to enter the precipitation, low temperature and high temperature for each quarter. As you gather this data, you will calculate the average temperature (using avg_temp) for each quarter by averaging the low and high temperature for that quarter.
After you gather the information you will calculate and output the following:
Total Precipitation for Year, Average Quarterly Precipitation, Average Quarterly Temperature, Highest Temperature for any quarter, Lowest Temperature for any quarter.
I am not getting the right output for average precipitation and temperature and I am not sure how to determine the highest and lowest temperature.
Code:
# include <iostream>
# include <iomanip>
using namespace std;
enum Quarters { March, June, September, December};
int main() {
const int NUM_QUARTERS = 4;
[code]....
View 3 Replies
View Related
Aug 14, 2014
How can I read this file in to my struct?
12345Joe Lim KH879.00
12233Kay Suet Yee35.98
23781Leong Jing Yang 10.00
67543Woon Tian Yi500.50
struct master {
unsigned short int IDnum;
[code]....
not working
View 16 Replies
View Related
Mar 29, 2014
I am doing this program where I analyze colors in a ppm file. Now I am having a hard time storing the data into arrays in this order...
P3
600 339
255
44 5 8
44 5 8
43 4 7
42 3 6
42 3 4
44 5 6
...
So far i tried using fscanf()
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define DEBUG true
#define CHECK true
[Code] ....
I then realized that the order that the second method gave me will make it very hard for me to calculate the RGBs. Because they will be calculated like wise..
P3
600 339
255
44 5 8 = sum
44 5 8 = sum
43 4 7 = sum
42 3 6 = sum
42 3 4 = sum
44 5 6 = sum
...
but that isn't the issue right now..
View 10 Replies
View Related
Oct 17, 2013
I have a file that can range from 100 rows to 10000+ that is comma delimited with 8 columns. The first 32 rows (also comma delimited) will always be ignored (geographical header information). I will be wanting the data from column2 and column3.
For this I believe I would need (2) For Loops as such:
for(i=0;i<2;++i) {
getline("do something here");
}
and
for (i=0;i<3;++i) {
getline("do something here")
}
Also would using a vector or array with dynamic storage be the better way to tackle this problem? Where to start from after accessing the file.
View 19 Replies
View Related
Mar 27, 2013
Is it possible to store data to CPU memory, if so how much memory can I store and how? Say I wanted to store an array, about 6MB in size?
View 1 Replies
View Related
Apr 12, 2013
I am trying to store the Title, Artist, and date published of a list of CD's. I can't seem to be able to get it to print the list or not sure if it is actually storing it. This is what i have so far.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
char names[5][100];
[code]....
View 2 Replies
View Related
Sep 6, 2013
I was working on float and double data types and to see the results i wrote this program:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
ofstream outputF("output.txt");
double a = 1;
double outcome;
[Code] ....
Well I understand the part it cannot store infinite numbers. but if you take a look at the output for example (since it is too long i just added some of the outputs)
//---------------------
for the value of : 001
1
//---------------------
for the value of : 002
0.5
//---------------------
for the value of : 003
0.3333333333333333148
[Code] ....
if you look carefully at the value "5" and "10" results. it is awkwardly abnormal. which is something i couldnt understand. also it is the same with value "20", "25", "40", "50" and so on.
View 5 Replies
View Related