C/C++ :: Atoi Does Not Work / Converting Cstring Vector Into Ints
Feb 18, 2015
I have saved the contents of an int vector to a txt file and the numerical data was converted into a c-string. Nov I need to import and read the contents back into my program but I have not been able to convert c-string numerical data back into ints.
I am getting a string from the consle and that all works fine. I'm expecting a string of number, ie 123456.... I store that fine in my string object. I then need to take each individual number, ie 1, and put it in a vector then take the next number, ie 2, and put that in the next element in my vector.
Everything i've found and does eaither takes the entire string and stores it as an int, or takes the ascii representation of the number and stores that. How do I seperate each individual number and store them seperatly. below is a few different variations i've tried that doesn't produce my desired results.
cout<<"Please type the memory memory frames (numbers 0-9) to be used to simulate the input memory "<<endl; cout<<"Your input: "; string line=""; cin>>line; cout<<" Please wait while your input is processed and validated..."<<endl;
[code].....
the only thing i havn't tried yet is string streams and i don't necessarly like using them. Am i going to have to go that route? will that even work?
EDIT:I forgot to mention that this is being done on linux and then ported to unix.
I am having problems with converting a CString to an int and than doing checks on the int to see if it is in a particualr range.Below is what I am doing.
CString numstr = "28" int num = atoi(numstr); BOOL valid = TRUE; if(num < -32,768 { valid = FALSE; }
For some reason when running the above code the if statement is executed but it should not be becasue 28 is not less than -32,768. Why this is happening, I am not seeing the reason for this at all!! The num variable is being assigned the correct value.
The code is supposed to take either an int or a string (and their respective vectors) and insert a given int or string into the vector in ascending order. My code works properly for ints, but it's having a problem with strings.
The order I get with the strings given is
penguin banana great jungle
For some reason comparing penguin to banana/great doesn't give the expected result. The template attached only includes the function and the private vectors needed for the function.
template<class T> class orderedList { public: void insert(const T& item); private: vector<T> list; int total = 0;
I'm able to convert an integer to a vector<unsigned char> and back. However, when I try to use a nearly identical function designed for the long long data type, the last byte or two is broken.
Program code:
long long num = 9223372036854775551LL; cout << "Before: " << num << endl; vector<unsigned char> data = getBytes(num); num = getLongLong(data); cout << "After: " << num << endl;
Code for converting between vector<unsigned char> and long long:
Code: vector<unsigned char> getBytes(long long value) { int bytes = sizeof(value); vector<unsigned char> data(bytes); for (int i = 0; i < bytes; i++) data.at(i) = (unsigned char)( value >> ((bytes-i)*8) );
I am trying to convert a char to a CString, I have tried to use the CString.Format method but didn't work. I have a char(char holder[10]) and I want to see if holder is a certain string, say for instance SeaLevel. Below is the code that I also tried.
Let's say I have unsigned char test[10] = "HELLO!!!"; How would I go about circularly shifting this to the right? Inline assembly instructions would be ok too
I dont see any point of NULL in cstring. The code given below just outputs same as it would have done with NULL. My understanding is if size of char array is less than length of char array then null must be manually added?
#include <iostream> using namespace std; int main(){ char chr[0]; cin>>chr;//or if you use cin.getline; cout<<chr<<endl; return 0; }
Enter something: hellowwwww hellowwwww Segmentation fault (core dumped)
I have some paper work to do about a game I should be able to play with my class mates. We should be able to send and recive a struct concerning about the 'moves' of the pieces at the game. This struct is made of 4 ints, simple as that.
I had a previous paper work to do where I'd have to send ints and that was ok, but now that I have to send a struct I'm failing to do so. This is What I've tried:
//SERVER struct message{ int code; int piece; int x; int y; }send; bytes_sent=write(new_socket,&send,sizeof(message)); cout<<bytes_sent;
So far so good, I see 16 as bytes_sent's value.
On the other hand:
//CLIENT struct message{ int code; int piece; int x; int y; }recive; bytes_recvd=read(my_socket,&recive,sizeof(message)); cout<<bytes_recvd;
Why would the following line of code cause an exception and how can I fix it? This is with Visual Studio 2013 and it is set to use "Multibyte Character Set". This is a very old program that I was updating.
Code: thepath = dadir + "*.csv";
Both dadir and thepath are type CString.
Prior to this line dadir looks fine when I look at it in the debugger but when I reach this line of code I get
First-chance exception at 0x0FA08EE1 (mfc120d.dll) in GAQUtilities2014.exe: 0xC0000005: Access violation reading location 0xFEFEFFC6.
I have gotten it to record the date and I can printf it either on the same function, or in the main(). However, one of the requirements I must adhere to is to printf the statement in a brand new function, but when I do that, it just doesn't work. Heres what I mean:
Code:
#include <stdio.h> #define TICKER "LRCX" #define INVESTMENT_AMOUNT "10,000.00" //Prototypes int getdate(int* month1,int* day1,int* year1,int* month2,int* day2,int* year2); float getprice(float* BPrice, float* SPrice); void printdate(int month1, int day1, int year1); }
At first i had my int variables in global scope however i cant do the so im trying to pass my variables from my main to the void functions but cant.....
I am trying to generate some lists of permutations of ints but I can't make std::next_permutation work for me. The problem is I need to include permutations which don't use every number. For example take the array of numbers [1, 2]. I need an algo that will return:
I am trying to figure out the larger 2 out of 3 integers when i call them into a function from main program so far i have . How to write a simple function that will take 3 ints and find the sum of the higher 2?
Code:
int findsum(int a,int b,int c)// will find the highest int and return it to our main program { int max,max2;// this sets our local variable max // next we will find the larger of our first 2 variables if( a>=b)
[Code]....
How to get the second highest number and add it to max...
I have a non-MFC static library which I share between a number of different projects, some non-MFC and some MFC. Currently the static library uses a typedef of std::wstring and std::string for UNICODE and non-UNICODE builds.
After discovering it's possible to use CString in non-MFC applications, by including atlstr.h header, I decided I'd rather that than using stl strings and having to keep converting between the different types. However, I seem to be struggling with linker errors when linking the library with a MFC application.
Can I create a non-MFC static library using CString from atlstr.h and link it with a MFC application?