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 have a project assignment for school to write a program that does number conversions using bitwise operators. The premise is that the user enters a number with one of three letter prefixes -- Q1232, O6322, H762FA, etc. -- and the program will take that number and convert it to the other two number bases. Q is for quarternary, O is for octal, and H is for hexadecimal. The transformations should be done using bitwise operators and bit shifting. I am guessing I need to scan the number, convert it to binary, then convert it to the other two bases.
However, I am completely new to bitwise operators and bit shifting, so how to convert numbers of different bases to binary and then binary to other bases using these bit and bitwise functions. I don't have much code done yet, since I am still unsure of how to approach it, but I'll post what little I have.
Here it is:
#include <stdio.h> #include <string.h> int main() { char numType; printf(" The user will enter a number up to 32 digits in quarternary "); printf("(base 4), octal (base 8), or hexadecimal (base 16). If in ");
[Code] ....
I figure in each case I can write a function that converts the entered number to binary, then maybe two more functions that convert said binary number to the other bases. For default in the switch I will tell the user they entered an invalid number. I don't have the program looping until the user types 'EXIT' yet, but I will once I figure out anything about these bitwise operators.
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).
2. Data represents some data object the user can pass to Processor's public methods.
3. Internally, Processor needs to use InternalData type which is based on the content of Data (I can use Data's public interface to get the required information from it, or construct a Data object using its public constructor when needed, and that's how I have done it so far).
4. To avoid repeating code and localize changes required when Data's interface would change someday, I made conversion functions from Data to InternalData and back inside Processor, as private methods.
Now here comes the kicker:
5. But I'd like these conversions to be implicit inside Processor's methods instead of explicit. And only there.
6. These conversion functions are only for Processor implementation's use. They shouldn't be visible nor accessible from the outside world.
Where the problem lays:
7. InternalData is a library type. I don't have control over it and I cannot modify its interface.
That is, I cannot just add converting constructors or conversion operator member functions to them.You can consider it to be built-in type if you wish.
8. I don't want to put those converters inside Data class either, since it's not its business and it shouldn't know that Processor converts it to something else internally.
Long story short, I'd like to teach the Processor's implementation how to make type conversions between Data and InternalData implicitly, but no one else except Processor should be affected by it. Outside world shouldn't be able to do these conversions or even know about them being done inside Processor's implementation.Is there any way to do it in C++?
The core of the problem seems to be the fact that in C++ defining implicit conversions is possible only from/to a user-defined type when defining this type. I don't know of any way to define such conversions for some other type's internal use only. (Especially when I don't have control about one of these converted types.)
Write a program that creates and displays a table of temperature conversions. Get the starting temperature from the keyboard in degrees Celsius (do not allow input of a value below absolute zero). Also get an integer value to represent the number of degrees to increment for each of a 20 row table (do not allow the increment value to be less than one. The first column will be a row number starting with one, follow by the Celsius value and then the conversions into Fahrenheit, Kelvin, and Rankine. Be sure that all columns are neatly right aligned for a variety of inputs.
Thats what i wrote so far:
#include <iostream> #include <iomanip> using namespace std; int main() { double C,F,K,R,n,a; cout <<"Enter starting temperature in Celsius: "; cin >> C; [Code] ....
Thats what the instructor looking for:
Enter starting temperature in Celsius: -500 ERROR: Temp must be >= -273.15: -273.15 Enter increments in degrees Celsius: 100
run-time check failure #0 - the value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention
when i try to run my code. It has compiled fine on another computer, but it simply will not work on this one. This is the part of code where it is receiving the error. it has to do with the stoi
Code: #include <string> // for use of string #include <fstream> //for file handling #include <iostream> // for file handling #include <cstdlib> #include <iomanip> //for the setprecision used further below using namespace std; struct MasterData //struct created named 'MasterData' to hold one line from master file
/*assume array is already initialized and declared and is of array type string.*/
int i = 2; int j = 1; string newvalue; cout<<"Current value at array[i][j] is "<<array[i][j]<<endl; cout<<"Enter new value "<<endl; cin>>newvalue; array[i][j]= newvalue; //PROBLEM IS IN THIS LINE. cout<<endl; cout<<array[i][j]<<endl;
I'm having lots of trouble with storing a cin string text into a string array. It just seem that after I cin newvalue, the program crashes. Is this way of storing it considered illegal? I'm just a beginner with 5 months of coding experience in C++.
I have a problem i have a textbox filled with info when the button is click but what i need to do now is when new info is place in the textbox i need the results textbox's to clear and the new info put in the textbox.
I have a pointer to a C string that contains multiple lines of text. I need to change a few characters in the string (possibly adding 1-2 extra). At the moment my code looks like this (char **objectData is the pointer):
Code: char temp[350]; char* end = strstr (*objectData, "word_before_data_changes"); strncpy (temp, *objectData, 39); // Copying the first 39 symbols strncat (temp, "6-11", 4); // inputting some data strncat (temp, end, 100); // copying the last symbols.
I don't know how many there would be. Tried sizeof(end), but this cropped off last two lines for some reason. So using 100 at the moment. This sort of works, but I think it also adds some new lines or null-terminating characters in the end or something like that, because data parser cannot parse the modified data after that.
I would like to make a sort of text parser, in which one enters a string, and it is broken by the whitespaces into chunks, and those chunks compared to different "dictionaries" where the words are assigned a value. For example if the operator enters "take lamp" it would separate "take" and "lamp" and then produce preassigned values for each of these words.
I'm trying to make a program that will read in names and grades from a text file and print them in the console. However whenever I try to use the OpenFile.get function I get an error saying that there is "no instance of overloaded function"
getting this error resolved before I can.
my code so far (I know it's missing a lot, but that's not what I'm worried about right now.)
I need to read from a text file and search for a string that appears in the file and then count how many times it appears. My count just prints zero though. Do you see what I did wrong in my code for this to happen?
I'm working on a program that can load all words from a dictionary "English2.txt" (it's attached to the post), then put every word into a 2-dimensional matrix (every line is reserved for one word) and display them. After loading every word into a matrix, when I try to display first 8 words with printf, I can't do it without '' at the end of a line. Otherwise only the 8th word is displayed... What's more, when I try to read the length of the first word with strlen, it says, that it has 4 characters (in fact there are only 3 and it's the word "AAA"). What could be the reason for that?
I'm making a program in which it will read an input from a text file and then count the numbers of spaces , characters , words . Here is what i think it would work : First i will transfer the contents from the input.txt into a string , after that i will create 3 strings which contain each of these : spaces , characters , words . Then comparing each of the contents of the intput.txt_string to the other 3 strings .
#include<iostream> #include<fstream> #include<string.h> using namespace std;
I'm trying to read all content from a text file into a string. This is the code I'm using for it (I know there are other, maybe better ways to do it but I'd like to stick to this for now):
char* buffer; std::string data; FILE* fp = fopen("textfile.txt", "rb"); if (!fp) { printf("Can't open file");
[Code] ....
So my problem is that I get an exception when I try to free the memory -> 0xC0000005: Access violation reading location 0x51366199
I even get the exception when I try to free it immediately after calloc() but why this is.
And if I use buffer = (char*)malloc(lSize); I don't get any exceptions.
I need to make program which converts text (letters and digits only) into 7-digit ascii code. The start and end code is "1100011", so it must not appear inside the output code, thus a zero should be added in it (11000011).
Not that it matters much what my task is - I have a problem. Here's the code:
Code: #include <iostream> #include <stdlib.h> using namespace std; int main() { string in,out="",pk="1100011"; getline(cin,in);
[Code] ....
The 'pk' string loses its value here when I enter a character in the input and I cant figure out why... I got that the problem is somewhere in the first if loop, since the code prints pk nicely when cout is before the loop (comment 1)..however, after the loop (comment 2) it prints nothing..... When compiled, it works nice when I input numbers, but 1 character - and 'pk' disappears...
My question is simply how do I center the lines on top of one another using setw (without the use of an additional header file or an additional function)?
I've attempted Code: setfill(' ') to no avail as well.
I can manually set the numbers of setw of course, but I was hoping for alternative smarter fix (without having to pain staking manually set & test each setw).
Note for reason I cannot use an additional header file or function: I've always wanted to know if this could even be done without the use of the <strings> header file (and without the use of an accompanying function). That, and I wanted to reduce the amount of lines of code if possible. If there's not an easier way that's fine (just wanted to know if setw or setfill could get the centering job done alone).
I didn't think it could be done (I believe with the header files I have and place & with no additional functional support one would have to simply manually set each setw).
Code: #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { char Enter;
Issue 1: I am using a stringstream object in a block of my program that needs to be visited repeatedly depending on a user's selection from a menu. I want the contents of this stringstream object to be cleared any time control gets to this part of the program. I have tried the clear and flush functions to no avail.
Issue 2: I am reading data from a source text file that would be regularly changed during the course of program run. After the program run is over, I am supposed to save the results(which is basically the source text file AND all updates) in a destination file. This destination file would then serve as the source file when next the program is run. In other words, I want a scenario where my results overwrite the original contents of the source file; implying that my source and destination files are now one, pretty much. How can I do this?
I am trying to print a specific line from a textfile
e.g I have a text file called info.txt and inside it contains the id,item name, price, quantity
Code: 1,shirt,100,10 2,pants,50,9 3,pen,20,8
I know how to find a specific word at the line in the file but how do I find and print out the specific line e.g(print out just the entire contents of line 2)?
Code: string temDescription; ofstream fout; int curLine = 0;