|
I've written code for a few different computers, but so far not the mac.
Lately I've been using:
Visual Studio for C++ on the PC,
CodeWarrior/Freescale for C cross compiled from the PC to a microcontroller, and
Visual C++ 1.52 plus some add-ons for C for an 80386EX embedded system.
None of this code is using iostreams & I have to consider myself weak in that area at the moment. I would have just looked at the responses you got to see what I could pick up, but seeing rtf rather than plain text was a glaring issue for me. In the last week I've had to deal with a couple of mismatched formatting issues.
modified on Thursday, January 21, 2010 12:31 AM
|
|
|
|
|
Jacob Dixon wrote: #include <cstdio>
#include <fstream>
#include <istream>
#include <ostream>
#include <iostream>
Of these, only fstream and iostream are needed.
Jacob Dixon wrote: if (not in)
Have you tried:
if (in.fail())
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I'm wondering if there is something else this book is talking about? It says C++. But the code above is exactly what the book put in. Interesting...
I take it you didn't see my post before this one but it seems it works fine in netbeans, but in xcode it wouldn't work. I got the code that I was having trouble with to work perfect in netbeans. What could cause this?
|
|
|
|
|
hello, i'm usig this spin control, it works fine, in my application i draw some lines when the spin control increases or decreases an edit box,nice, but when the control change the acceleration it seems like iDelta and iPos (from UDN_DELTAPOS) change their behavior, because in that moment the line plotted is wrong , but after and before that moment all works fine, and the variable shown in the buddy edit box is always well. I guess that in acceleration changes happens somethig that i dont know whith spin control. Any ideas ? i can`t find any good information about how this controls works, specially the acceleration thing.
|
|
|
|
|
Hi to all,
I have a small doubt.Is it possible to use arithmetic operators between two char* variables, like bellow
char* conc(const char* c1,const char* c2)
{
char* c3=c1+c2
return c3
}
Above code is giving error "adding two pointer variables is invalid". Please tell me how to concat(add) two char*.
To invent something, you need a mountain of junk in your mind.
---------------------Thomas alva edison
|
|
|
|
|
You could use strcat()[^] to concatenate strings.
Alternatively, you could use one of those string classes (such as CString) which will allow you to just use + (the addition operator) to add two strings.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Rajesh R Subramanian wrote: You could use strcat()[^] to concatenate strings.
Actually my requirement is "not use the strcat() function".
To invent something, you need a mountain of junk in your mind.
---------------------Thomas alva edison
|
|
|
|
|
If you have to stick to the two pointer of char in order to concat them.
You have to creat a new char * with the length of both. Then copy both to the one.
I would suggest using stings.
You could also create a string and use += to do it.
I guess that is your homework, isn't it? Sounds like.
Cheers
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
|
|
|
|
|
sampath-padamatinti wrote: Actually my requirement is "not use the strcat() function".
Why?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Are you new around here? It probably is one of these:
- I let others do my homework
- I am troll
- strcat() isn't safe enough; I prefer strcat_s()
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Which one, sir?
I suppose: strcat isn't safe enough, I prefer mess up with pointers myself.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
My interviewer thrown a question like:
He wants to implement a function where he needs to takes two char* parameters as input at the end it should return a char* by concatenating these two char* values(without using strcat(), strcmp() functions).
required function prototype is
char* concat(const char* c1, const char* c2)
I am unable to solve this one. Please give me the approach
Thanx in advance.....
To invent something, you need a mountain of junk in your mind.
---------------------Thomas alva edison
modified on Thursday, January 21, 2010 2:21 AM
|
|
|
|
|
sampath-padamatinti wrote: I am unable to solve this one. Please give me the approach
David did, here.
However, if you're stuck with the const requirement on the first argument, then:
char * concat(const char * c1, const char * c2)
{
size_t size[2] = {strlen(c1), strlen(c2)};
char * c = new char[size[0]+size[1]+1];
strcpy(c, c1);
strcpy(c+size[0],c2);
return c;
}
if you can't use strlen and/or strcpy :
char * concat(const char * c1, const char * c2)
{
size_t len=0;
char * c;
const char *p;
p = c1;
while (*p++) len++;
p = c2;
while (*p++) len++;
c = new char[len+1];
p=c1;
while (*c=*p++) c++;
p=c2;
while (*c++=*p++) ;
return (c-len-1);
}
Test program:
void main()
{
const char * str1 = "hello, ";
const char * str2 = "folks!";
char * result = concat(str1, str2);
printf("%s\n", result);
delete [] result;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Here is the function as per your requirement:
char* concat(const char* c1, const char* c2)
{
int size1 = 0, size2 = 0;
for (int i = 0; c1[i] != '\0'; i++)
size1++;
for (int j = 0; c2[j] != '\0'; j++)
size2++;
char* result = new char[size1+size2];
for(int i=0; i<size1; i++)
{
result[i] = c1[i];
}
for(int j = 0; j <= size2; j++)
{
result[size1+j] = c2[j];
}
result[size1+size2] = 0;
return result;
}
|
|
|
|
|
Note that in what you are doing, you are dealing with 6 variables. You are talking about the 3 char * variables, but as you said, those are pointers. What you must remember is that pointers are pointers - they are used to point to things, in this case arrays of char. These arrays are not automatically provided. You, the programmer, are responsible for providing them for the pointers to point to.
You have to keep in mind the distinction between pointer and "pointee". Once you do that, you would not say that you want to add or concatenate 2 pointers. Instead, you want to concatenate the 2 strings contained in 2 char arrays and put the result in a third char array. You use the pointers to access the char arrays.
Good luck
|
|
|
|
|
sampath-padamatinti wrote: return c3
Are you really wanting to return a variable that will go out of scope when conc() ends?
sampath-padamatinti wrote: Please tell me how to concat(add) two char*.
Something like:
char *c3 = c1;
while (*c3)
c3++;
while (*c3++ = *c2++)
; While not exact, this is roughly what strcat() does.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
DavidCrow wrote: Something like:
char *c3 = c1;
while (*c3)
c3++;
while (*c3++ = *c2++)
;
MVP 2010 - are they mad?
|
|
|
|
|
Yes, I was wondering too
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
|
|
|
|
|
Why wondering?
It is concise and elegant, functionally equivalent to strcat , here the complete function
char * concat( char * c1, const char * c2)
{
char *c3 = c1;
while (*c3)
c3++;
while (*c3++ = *c2++)
;
return c1;
}
here a test program:
#include <stdio.h>
void main()
{
char buf[100];
sprintf(buf, "hello");
char * str = " folks!";
printf("%s\n", concat(buf, str));
}
He just missed the const reuqirement for the first argument. But this is an OP fault (i.e. David's prototype is better).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
MVP 2010 - are they mad?
|
|
|
|
|
What's that funny, Rick?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
That code is too dangerous for words. As long as the length of buf is greater than the combined length of c1 and c2 plus a null terminator it will work. But as soon as the result overflows all hell breaks loose. This maybe OK for a skilled developer such as yourself, but I would not suggest it as a solution for a newbie.
MVP 2010 - are they mad?
|
|
|
|
|
The code provide what provides strcat , no less no more. I know strcat is a very dangerous function...
God once said: "The newbies should learn C pointers or go to Hell managed".
Moreover, since that was an interview question (see [^]), the OP was expected to have such a skill.
BTW My 5 for the 'skilled developer'.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
MVP 2010 - are they mad?
|
|
|
|
|
Hi,
How can we convert a modal dialog box into modaless dialog box and vice versa by adding some code. This was the one of the interview questions.
Thanks & Regards
msr
|
|
|
|