Nothing is wrong with that code.
WolfPack 491 Posting Virtuoso Team Colleague
WolfPack 491 Posting Virtuoso Team Colleague
Nothing is wrong with that code.
Well, the behavior is certainly undefined. You don't want to unpredictably overwrite other variables, do you?
Nope. I just went through the specs for strcat in MSDN. It says that it does not check for buffer overruns. So that clears the thing with my compiler.
I) Its gives the warning that
The function returns the addr. of a local variable
Can anyone explain the logic behind this?
You are declaring temp as a local array of characters. You are returning the address of this array. When the function exit, the memory for this local variable is lost, and it's contents cleared. So the address returned to you from the function is not of any use.
II) replacing the
char temp[] = {'s', 't', 'r', 'i', 'n', 'g'}
withptr char* temp = {'s', 't', 'r', 'i', 'n', 'g'}
makes the prog jump out.
What is the prob i am facing here?
You must mean char* temp = {'s', 't', 'r', 'i', 'n', 'g'}
. Well temp is a pointer to a character. Not an array of characters.
]
III) What is the difference betweenchar temp[] = {'s', 't', 'r', 'i', 'n', 'g'}
char temp[] = {"string"}
The first one does not store the NULL terminator in temp. But the second one does.
Thank for both of you guy's reply.
But a little further confusion here.Wolfpack: are you saying "You can do strcat in this case also", meaning for declaring as "char * p= "string"; "?
If so, it doesn't work. It compiles, but if I tried to print out the conctated phrase, I got "Bus Error".
Yep. That is what I am saying.When I did it in C++ and I get an error. I compiled it as a C Program and it executed without any problem. (By the way it is using Visual C, so it maybe because the compiler is non-standard)I also want to clarify this. That was why I answered it like that although I may be incorrect.
also a quick programming ethics question.
I heard that in principle, when programing I should try to "avoid" array as much as I could, instead try to use pointers as much as I could.
Is this true?
I really don't know. Pointers give me shudders so I use pointers only if I really really have to. But dont quote me on that. I really have no idea.
You can do strcat in this case also.
You can't strcat onto it because there are only 7 bytes of space for it, and the bytes after the string are used for other information.
I see contradicting replies in this case. How come? I agree with the part where other information can be overwritten. But is this illegal?
I am confused about what is the difference between:
A: char *p="string";
B: char q[10]="string";
See the diagram in this link. q will be like 6 squares filled with characters from 's' to 'g' and 4 squares filled with 0.
For A:
I can do "p++", which gives a pointer points to "t".
But I cannot do command like
"strcat(p, " test")", for example.
You can do strcat in this case also.
For B:
I can do "strcat(q, " test")",
but I cannot do "q++".
You can do strcat only if there is enough space left in the array. For example if you defined q as q[7]="string";
, strcat(q, " test")
wouldnt work. It will be writing in memory out of limits.
The reason you cant do q++ is because q is constant. You cant change it.
Road Trip
Thank you for your help, I read the it, and was very usefull, but still I need a function that can convert from escape URL to unescape with unicode chars, if this possible at all, or maybe I'm trying to do something that is not possible at all.
Thanks
UrlUnescapeInPlace takes LPTSTR as it's URL pointer. LPTSTR is a pointer to a TCHAR. A TCHAR is defined as wchar_t when you have defined the UNICODE constant in your project. Since wchar_t is wide character, you only have to define the constant UNICODE as a preprocessor command to make this function work for unicode strings. So either you write #define UNICODE in a header file common to all your source files, or add UNICODE as a preprocessor directive to your project file. The second method is the easiest.
More on unicode in windows.
@WolfPack
Can you please tell me which compiler are you using :cry: ?
Visual C++ 2003
The C code works for me. Output for input 3 is 6.0000
You guys are a big group of uncreative people!
*Looks smugly at Dani*
There shouldnt be any problem in dereferencing array elements if you use the code you posted. So maybe it is in the code you have not posted. Can you post the code where you actually get the error?
If you don't have one, what's stopping you from starting?
Well I guess I can't make the commitment of writing something down daily or at a decent frequency. And of course there is the slight problem of not having anything exciting to write about also. I tried keeping a diary, but that didnt work out either. But I really like reading other people's blogs(and other people's diaries if I manage to get my hands on them :-| ). Of course there is the Daniweb blog which you have been contributing to a lot, and I really like your articles too. And some external blogs in the web. Guess it is a personality trait. Some just blog while the others do not.
when i type a[0].
You mean type a[0] like this?
Cell_Info a[0];
???
All that mod power has gone to your head wolfie. ;)
ehehee. Well it is your fault that I am a moderator(you know what I am speaking about ;) ) Anyway I realized this would come up. Well what can I say.... I wouldnt have said a thing if server_crash called you an [Profanity edited for the G-rated among us....] :p I would just sat back and enjoyed it. he he he. But I think Salem does not deserve to be called that.
Why don't you head on over to dictionary.com and tell me which is worse, what he said, or what I said. I'm still fuming over that. I could have said a lot more but I had some self control.
Okay. I did. I found what Salem would have meant.
# A willingness or tendency to subject oneself to unpleasant or trying experiences.
This was what I thought Salem meant when I saw his reply at first. Is this so much agitating to you?
Edit:
Hey, ya know, what's wrong with a little excitement? I knew YOU would understand
Okay. All is cool. I consider the original intent of this thread achieved.
It's reading the 5.5, but for some reason it's being truncated in the conversion:
int num; if (sscanf(text,"%d",&num) == 1 && num > 0)
But %d
is the format specifier to read an integer. So it will read only the integer part of the contents in text
. You have to use something like %f
or %lf
. I dont know what is standard specific ( I thought lf was the correct one for double, but someone said above that it was wrong, but my MSVC compiler only reads double for the %lf specifier) . I also saw some suggestions for a double in the above replies of this thread. you can lookup them.
Note:
English is not my first language. I dont know if being suggested to be a masochist is offending so much. But I know that being called an asshole is. I think that type of language is uncalled for. Think twice before posting things like that.
Is there a reason why the fgets is delimiting a '.'? If I enter 5.5 it truncates to 5... How can I get around this?
Not according to the specification. It should stop only at a Newline or EOF character.
Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first.
How do you know that it is reading only 5? Are you using a debugger or using printf? If you are using printf maybe you are using a wrong format specifier.
Is there a way to unload, detach, or close a file from an application such that the parent directory can be moved without problems?
Yes. Refer the File Management Functions in MSDN.
How does the application/OS recognize that it cannot move the active file?
When an application opens the file, the programmer can set various sharing access rights so that the OS knows what to do when a request that affects the same file comes while that file is still open. You can see the list of flags that can be set under the Security Attributes section for CreateFile.
Nobody will write code for you. If you have already written a postfix parsing function you will find infix easy. Try and post what you have done upto now. We will help you correct errors.
Also you are typing J instread of I.
J need
J make
Correct that.
If scanf returns a certain value, it failed. Your documentation will tell you what values mean what failures.
As you may know,scanf returns the number of items stored from the stream, and EOF only if the EOF character was read at the beginning. So when you input 123.asdf
for a double value, scanf will store 123
as the value but not return a unique error value. In this case the return value will be 1
because it stored one value from the stream. This is not a error checkable return error. Is there anyway to get over this? I think that was what server crash was looking for.
I havent programmed in TurboC in my life, so I cant help you with your settings. But theoritically if the include, and library directories are configured properly, there shouldnt be any problem. Did you check that stdio.h is actually inside that directory?
Anyway why do you think that Microsoft VC isnt a good compiler for learning C? Since TurboC is a bit outdated, it would be better if you use a modern compiler like Microsoft VC or DevCPP. You can get the VC 2003 toolkit for free if you only want a commandline compiler. Although Microsoft has replaced it with the VC2005 compiler, there are external sites that lets you download it. Google for it. When it comes to compilers, there is nothing called simple or complex. All of the above compilers can be used in the command line, and that will be enough for a beginner. The way you compile is more or less the same for all compilers.
Since you write the code according to the psuedocode, the pseudocode should reflect your code. So post code. Maybe then we can show you how to write a psuedocode. But it will be very unlikely that someone will do it for you.
(sizeof array / sizeof array[ 0 ])
I think it is just you. :)
Edit:
ehehe
I saw that you have posted in that thread after this post too.So I guess you also have solved it.
Then I can sort that class by their class definition?
Is that making any sense?
I don't get the above part. You have only one class, no?
Maybe I am not understanding your question, but can't you use a std::list for that?
Okay. Got it. Thanks for the additional info too.
Is it possible to read data from a binary file to a C++ class using istream::read
? I know it can be done for structures, but I couldnt do it for a class.
[Edit]
Want code?
#include <iostream>
#include <fstream>
class BITMAPFILEHEADER
{
private:
char type[ 2 ]; // Magic identifier
unsigned int size; // File size in bytes
unsigned short int reserved1; // Reserved; always zero
unsigned short int reserved2; // Reserved; always zero
unsigned int offset; // Offset to image data, bytes
public:
friend std::istream& operator>>(std::istream& s, BITMAPFILEHEADER& r);
};
std::istream& operator>>(std::istream& s, BITMAPFILEHEADER& r);
std::istream& operator>>(std::istream& s, BITMAPFILEHEADER& r)
{
#if 0
// This does not work
s.read( reinterpret_cast <char*>(&r), sizeof(BITMAPFILEHEADER) );
if( s.bad() )
{
std::cerr << "Error reading data" << std::endl;
exit( 0 );
}
#endif
#if 1
// This works
s.read( r.type, 2 * sizeof(char) );
if( s.bad() )
{
std::cerr << "Error reading data" << std::endl;
exit( 0 );
}
s.read( (char*)&(r.size), sizeof(unsigned int) );
if( s.bad() )
{
std::cerr << "Error reading data" << std::endl;
exit( 0 );
}
s.read( (char*)&(r.reserved1), sizeof(unsigned short int) );
if( s.bad() )
{
std::cerr << "Error reading data" << std::endl;
exit( 0 );
}
s.read( (char*)&(r.reserved2), sizeof(unsigned short int) );
if( s.bad() )
{
std::cerr << "Error reading data" << std::endl;
exit( 0 );
}
s.read( (char*)&(r.offset), sizeof(unsigned int) );
if( s.bad() )
{
std::cerr << "Error reading data" << std::endl;
exit( 0 );
}
#endif
return s;
}
Okay. Figured out the problem. Darn IE Extention for Firefox. Thanks again.
Okay Let's go through this step by step.
1. Press
or [Quick Reply]
2. Type any word
3. Double click on that word so that it is selected ( highlighted)
4. Press Delete
5. Did the word get deleted? Not in my browser.
Hope I made myself clearer.
Thanks.
I can't edit words that are selected by double clicking them. Cant change, delete, or anything. I have to highlight it by dragging the cursor, which is very very inconvienient.
Edit: If it matters I am using Firefox. But I didnt have this problem before the current VBBulletin (I think that is what it is called ) upgrade.
how do i make it save a whole sentence
replace
cin>>word;
with
getline( cin, word );
how do i write a program that can read from the file and print it on the screen?
ifstream fin("biscuit.txt", ios::in);
while( fin>>word )
{
cout<<word;
}
how do i add data into the file without overwriting the previous data
ofstream outl("biscuit.txt", ios::app);
will open the file in append mode.
is using ifstream and ofstream the same as using fin and fout? cuz i cant find enough info about fin or fout anywhere
What is fin and fout? Are you sure that you are not confusing two fstream variables?
Recently I installed Visual Studio 2005 .NET on my computer. I wrote a Win32 program. But my program doesn't run on PCs that doesn't have Framework 2 installed on.
Is there any settings for VS 2005 .NET to make my code run on all PCs?
Or what DLL file should I put in my .exe file directory?
Or, if I had installed "Visual Studio" instead of "Visual Studio .NET" would there have been the same problem again?Please help me, I am all stuck!
I don't have a way of testing this. But try setting the vaue of
[Configuration Properties]-->[General]--->[Common Language Runtime Support] to "No Common Language Runtime Support".
WolfPack is correct if you want to put the name of the file into your code. From the post, I read that you wanted to pipe a file into it. It would help if you posted your operating system. This is out you can pipe files on a Linux machine:
> output redirect
< input redirectYou could type in a command something like this:
./myprogram < inputfile.dat > outputfile.dat
This would use whatever information is in inputfile.dat as your 'standard input'. Likewise it would redirect your standard output to outputfile.dat.
This way, you can use a file with your program without rewriting the code to open a file.
Yes you are correct. You can do that in windows too. Then no need to rewrite the code. Sorry I didnt see the piping part. My mistake.
Although I dont understand why you decided to change the program I gave, which is working perfectly, here is a partly corrected version of your program.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string id,
rest_of_the_line,
name_and_address;
// sub_header is a flag that indicates the sub header should be output
bool sub_header = true;
char gate;
float total =0;
float amount_gate_A = 2.20;
float amount_gate_B = 2.80;
float amount_gate_C = 2.30;
float amount_gate_D = 3.80;
string heading;
heading = " Interstate Mototrway Pty Ltd invoice statements";
cerr << heading<<endl;
cerr <<" ----------------------------------------------"<<endl;
while (true)// How do you intend to break this loop?
{
cerr << " customer id : ";
cin >> id;
cerr << " station gate : ";
cin >> gate;
cin.ignore();
cerr << "adress and name : ";
getline( cin, rest_of_the_line );
switch(gate)
{
case '@':
if ( total > 0 )
{
// output the total
cerr << "Total = " << total << endl;
// save the address as it will be overwritten in the next cin
name_and_address = rest_of_the_line;
// clear values of total and sub_header
total = 0 ;
sub_header = true;
}
else
{
if ( sub_header )
{
cerr << " 1. Customer ID:" << id << endl;
cerr << " . Name and Address:" << rest_of_the_line << endl;
cerr << " 3. Date Time Station Amount"<< endl;
sub_header = false;
}
}
break;
case 'A':
// use substr to extract the date and time
// The …
To get the values from the file, you have to open the file. Your program takes values from cin ( which is the standard input ) and outputs results to cout ( which is standard output). To be able to get values from a file. you have to open the file and then replace cin with the pointer for the opened file. Try searching for C++ File Handling tutorials. You will have to use
file open
replace cin with the file you opened
file close
You have done what?
You need troubleshooting on what?
Care to be more specific? Tell us where you are getting it wrong. We can't compile and run the program for you.
You wont find much tutorials on that particular subject. So try searching for
1. Reading data from a file
2. Inserting data to a list.
and then try combining those two. You will not get much help just by saying that you searched for this and that and you couldn't find it.
Short story: How do you compile two .cpp files at once?
Okay I will go with the short story.
You RTF(ine)Manual.
Well any decent compiler should be able to compile multiple source files at once. You should be able to compile it by dmc sourcefile1.cpp sourcefile2.cpp
, without any trouble for simple files.
Here is a complete program that reads from cin, and prints to cerr. Now you should be able to modify this to read from a text file. It is fairly simple to do it with file reading. Just open the file to a file object and replace cin with that file object. After the processing close the file at the end of the program.The format of textfile was assumed to be like.
3435344 @ L Brooks,12 Shaftsbury Road,Burwood NSW 2134
3435344 A 2005/02/22/08:22:41
3435344 A 2005/03/20/08:22:41
3435344 B 2005/03/23/18:22:41
3435344 B 2005/04/10/28:22:41
3435344 C 2005/03/11/08:22:41
3435344 C 2005/05/10/14:22:41
3435344 C 2005/05/19/06:22:43
3435344 D 2005/05/01/01:26:41
3873242 @ N McGoldrick,8 Colless Place,South Melbourne VIC 3205
3873242 B 2005/03/29/02:40:59
3873242 B 2005/05/16/02:40:59
3873242 C 2005/04/07/22:40:59
3873242 D 2005/03/02/02:40:59
#include <iostream>
#include <string>
using namespace std;
int main()
{
string id,
rest_of_the_line,
name_and_address;
// sub_header is a flag that indicates the sub header should be output
bool sub_header = true;
char gate;
float total =0;
const float amount_gate_A = 2.20;
const float amount_gate_B = 2.80;
const float amount_gate_C = 2.30;
const float amount_gate_D = 3.80;
while ( cin >> id >> gate )
{
getline(cin, rest_of_the_line);
if ( gate == '@' )
{
if ( total > 0 )
{
// output the total
cerr << "Total = " << total << endl;
}
// save the address as it will be overwritten in the next cin
name_and_address = rest_of_the_line; …
1) Please first notice you just described above how to enlarge the Windows command prompt console, which I've already enlarged at the right time (I mean when installing my Windows).
First time I heard of someone like you, who changes the console size at installation.
2) However, you might just learn now that the default size of C++ Builder programs' console is a completely different matter than the one of the command prompt Windows accessory. Therefore your advice from above might be useful to you and your lovers at most.
Too bad. Don't you have anything better to do with your lovers?
Well if it is different from the command prompt, you can do it like this.
1. Click the icon of the C++ Builder command window
2. Select properties
3. Change the layout
3) Please do notice I was asking about how to do something while your answer was about how not to do that. So you might agree you were unpolite.
No I dont agree. I have done the same thing for my visual studio command prompt and it worked. I dont think you have even tried my solution. Anyway since this is your first false accusation, I will leave you in peace.
If you are a beginner better to do it by hand. Right click the command prompt shortcut in accessories, and select Properties. Then select the layout tab. There you can change the width and height. There are other options that can be set in the other tabs. Then when you click apply,select "save properties for window with same title."
I had a similar problem. As it turned out
1. The firewall in the linux machine was blocking the samba service - stopped me from browsing the linux computer
2. This Security Enhanced Linux ( SE Linux ) thinga ma gingy which is in Fedora 5, was blocking samba - stopped me from browsing the shared folder after above 1 was fixed
After fixing the above settings to give permission for Samba, it worked. Maybe you should give it a try.
difference between passing by value and passing by reference.
When you pass a variable to a function by value, the function creates a local copy of that variable and acts upon it. The variable that you passed into the function is not changed. So after the function finishes it's operation the original variable's value remains unchanged.
When you pass by reference, the function does the processing on the original variable's memory location. So the changes that are done to the variable are saved even after the function returns after processing.
You are passing the stk variable by value. You should pass it by reference if you want to update the original value after it is returned from the function. Try this
void push1(CStack& stk) {
stk.push(1);
}
int main(void) {
CStack mystack;
printf("Stack initially: ");
mystack.print();
push1(mystack);
printf("Stack after the push: ");
mystack.print();
}
That will be asking me to do the file reading part of your assignment.
You Open the file
Do the following until the file is finished.
Read a line
Print the line
Go to the Next line.
Welcome to Daniweb. The first hint will be to first start doing the assignment. Looks like you are intimidated by the assignment size, and are afraid to start. Once you start doing, you may find dead-ends, and then pose a specific question here.
Since this looks like an assignment on string handling, and file handling, you will have to look them up in your lecture notes and C++ reference book. You will have to read a single string, process it, do the calculations and move to the next string. Also since the use of arrays are discouraged, that means there should be little need to store the values of the toll-fee and you should be able to calculate the value and print it before continuing to the next customer.
Good luck.