C/C++ :: Why Doesn't String In Vector Change Value
Apr 22, 2014
I am trying to change the value of of time in my vector of structs. The result should output 1430 as the value of shares[1].time though I get something different.
unsigned convTime = 1430;
std::stringstream out;
out << convTime;
shares[1].time = out.str();
std::cout << shares[1].time;
But my shares[1].time stays as its original value and not 1430.
View 1 Replies
ADVERTISEMENT
Feb 20, 2013
My coin/money change code works when there can be an exact change each time, i.e. when the 1 cent option is available. However, when the change options are only $10, $5, $1, 25 cents and 10 cents, it does not give me what I want for instance, I wanted to get change for $237.80, I was expecting to get:
23 10's, one 5, two 1's and 8 dimes. However, the code below is giving me 23 10's, one 5, two 1's and 3 quarters (there is no option left for the 5 remaining cents).how to fix it?
Code:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void change(double cents, int a[]);
int main() {
double Dollars;
double cents;
[code]...
View 14 Replies
View Related
Dec 24, 2014
when i pass a string pointer to a function such as string *str = new string(""); and pass that string to a handleElement() function e.g. handleElement(str), and i change the value in the function it simply doesn't change when the function exits as though it's passing it by value or something, even though it gives the pointer address.. I've now changed the code to use double pointers and pass the reference of the str pointer and it works but it seems ugly to use double pointers for this.
//handles when a new element is encountered when parsing and adds it to the parse tree
bool ParseBlock::handleElement(char cur, string *curString, int count, bool isOperator) {
countNode = new ParseNode(count);
//keep track of numbers and strings if they exist and insert them
if(!curString->empty()){
if(isNumber(*curString)
[code].....
View 2 Replies
View Related
Jun 25, 2013
#include <iostream>
#include <vector>
using namespace std;
int main() {
int * ptr;
vector<int> data;
data.resize( 1000 );
[Code] ....
So I need container that doesn't rellocate their address, It doesn't need to be like vector that everything is in a single pointer such as
int * a = new int[2000];
I have a pointer pointing to a member of a container and it needs to remain valid... doing
vector<int *> Array;
// allocating
for( int i = 0; i < 1000; ++ i ){
Code] ...
Waste of time, allocating them and deallocate them seem to take some time too
a List is also not efficient enough because I access them based on index
It is not a int it is pointing to but a texture, for the sake of simpler example I pick int
So is there a container that doesn't change member address and allocate when it needs to expand ?
View 3 Replies
View Related
Nov 1, 2012
i'm right now using C, IO is done via ncurses, but that won't affect the following problem, i think. The relevant code is:
#define SIDEBARWIDTH 27
//...
typedef struct {
[Code]...
surprisingly this works, now the new 3rd outputline is correct again. So it seems that the printcommand has some troubles with accessing the struct here. Not sure if that might be ncurses fault. Still feels odd.
View 2 Replies
View Related
May 3, 2014
What I am trying to do is read a sequence of words from cin and store the values on a vector. Then process the vector and change each word to uppercase.
I know line 21 is the problem. I think I don't have the right syntax. That syntax works on string. I'm trying to adapt and use it on vector but it doesn't work.
#include <iostream>
#include <string>
#include <vector>
int main()
[Code]....
View 7 Replies
View Related
Oct 29, 2014
// dynamic memory for 2D char array
char **board = (char**) calloc(column, sizeof(char*));
for(int i=0; i<column; i++)
board[i] = (char*) calloc(row, sizeof(char));
//for char 255 row and colum
char temp[255];
inputFile.getline(temp,255);
[Code]...
so i want to change into vector class like Vector<board>row;
View 8 Replies
View Related
Jun 7, 2012
I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,
N,N'-dimethylethylenediamine.mol
This is the function that opens the file :
Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );
[Code] ....
The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.
View 9 Replies
View Related
Apr 23, 2012
I want to change the string name to property name..
for example
public class Code {
public int RD{get;set;}
public Code() {
GetCode();
}
private void GetCode() {
string cd= "RD";
cd=1; // cd represent to property name "RD"
}
}
View 2 Replies
View Related
Dec 16, 2013
why does this give me an error when i try to change the elements of the char string array.
code:
int main(void)
{
char *name = "aaa";
// setup the name
name[0] = 'Z';
name[1] = 'e';
name[2] = 'd';
name[3] = '';
return 0;
}
[code]....
View 3 Replies
View Related
May 29, 2014
I wrote a program that reads a list from a file and stores it in a string type vector. Now, I want the user to input a word so that the program can search the vector to see if that word already exists. I have used every possible way of reading input from the console and storing it in order to compare with the vector but it never results in a match. When I print the input string and the vector string they are exactly the same thing (or at least print to the console as if they were). I've tried using getline; using cin direct to a string var; using cin to a char array and then casting to string using string str(arr); I even added a newline at the end just in case and STILL I cannot get a match.
vector <string> currentSet; //read a list in from a file and has 9 items in it
cin.ignore();
string line;
getline(cin, line);
if(line == vector[0]){//if printed to console line is HEAT and vector[0] is HEAT
cout<<"match"<<endl;
}
View 3 Replies
View Related
Dec 17, 2012
I want create menu button to change font of CString displyed in main window. We can see string "SAMPLE",and when i press menu button that i created i want to string "SAMPLE" to display with different font,i already created CFont object and added italic style with CreateFont;
This is my menu function:and CString m_text and CFont font1 are declared in FesbDoc.h
ON_COMMAND(ID_EDIT_FONT, &CFesbView::OnEditFont)
void CFesbView::OnEditFont() {
CFesbDoc* pDoc = GetDocument();
::::WHAT TO ADD HERE:::::::
Invalidate();
}
View 5 Replies
View Related
Feb 17, 2014
This code ran well until i added in the ToLower function which is supposed to convert the char array string to lower case (based off ascii strategy -32). correct this function so it converts string to lower case and doesn't get errors.
#include <iostream>
#include <string>
using namespace std;
const int MAX = 81; //max char is sting is 80
void ToLower(char s[]);
int main(){
string y_n;
[Code]...
View 1 Replies
View Related
Feb 19, 2015
I'm trying to determine the number of times I have to change each specific character in a string to make it a palindrome. You can only change a character one at a time from the end.
Example: "abc" -> "abb" -> "aba" should print 2. "aba" will print 0 because it's already a palindrome. "abcd" -> "abcc" -> "abcb" -> "abca" -> "abba" will print 4 because it took 4 changes to make a palindrome.
I'm not too sure how to approach this - I figured out the case where if it's a palindrome (if reversed string is the same) then it'll print out a 0.
int main() {
int number;
cin >> number; //expecting a number for first line user input
for (int i = 0; i < number; i++) {
string str;
[Code] ....
View 1 Replies
View Related
Oct 15, 2013
So the program reads contents from a text file into a vector and then the user enters in a string that they want to search for. The program iterates through the vector to find the string and then saves that line to another vector to display later(incase there is more then 1 instance of the string found).
Here is what I have atm:
void doSearch(vector<string> &phonelist, string searcher, vector<string> &holdNumbers)
{
int i = 0;
string value;
[Code].....
I just get an R6010 error -abort() has been called.
View 2 Replies
View Related
Aug 26, 2012
I'm trying to explode a string and put each word in a vector. I don't know how to use stringstream yet, and I'm trying to get practice at using some of the member functions of string. I can get the first word in the right position, but then I can't figure out why I get the 2nd word plus the first 3 chars of the 3rd word in the 2nd position.
Code:
#include <iostream>
#include <string>
#include <vector>
[Code].....
View 2 Replies
View Related
Nov 3, 2013
New to C++ lambda, got two question in regards of searching for strings within a vector struct.
Minimalistic example:
Code:
struct singleFile {
std::string filename;
};
std::vector<singleFile>myStorage;
myStorage.push_back(singleFile());
[Code] ....
1) why does found always return 0 eventhough the filename exists?
2) I don't know how to pass an actual search string instead of the hardcoded approach as above with filename =="
View 6 Replies
View Related
May 23, 2013
I have below classes
Code:
class moClassValueContainer {
public:
moClassValueContainer();
moClassValueContainer(string,int);
[Code] ....
In my main.cpp, I have blow loop
Code:
for (xml_node tnode = it->first_child(); tnode ; tnode = tnode.next_sibling()) {
Container tmpContainer(tnode);
if (tmpContainer.getType() == SINGLE) {
string t = tmpContainer.getName();
[Code] ....
I cannot push_back(t). I examined the code with debugger, t has correct string value assigned, but even after 20-30 iterations, there is no element for headerFields vector.
View 1 Replies
View Related
Oct 14, 2014
I am having trouble with parsing out string value into a 2D vector. Suppose i have the string "attack at dawn " consisting of 15 characters, i will like to store it into a 2D vector with 5 rows and 3 columns and the result is as follow.
Vector[0][0] = "a"
Vector[0][1] = "t"
Vector[0][2] = "t"
Vector[1][0] = "a"
Vector[1][1] = "c"
[Code] ....
View 1 Replies
View Related
Feb 3, 2013
I was working on a program which compares sequences of characters, counts the differences between them, and displays them. I had the sequence inputted as a string (into a vector so any number of sequences could be chosen), and then, the way I tried to check the strings for differences, was by converting the string to a (multidimensional) vector of chars:
vector< vector<char> > sequencesC;
for (int a = 0; a < sequenceCount; a++) {
cout << "
Enter sequence " << a+1 <<" name: ";
cin >> sequenceNames[a];
cout << "
[code]....
However, it crashes (as shown above) when I try to set, by a for loop, every char of a multidimensional vector (sequencesC) to the same char of the data vector. Is there any way I can convert the string to a char vector?
View 7 Replies
View Related
Apr 5, 2013
I want to find that whether the 2d Vector table having duplicate or not. I can see lot of programs for removing duplicates by using unique STL algorithm. Which is the best way to find " is Duplicate or not " for 100,000 Records.
View 1 Replies
View Related
Sep 21, 2013
Here's my code so far:
case DELETE_TITLE:
std::cout << "Game to remove from list: ";
std::cin.ignore();
getline(std::cin, gameTitle);
for (iter = gameList.begin(); iter != gameList.end(); ++iter) {
[code]....
It deletes the text from the string but not the index it self.
It deletes the text but when I print the list of game titles it has it there blank. I want it to completely remove the object from the vector from where it was deleted
View 1 Replies
View Related
Oct 13, 2014
I am having trouble with parsing out string value into a 2D vector. Suppose I have the string "attack at dawn " consisting of 15 characters, i will like to store it into a 2D vector with 5 rows and 3 columns and the result is as follow.
Vector[0][0] = "a"
Vector[0][1] = "t"
Vector[0][2] = "t"
Vector[1][0] = "a"
Vector[1][1] = "c"
Vector[1][2] = "k"
Vector[2][0] = " "
Vector[2][1] = "a"
Vector[2][2] = "t"
etc...
Here is a draft code that i did but is not working as desired.
vector<vector <string > > plaintextVector;
vector<string> row;
string totalString = "attack at dawn ";
int dimension = 3;
[Code] ....
View 4 Replies
View Related
May 9, 2014
I would like to compare two strings, one of which is an independently stored string - the other being a string within a vector. However, i do not know how to do this, as
string thisString = "yes";
vector<string> stringHere;
stringHere.push_back("no");
if (thisString == stringHere[0]) {
cout << "It worked." << endll;
}
does not appear to work.
View 2 Replies
View Related
Jul 10, 2013
The error is unclear but suggests Its received a bad pointer from another heap. It references dbgheap.c line 1322 and assertion failure
I have two string vector functions the first is called from the main function, the second is called from the first.
Their purpose is to receive a string of text and numbers in a semi-specific format, which the main body of the code reads from a text file, and delaminates the data as to return the first variable in the string as the variable name and the second as the variable value. Along the way it filters out a lot of the unwanted whitespace and punctuation.
E.g "{ VariableNameA 123 }" would be returned as "VariableNameA" And "123"
The code works perfectly for most of the lines in the text file but fails on one particular line where the first variable is 25 characters long. Basically it works for anything 22 characters or less. There are never more than 4 elements in the vector and each element is never intended to be longer than 25 characters.
It fails trying to return from the second split function to the first split function.
Is there a limit to the size of each vector element? I'm struggling to find a way round this without having to rewrite the whole thing.
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
[Code] ....
View 4 Replies
View Related
Jan 27, 2014
For the last part of this problem, if player 2 loses the game of hangman, I need to display the letters they did get right. In order to do that, I believe that I need to traverse vector v for the elements that exist in the string hiddenword, and then print those characters that match.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int letterFill (char, string, string&);
[Code] ....
View 1 Replies
View Related