I have data that is coming into my buffer via popen (process data, not a file). Every seven records is a new set [0-6]. I am trying to 'print out the array line/element value' and 'change the value of element [2] to 0', but my loop appears to be looping through every character and not just every line?
I have data that is coming into my buffer via popen (process data, not a file). Every seven records is a new set [0-6]. I am trying to 'print out the array line/element value' and 'change the value of element [2] to 0', but my loop appears to be looping through every character and not just every line?
199729173 2014-11-16 10:09:34 Found String! 198397652 2014-11-14 15:10:10 Found String! 198397685 2014-11-14 15:10:13 Found String! 198398295 2014-11-14 15:11:14 Found String!
I am a newbie to C++ and VS ++. I have created a windows form application by dragging and dropping button, label..etc. i wish label text to be appeared as return value from a function. The function returns ' const char* '.how this returned string pointer can be used to display label text.?
I have a file which contains a year and the name of an associated file to be read. I need to extract the data in the txt file and perform some calculations.
( year data file) 2004 2004data.txt 2005 2005data.txt 2006 2006data.txt
Here is what I do. I first declare "char yeardata" and then pass "2004data.txt" to it. Then I call yeardata in ifstream to extract the data inside the file "2004data.txt". The problem is that char yeardata is not constant so I cannot pass the file to it. It doesn't work if I change "char yeardata" to "const char yeardata".
Code: int oldnewcomp_temp(char* lcfile) { using namespace std;
I want to use a const char* as a buffer. I am reading values from a file and adding them to a buffer. How to extract the values is simple enough. I am reading through a filestream, reading each character into a char pointer and progressing that char pointer every time. I have another char pointer marking the start positon
eg.
char *mychar = new char; char *char1 = new char; char *char2 = new char; const char *constchar ; char2 = char1; while(filestream.read(mychar,1) { *char1 = *mychar; ++char1; }
Then I get this problem: constchar = mychar; // const char* = char*.
Constchar does not catch all the data in other words. At some stage some data is lost due to zeros in the data.. How can I put values into a const char and get around this problem? The const char* will //only record everything up until the first zero.
I'm currently finishing up an assignment that was half written by my professor. Below in the testGrades section of code there are two errors both are the same message.
Error: no matching function for call to Grades:: Grades(const char [15])
Test Grades
//Purpose: Test program for the class Grades // Create stu1 Grades object // Add 5 grades to stu1 - only 3 can be stored in stu1 - other 2 discarded // Create stu2 Grades object // Add only 2 grades
What is the programmers responsibility with respect to const char * returned by various functions, like the C++ string class c_str() function which returns a const char * to an c style string array? In VC++ I cannot delete a const char * which holds a string literal. Take the following code for example:
Code: void func() //a useless function with illustrative code { string s1("abcd"); string s2("efgh"); const char * cc1 = s1.c_str(); //c_str() returns a const char * c style string pointer s2.c_str(); //this returns a const char *, which must be allocated on the heap right? delete cc1; //produces run time error in Release mode in VC++ }
The problem with the above code snip is that space is allocated on the heap (or so I believe) for the const char *'s returned by the 2 calls to c_str(). The delete attempt fails and there is no opportunity to delete the space allocated by const char * because its not assigned to anything (however I see c_str() used this way extensively)
So, if I cannot delete a const char *, how does the memory get recovered? Perhaps the string objects s1 and s2 themselves have pointers to the items on the heap made by c_str() calls and they get deleted by the destructors of s1 and s2 when the function ends?
I have a class that defines a window (a popup dialog of sorts), and I want the name of that window to be constant. The only problem is that the name of the popup needs to match the title of the parent window, and I get the name of the parent in the constructor. So how do I go about defining this member variable to be constant and initializing it with a value in the constructor?
I want to do something like this, but I know this isn't allowed:
/* class.h */ class foo { public: foo(*parentWindowPtr);
[Code] .....
I should mention that yes the name of the parent window is const char *, and I would like to keep it this way.
I'm having trouble returning a char array by a function, here's the code. The problem is the 'reverse' function, the purpose of the function is to send two char arrays, 'newline' containing the char array, reverse it and place it in the 'rev' char array then output it back in main, however the output remains blank so I assume there must be something wrong with the reverse function.
Code: #include <stdio.h> #define MAXLINE 10 int fgetline(char line[], int maxline); void copy(char to[], char from[]); void reverse(char forw[], char rev[], int arrsize);
In the below program, When the getline function is called, it passes a char array of size 1000 by VALUE. It must have passed by value because there is no pointer or reference in the argument list of the getline function definition. And if that's the case, when exiting the getline function, isn't the s[] char array destroyed? And if it is destroyed, then when we reference line in the copy function, what are we actually copying?
#include <stdio.h> #define MAXLINE 1000/* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], char from[]); /* print the longest input line */ main() {
i am trying to describe the unusual situation where you declare a class member function with this format:
bool class::function_name(void) const
Specifically where the 'const' follows the parameter list. It is my understanding this is a very useful way of ensuring that whatever code you put in the function definition cannot change any data members of its class.
However I have recently read that this form of declaration should not be used as it leads to less optimized and slower code. Is this correct?
I'm getting a "passing...discards qualifiers" error on my if statement and not sure why because I'm not changing anything. I know removing const or making test mutable fixes the issue. I've been taught to always make a function const if it doesn't change anything, in which case, have I finally come across an acceptable time to use mutable?
Are there other ways of calling a const/non-const override? I want to defined some functions in terms of others, particularly accessors which might or might not require constness- in order to not copy & paste code. This is my current solution:
difference between const and static const, more effectively. I know the basic concept of const and static but I need clear explanation of declaring "const" and "static const"
Is there any way to cast a non-const variable to const one?
I want to read variable n from file and then use it to declare array "int arr[n]", but because n is non-const, the compiler doesn't allow me to do that.
I am trying use a print function to print out data in a struct. My questions are:
1. I have to use pass by reference. For the print function, I am passing the struct pointer as a reference, however, I don't want the print function to accidentally change anything. How can I make it use const to ensure that?
2. The deleteprt function doesn't look right to me. I feel like it should just be delete ptr not delete [] ptr.
I am required to write a program which, when given an nxn 2D array of char, and the specified coordinates of a specific point in that array, returns thelargest number of horizontal, vertical or diagonal contiguous (side-by-side) sequence of points of that same char value that intersects with the given point.
The way I took on this problem was to:
1) First find out the number of points with the same char value up, down, right, left, north-east, north-west, south-east, and south-west of the given point.
2)Add up+down+1(the one is for the point itself), north-west+south-east+1, etc...
3) Finally I compared the four values (updown, rightleft, NESW, NWSE) and returned the largest one.
Well, that's how the program is supposed to work in theory but as you can probably guess it doesn't work. In addition to telling me what I'm doing wrong, is there a simpler way to do what I am trying to accomplish?
Here's the code:
Code:
int findLongest(char **board, int n, int row, int col) { char current; int rightleft, updown, NESW, NWSE; int r, c, c1=0, c2=0, c3=0, c4=0, c5=0, c6=0, c7=0, c8=0, d; int t1=1, t2=1, t3=1, t4=1, t5=1, t6=1, t7=1, t8=1; current=board[row][col]; //check Above: col remains the same for(r=row-1;r>=0||t1!=0;r--) //with the condition r>=0 I made sure not to accidentally check values outside of the array
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;
I have been assigned the following task and I am having difficulty in getting it to compile. The compiler is stopping at line 27 but I don't no what the error is.
The task is as follows:
Write two functions with the following function prototypes:
int my string len(char str[]) void my string cat(char dest[], char src[], int dest size)
Both functions should take zero-terminated strings as input. The first function should return the length of the string to the calling function. The second function should take a source and a destination string and the total size of the array pointed to by dest. It should then concatenated the source string onto the end of the destination string, if and only if the destination string has the capacity to store both strings. If the destination string does not have the capacity it should not alter either, print and error, and return to the calling function. Demonstrate the use both the above functions by using them in a program in which you initialise two character arrays with two strings, print out their length, print out the combined length, and print out the combined string
And this is my code so far:
/* A program to demonstrate string concatenation */
#include <iostream> #include <string.h> using namespace std; int my_string_len(char str[]){ // function to calculate length of a chracter array int len = 0;
I'm working on a piece of code written long time ago. Without getting in the details or too much context here, there is a function that declares an array of char of a size of 350,000 elements, in order to fill it (using a pointer) with the list of all running processes on the machine (using "ps -ejf" on a Linux box).
The size of the char array has been changed from 40,000 to 350,000 sometime along the years, probably because of a lack of space required.
What kind on data structure / storage would you use to store the running processes in order to eventually search for a value in it?
Ok so I am back in school and it has been to long since I have used c++ I can not find or figure out how to convert a char array into a int array so i can add numbers really large numbers. the string is being passed into the function as a const char*...
If I have an array of some class, and that class has const members, is there some way I can call a custom constructor on elements of the array?
I can't seem to reinitialize an element in foos in the example below. A thread on stack overflow mentioned the copy constructor show allow it, but I get "no match for call to '(Foo) (Foo&)'" when I try it.