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).
I got the following to compile and execute, but I don't understand how this works. Here is the code verbatim:
Code: // Basic conversions in C #include <stdio.h> int main () { float f1 = 123.125, f2; int i1, i2 = -150; char c = 'a';
[Code] .....
Okay, so first we have some variables declared as integers, floats, (and that char what it's doing). On the same lines we have values assigned to some of those variables. At first the "f2" and the "i1" confused me, but I think they're just variables whose type has been declared but have not received a value. So far I think I'm good. Then we get into the routines
I think I understand the first one. i1 didn't originally receive a value assigned to it, so when it says f1 = i1, then f1 (123.125) just becomes an integer, simple enough.
Then we get to the next routine and I'm like what dafuq??? So first we have f1 (123.125) being set to i2 (-150). wtf? So does that mean f1 is now going to have the value of -150? Vice versa? How are they becoming equal? Or does it have nothing to do with the values of the variables at all and just the type??? I'm just totally lost.
The output of that line is "-150 assigned to a float produces -150.000000." Which makes perfect sense to me written in plain English, but I don't understand how the C code works ....
I'm reading lines from a text file in C++ which contains integer + string + float number(like 3,67 with comma) + string in this order. I need the float number to sort the lines but I couldn't manage to separate the data into the types I can use so far. I tried different kind of functions and the best I could do was such a code;
void main (){ ifstream records; records.open("records.txt"); int id; string line; char name[100]; float gpa;
[Code] ....
This fails at reading the floating number which has comma in it and then last string is read as string starting with the comma and rest of the number. An output example is:
698 John 3 ,67
It doesn't read last string on the line as well. I understand that part but simply I need another read but what I want exactly is to separate one line using "tab" as a seperator into proper data types and then using the numbers as integers, and the grades as floating numbers. How Can I do this?
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.
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. };
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.
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.
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;
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.
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.
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.
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):
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;
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.
I have been thinking about this all day and I am yet to come up with a good solution. So, I need to design an image class which should work with various data types (int, float, double etc.) and can also be multidimensional (2, 3, 4, 5). So, what I did was generate a template image class as follows:
Code: template<typename T, int dimensions=3> class Image { private: T * data; };
Anyway, now I have various image formats that I want to support, so the easy thing to do is create a Factory sort of object which will call eventually generate the correct image.
So I want to create various image classes called ImageType1, ImageType2 etc. which will take the input image and generate the correct Image object. However, I do not want these objects to be templated because they need to be passed from functions and be used in a generic way.
So, at run time I will need to be able to do this…
Code: class ImageType { public: ImageType() { PolymorphicImage * image = new Image<float, 3>(); } private: PolymorphicImage * image; };
So, I want my ImageType classes to contain the Image object and be able to generate it with the right template arguments at run time. Is there any way to do this without having multiple specialised definitions for ImageType?
I'm trying to develop a deeper knowledge of how loops work (and what better way todo that than a dynamic password guesser). My main problem lies with the conflict between data types, as I try to point to a char at a specific index position of the password guess.
See in my code (at line 57):
Code: #include <iostream> #include <iterator> #include <algorithm> #include <string> #include <cstdlib> #include <iomanip> using namespace std; string AlphaNum("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");//62 possible characters int size = AlphaNum.length();//should be 62
[Code] ....
This is annoying, because strings are arrays of characters themselves.
I think I may have found a new way of checking for 3d polygon collisions, but I'm not sure. The method involves...
1. finding the planes that the primitives lie on 2. finding the line where the planes intersect 3. if both polys have points on both sides of the line AND have points that overlap on the 1d space of the line, then they intersect.
I have some half done code testing this, and so far it seems to be sound and fairly fast. These are some average time-tests done on my machine for each part:
In class we were asked to create a C++ BTree program that would allow a user to input the following data types and then store said data in a .txt file:
0. ID 8 bytes
1. First name 30 char
2. Last Name 30 char
3. Street Address one 30 char
4. Street Adress two 30 char
5. City 30 char
6. State 20 char
7. Zip 10 char
8. Country 30 char
(I'm not particularly asking for full code, pseudo code would also be great). I had a great deal of my work done, unfortunately, the computer I was working on crashed, corrupting my files.
I have defined my own class, Queue, which inherits from my own class, LinkedList. I have been using templates to allow Queues to be of int, string, etc types.
But now I want to be able to store objects in my Queue type. And so the problem I have is that in my LinkedList class, I have two instances where I initialize an instance of my generic type T to 0.
For instance, the removeFirst() method starts like this:
template <typename T> T LinkedList<T>::removeFirst() { T a = 0;
And so the compiler complains that it can't convert from int to [in this case] Command&.
int check_up(char string[]); int check_low(char string[]); void to_up(char string[]); void to_low(char string[]);
[Code] .....
When I compile this I have the following problems: warning: data definition has no type or storage class [enabled by default] in 'to_up(word)'conflicting types in 'to_up' function and to_low function warning: data definition has no type or storage class [enabled by default] into_up function error: unknown type name "word" in line 'printf("All uppercase %s. ", word):;'warning: parameter names (without types) in function declaration [enabled by default] in 'to_up(word)'and 'to_low(word)' 'note: previous declaration of "to_up" was here in function declaration of to_up function