Best resource that answer your question :
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.10
In short: Just like the rest of your team
Best resource that answer your question :
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.10
In short: Just like the rest of your team
>>Yeah, it's .c_str()
NO. it is .str() for string streams.It will return a string object on which you will have to do a .c_str(). So the corrected code would be somewhat like this:
#include<iostream>
#include<sstream>
int main()
{
std::stringstream arg;
const float pi=3.145745;
arg<<"AdjustSealevel.exe "<<pi;
std::cout<<arg.str().c_str();
system( arg.str().c_str() );
}
Imagine a liked list with no data in it. You get pointer pointing to pointers.( chain of pointers)
When declaring the pointers, always read from right to left
int * p; //Declare p a pointer to int
int* *q;//Declare q as a pointer to, pointer to int
int* * *r//Declare r as a pointer to (a pointer to (a pointer to an int) )
Well this was from C++;
A double pointer has an ability to store the address of a pointer.
If you drop Point #2, Python can be your wand. It is a language which will support easy transformation of your ideas to codes.
If you drop Point #4, C++ satisfies all the above points.
Perl is an interpreted language.
I still would go with python since it is really easy to learn and follow. It is not as fast as most of the compile time languages(like C,C++) but you won't care about speed for most of the matters.(All I meant to say is that it will compete C|C++ in most cases)
Python though requires python runtime requirement. But it is not a bad thing until you are programming stealth tools like trojan horses or keyloggers.
>>or as a reference
Good Point.
Its mostly better to pass arguments by-reference-to-const rather than by-value.
the static_cast still gives the ascii value of 118, is there any way of making the v0 char equal to int 0? (and subsequently v1 char = int 1)
Yes there is : just substract 'v0' instead of stubstracting '0'
#include<iostream>
int main()
{
const char k='v0';//the constant char need to be stubstracted
char c='v7';//chage this to whatever value you want
int i=c-k;
std::cout<<i;
}
Will print 7
PS:
I don't know why are you after multi-character chars. You should perhaps use cstring(i.e. char [3]) to store the value and then call strcmp() from cstring header file to compare with "v0"
//much neater code
#include<iostream>
#include<cstring>
int main()
{
const char a[3]="v7";
std::cout<<strcmp(a,"v0");
}
For Network Programming, I suggest you Read Unix Network programming by W. Richard Stevens. Its really a awesome book.
Also, I would suggest you to go through Beej's Networking Tutorial
As far as Anti Virus Programming is concerned, I don't really have an idea.
You may (obviously) start searching google.
Your problem is perhaps about flushing the input stream.
Read the sticky thread http://www.daniweb.com/forums/thread90228.html
.
You though, have not fully described your problem.
What I feel is, you may not be able to enter the 'marks for c' as the '\n' remains in the input stream and is entered automatically when prompted to enter 'marks for c'.
Try cin.ignore(1000,'\n');
after cin.getline(name,20);
>>hmm thats strange because when i run my code above the cout shows the i value as 70
Yeah, that is strange because it is undefined. It is not defined by the language and so is implementation dependent.
>>To further clarify, I need to enter a char variable and then convert the char into an int. i = c - '0'
is not correct way. What if the value user enter A? Its ASCII value is 65 so 65-48=17 but this is not what you want right?
So you should do i=static_cast<int>(c) ;
which would print the actuall ascii value : 65
PS : Throughout the discussion I am assuming that 'converting' char to int means determining its ascii value. If you meant to convert a character '1' to int 1 then go with the minus '0' aproch
There is no char as 'v0'
char constitute only one character.
Multi-character constants are undefined by the language.
So don't use them.
While converting to int, only last character is read. That means: int i=(int)'ac' ;//will store the ascii value of c
>>
writer.write(&img_pix[i].rgbb[0],1);
I don't know what is the type of img_pix but if it is not char, you better use a cast to char
writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb[0]) ,sizeof(&img_pix[i].rgbb[0] );
>>How should I define the Mem pointer so that I can pass it between String objects so I can refer to the same character sequence??
I guess you would be equating the two pointers Mem=other.Mem; //dont do this
Don't do this. Use strcpy or by character by character copy.
Hmmm, I have seen the exact question before on this forum.
Anyways, first of all I am glad that you have used code tags on your first post.
>>BUT i don't know how I should set the Mem pointer
In the constructor, you should use new operator to assign required memory(determined by the variable called Capacity) to Mem Mem=new char[capacity];
Then you should perform copy of the char[] argument to the Mem by either strcpy() {needs cstring header file} or by character by character copy.
Copy constructor and operator= can be done in the same way.
Remember to delete[] Mem every time you assign it with a new chunk of memory
Look, you are on the right path.
If you have already overloaded the operator= and operator+, you can use them in the definition of operator+= .
Say the parameter of the += operator is aSimpleString.Then, the definition of += operator may look like.
(*this)=(*this)+aSimpleString;
>>Hi .. I need a c++ program dealing with sales and marketing
Hello, I need more than 24 hours a day to solve that!!
Well, here's a TODO list:
Read the Announcement and Sticky Thread :http://www.daniweb.com/forums/announcement8-2.html and http://www.daniweb.com/forums/thread78223.html
Decide, what you want to do actually with your project.
Cast your thoughts into something precise so that it could be molded into programming interactions.
Write a Algorithm and overview of all type of Data Structure you will need (classes, structs)
Try to design the basic interface of your program.
Then start designing the back-end.
Code the above two.
And report any difficulties you face
>>gud is not in any English language dictionary that I know of, so I have no idea what you mean by that.
Hey! may be he meant 'good' by that. I am not sure, but its just a guess!.;)
>im using a turbo c++ program, thank you for the threads
so don't use the header file I mentioned as they won't work on ancient compiler like Turbo C++.
Read the thread. Take a pencil paper and construct a algorithm.
Get back to us with your progress.
All your header files are non-standard use <iostream> <cmath> <string> instead of what you are using.What compiler are you using?
Palindrome problem is not new here. Remember, you should search the forum first before starting a new thread.
Here are few thread I found by simply using the search option :)
http://www.daniweb.com/forums/thread62808.html
http://www.daniweb.com/forums/showthread.php?t=176303&highlight=palindrome
http://www.daniweb.com/forums/showthread.php?t=101091&highlight=palindrome
http://www.daniweb.com/forums/showthread.php?t=94190&highlight=palindrome
http://www.daniweb.com/forums/showthread.php?t=63936&highlight=palindrome
>However return n; in the main performs local objects destruction
extend it to any function. Not just main()
>For integers, it will be just as accurate as the iterative or recursive version.
Okay heres the bottom line:
If you want to calculate the nth Fibonacci number, use the closed form(i.e. the formula mentioned in Post #3).
If you want to print the series till some n, always use the iterative version suggested by ArkM and other.
It may look that the closed form is always better than the iterative form since it will be calculated faster, but it is not. The calculation of power takes more time than simply to add/subtract hence the best choice( if printing the whole series) is the iterative version.
Both are of O(n).
Recursive method in this case is evil. It is not suggested.
Now regarding the limits:
limit.h( newly called climits) header file contains the maximum value of int, float, double etc.
So, it is likely that you may run a loop till the current Fibonacci number is less than the INT_MAX (defined in climits).
>I'm using Code::Blocks with the MinGW compiler
Code::Blocks is a great IDE for sure. I use it when working on a BIG project within a group. As many programmers around me are not 'comfortable' with command line, they say.
Code::Block surely catches eye.
Regarding M$,
I (as Linux users) doesn't go against M$. But the fact remain that I am a OS/FSF fan and ideas of M$ do not fit into minds.
I am just waiting for the day, when everyone would be using a free operating system,(again, free not as in 'free beer' but as in 'freedom of speech')
You may ignore whatever you I said about M$ and remember that "try to use the tools your neighbor uses" so that If you ran into trouble, you can ask help from neighbors.;)
Well, according to me: Its the knowledge about language that counts rather than what tools/text editor you use. Though I believe that compiler plays a important role in programming
First convert your equation to a closed form. The close form clearly is [tex]\sum_{n=0}^{\infty}(-1)^n\frac{x^n}{n!}[/tex]
Now construct a loop and evaluate it to desired n
You would need a function to calculate factorial and powers.
I don't know if I am posting off-topic but there is a little more info about exit();
exit() calls destructors but not for auto objects
A a;
void test() {
static A b;
A c;
exit(0);
}
Will destruct a and b properly, but will not call destructors of c.
std::abort will not call destructors of neither objects.
Source: http://stackoverflow.com/questions/397075/what-is-the-difference-between-exit-and-abort
Oh man!! Ancient Dragon would be snatching his hairs!!
Look kavithabaskar, You are basically using recursions to avoid loops.
Every series has Recursive form and a closed form(non-recursive)[Note that it is not possible for every series to have both forms].
Say, the series 1,2,3,4,5,6,7,8 has a recursive form as [TEX]A_{n}=A_{n-1}+1 || A_{1}=1[/TEX]
and open form as [TEX]A_{n}=n[/TEX]
So, if you know the closed form it is best to use it through loops.
But when you dont know closed form you go to recursive functions.
Who said C is ancient?
He meant to say that the C++ you are using is ancient.
As most of people assume, C is a ancient C++, while it is not.
But the fact remains that DaniWeb has a C forum too, and this problem had been soughed more finely there.
>it says that it can't find 'GetSumX', and 'GetSumY'....
This is because you didn't make them the member function of the class. You can do this by declaring them inside the class in the public.
class CPoint
{
...
...
...
public:
...
...
int GetSumX(CPoint* pCPoint, int nsize)
{
int nvalue = 0;
for (int i=0; i<nsize; ++i)
{
nvalue += pCPoint[i].x;
}
return nvalue;
}
int GetSumY(CPoint* pCPoint, int nsize)
{
int nvalue=0;
for (int i=0; i<nsize; ++i)
{
nvalue += pCPoint[i].y;
}
}
};
>Is it possible to use 'friend' for 'GetSumX', and 'GetSumY'
Yes it is possible. You must add the declaration of the function on in the declaration of the class. But I don't suggest that you make them friend functions.
Always post your codes in the code tags. [[B]code=c++[/B]][[B]/code[/B]]
Make a class called Point.
Make two member function of type int called perhaps X and Y.
You will have to make a two-arg constructor that will initialize the values of these data member.
Write Two member functions accordingly which takes a array and its size and calculate the sum of abscissa and ordinates respectively.
You will have to write a member function that takes two argument and set the value of X and Y accordingly.
So, write the class and let us know where you face the problem
Well well,
Look what are doing.
You should extend the for loop till last.
put the line 14 and 19 out of the loop.
Got it?
So here is a brief algo:
Repeat 10 times
{
input a number
If it is odd, increment oddCount. If it is even increment evenCount.
}
cout the oddCount and evenCount
>I have tried that but its not working
I don't believe you. Show me the corrected code after all the correction what jenas suggested.
And please for heaven sake, don't use void main(). It kills and is injurious to health.
http://cppdb.blogspot.com/2009/02/should-i-use-void-main-or-int-main-or.html
Well, I don't suppose you should create thread like these.
BTW, I use Emacs with g++ command line on my Linux Box.
I also sometime uses Gedit( the default gnome text editor) with some plug in to make compilation easy.
>Don't stop C++ programs with exit() function (except on severy run-time errors).
I fully agree with ArkM.
exit(0) is evil
I would extend ArkM's advice that you should not use exit(0) even in case of runtime errors. You should use exception handling mechanism.
Ideally, All functions, sub routines should end back to main() and main should return something.
>can someone teach me how to use pointer in this program??i don't know if what i did in this program is correct. can you tell me how to use pointers here.thanks!
Please Be specific.
This is not a teaching classroom dear. You must read your text book and come to us with very specific questions.
We, can tell you how to use pointers but how the hell are we suppose to explain you the whole program. Do you think it is feasible?
Secondly, DO NOT USE VOID MAIN() http://cppdb.blogspot.com/2009/02/should-i-use-void-main-or-int-main-or.html
use #include<iostream>
and #include<cstdio>
Use a new compiler not a old one(http://cppdb.blogspot.com/2008/10/why-you-shouldnt-use-you-use-old-c.html)
And thirdly, The program contains lot of (trivial) errors. We are not going to debug them for you. For your curiosity, there's a error in the cin>>inputfruits()
You cannot cin to function.
So, please correct the errors and read the book. Then asks what is troubling you.
You don't write anything in the first file. You read from it.
You also read from the second file character by character and look for its encrypted counterpart in the first file. Then you write the third file accordingly.
>the text file(#1) that will encrypt the contents of text file(#2)
How is a FILE suppose to do the conversion?
1.You are using C codes rather than C++.(The only thing in which you are using C++ is Console IO)
2.Non-Standard Headers.
3.Never use void main
4.Use fstream objects rather than fopen().
5.
ch=0;
ch=ch+c;
ch=ch/2;
c=c+ch;
can be better stated as
ch=c;
ch=ch/2;
c+=ch;
6.Please specify, what is the exact problem you are getting.(like, is the code not running or what?)
7.>>that will convert the the contents
What does convert imply?
>Finally I wanna say, Use STL, Don't Reinvent the Wheel
Perfect. You said what I, and even Bjarne Stroustup and even most of elegant programmer says. But here are few points for you:
1.The Thread Starter, on Post#5 says that he cannot go that advanced.
2. You should perhaps not give away codes like this. Read the sticky thread http://www.daniweb.com/forums/thread78223.html last point
3. The code you pasted was good :)
Good, very good.
So is your problem solved?
If yes, mark the thread as solved.
I suspect this is a copy-paste since there shouldn't be no mention of numDollars in your code.
Anyways,
Look at the three lines after the comment /* swap smallest with the head of the unsorted portion */
. Just repeat these three lines for all of your arrays.
Wanna help out on what the code for that might look like?
Hmm, I guess I will, but you need to tell me what sorting algorithm are you using.
I mean, First show me how your code will sort a single array numunit. Then I shall tell you what modification you must do to make it work.
>although I think I know
Well, if although you know, better start writing the code. Its always better to write your own rather than asking someone else to write for you ;) wish you luck
Hmm,
Look: I use Linux, and I have firefox in it as my favorite browser. Every time I need to open a URL, I call $ firefox URL
So, if you are saying cross-platform, your problem is merely to open a file in some application.
What I suggest is, you can try conditional compiling in this case.
This will make your code portable to a great extend( but not complete portable).
Yeah, Nucleon is right.
What nucleon suggested is called implicit type conversion. The compiler in this case will convert the type of 2 from int to double. Hence the division performed will be on decimals(rather than integers).
To do a explicit type conversion, use the static_cast<type_name>(operand)
For eg:
int a=1,b=2;
//cast promotes the type of int a to double
double value = static_cast<double>(a)/static_cast<double>(b);
//you can even use cast on one operand like:
//value=static_cast<double>(a)/b;
cout << value;
Learn about casts. Although they are evil, but you will need them in some cases.
Say, you have 3 Arrays A[], B[], and C[], you want to sort all these according to A[].
What you should do is Apply the sort on A[] and while swapping the elements of A[], swap the corresponding elements of B[] and C[] along.
Don't with Visual C++ MFC.
Use some third-party platform independent libraries like GTK+ or wxWidgets.
In that way, your code will be more portable.
You now, don't have a logical error but just a human error. So I think there is no point of explanation. Hence, I am posting the correct code with very minnor changes. Your code was fine, but just have very minute glitches. Not to worry, it happens to everyone:
#include <iostream>
using namespace std;
int main()
{
char digit;
int character;
int select = -1;
{
cout << "What function do you want to use : " << endl;
cout << "1) Switch Statement " << endl;
cout << "2) Nested Statement" << endl << endl;
cout << "Enter: ";
cin >> select;
if (select == 1)
{
cout << " Enter the letter you want to see as a number: ";
cin >> digit;
switch (digit)
{
case '2':
cout << " A, B, C";
break;
case '3':
cout << " C, D, E";
break;
case '4':
cout << " G, H, I";
break;
case '5':
cout << " J, K ,L";
break;
case '6':
cout << " M, N, O";
break;
case '7':
cout << " P, Q, R, S";
break;
case '8':
cout << " T, U, V";
break;
case '9':
cout << " W, X, Y, Z";
break;
default:
cout << digit<< "Error! Is not a number between 2-9 ."
<< endl;
return 0;
}
}
else if (select == 2)
{
cout <<" Enter the number you want see its corresponding letters: ";
cin >> digit;
if ( digit == '2')
cout << digit …
If it is a standard header, (like cstdio, iosteram, fstream, string, cmath, cstdio etc.) #include<iostream> instead of #include<iostream.h>
But if your header is non-standard (like gtk.h, sockets.h or myHeader.h) then you should #include<gtk.h> or #include<socket.h>
If the header file is home-made, you should perhaps
#include "myHeader.h" which usually tells the compiler to look for the file in the current directory rather than the standard include directory.
>Also note that you don't need to worry about ending a string with a NULL terminator ( '\0' )
Good! But writing that extra (1 Byte) null character will give him possibility to use the whole range of <cstring> library!
But then, it is not unreasonable to omit the '\0' and write each functions(strcat(), strcmp() etc) on your own.
Both have its own pros and cons.
If you are in learning phase( or otherwise), I still go with vmanes comment and won't suggest you to use <cstring> library.
>I do not see the point of reserving memory for a string when you already know >how much space to save
First of all, your goal should be clear. Here is it:
You want to create a string class which will wrap the conventional char[] so that the length of the string can be change at run-time.
Getting me? I mean, you are basically writing a string class because C++ don't allow you to create variable length arrays. So the following statements are not valid:
int i;
std::cin>>i;
char myString[i]; //error, the length of string should be constant
To solve the above problem, you are actually designing the string class.
Now the next point to consider is, what help do the member functions Capacity and Length give.
At every point of time, the Capacity holds the size of the dynamically alloted memory(alloted with perhaps new) to the Mem, while the Length stores the actual length of the string.
These two, Capacity and Length often may have the same value.
So, there is a point of having Length and Capacity since you don't know(at the compile time) what would be the size of the char[]
Just to clarify what Lerner posted, you need to have if ( digit ==[B]'2'[/B])
instead of if ( digit == [B]2[/B] )
on lines 46,49,52 etc.
(I hope you know why? If not, ask us back)
Also, you must add a break;
after line 21 23 25 etc.
Otherwise, all the following statement will be executed. To say, if the user choose to use the select-case(by entering 1 in the main menu), and Enters 2 as the number, your program will print
A, B, C
D, E, F
G, H, I
and so on
Well, what you want to implement is called multi-threading applications.
It is OS dependent. So you may like to search a bit more about this.
>what i have learned is that we donot use ".h" in VC2008 like : i use "iostream" not >with ".h"
Most of the C-header files like (stdio.h, string.h, math.h,) are now comes with stripped .h and a c added before there name.
stdio.h --> cstdio
string.h --> cstring
math.h --> cmath
Further, there is no <stdio>. it has to be <cstdio> or <stdio.h>
ArkM is right :)