else if(operator=='-') { int result = a + b; }
Dear arshad take a look at your code:
else if(operator=='-')
{
int result = a [B]+[/B] b;
}
that has to be
else if(operator=='-')
{
int result = a [B]-[/B] b;
}
else if(operator=='-') { int result = a + b; }
Dear arshad take a look at your code:
else if(operator=='-')
{
int result = a [B]+[/B] b;
}
that has to be
else if(operator=='-')
{
int result = a [B]-[/B] b;
}
or use if statements to solve the variable like...
if(operator=='+') { int result = a + b; } else if(operator=='-') { int result = a + b; }
Actually I see a better way: use the switch
statement for that purpose (As I already mentioned in one of my previous posts)
switch(sign) { case '+': /* Your code for addition here */ break; case '-': /* Your code for subtraction here */ break; case '*': /* Your code for multiplication here */ break; case '/': /* Your code for division here */ break; default: cout << "Unknown operator\n"; }
> A simple voice chat program is just a program which sends sound over a network from one side to another side so I guess you'll probably need at least two things:
>>> A network library
>>> A library which deals with sound
To release the allocated memory (of your featlist
):
for(int i = 0; i < len; i++)
delete[] ptr[i];
So, no more memory leaks :P
> The function cIndex
isn't always returning a value:
int cIndex(char *x,char **featlist,int len)
{
int i;
for(i=0;i<len;i++)
if(strcmp(x,featlist[i])==0)
return i;
/* So what happens when featlist[i] and x aren't equal ? */
/* You should add a return code to check whether they were equal or not */
/* Let's say if they weren't equal you return -1 for example */
}
> Your code compiled and ran fine for me (what OS and compiler are you using?)
> Running your code gave me the following output: -7;-7;
, is that correct? (I'm using the same file contents for featFile and trainFile as you provided)
> I can't seem to find any place in your code where you're deallocating the assigned memory to featlist
, that's a serious memory leak :) ...
Sweet, but sadly im running Linux so i dont think i can view the XPS files.
If you scroll the page a bit down you'll see there's also a PDF version of that ebook available :) ...
> Try ofstream fileOut(P.c_str());
instead of ofstream fileOut(P);
:) ...
> ofstream requires the filename to be passed as a const char*
, the c_str()
method from the string class will convert the string which contains the filename to a const char*
and return it (the string itself remains intact) ...
Instead of cin>>name
you could use getline(cin, name);
to get the first name and the surname :) ...
C++ Beginners Guide (from Herb Schildt), you can get a free ebook copy at Microsoft's site: http://msdn.microsoft.com/en-us/beginner/cc305129.aspx :) ...
> The Complete Reference: C++ from Herb Schildt (1)
> C++ Black Book from Steven Holzner (2)
I'm not good in reviews, but I'll try to give some comments :):
(1) Herb explains everything very clearly and in an understandable way + very detailed explanations, very good examples used in the book and it covers the whole C++ syntax :) ...
(2) This book is also a good one but sometimes it becomes difficult to understand and are there too few examples to demonstrate such an advanced topic :) but to learn to basics it's just a great book, it also covers the whole C++ syntax in detail and might be especially very useful as a reference :) ...
(Both books are written by professional C++ programmers)
> Edit:: C++ Beginner's Guide is also a very nice one (you can get a free legal ebook copy here)
It's not Hensworth but Hemsworth :) ...
Implicitly you can say that it's possible using the work-around Ancient Dragon provided you with :) ...
I don't get that, do you mean the following: Is it possible to call a certain function using two different names ?
e.g:
int a = 5, b = 6;
//makeSum and addition do exactly the same
int c = makeSum(a,b);
int d = addition(a,b);
Do you mean something like this?
> If you're using .NET framework: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.hide.aspx
> This may be more useful to you :) ...
:D I did change it though...
Change
if ( !inData )
{
cout << "Error in opening file" << endl;
}
to
if ( !inData )
{
cout << "Error in opening file" << endl;
return -1;
}
Oh ... I meant that the program runs but displays the error message, "Error in opening file." Howcome? I got it to work, moving it to main.
I've already explained that in one of my previous posts :) ...
Because one reads and one writes? Hahahaha
Of course :), but I asked why you SEPERATED these from your function, why not implement them directly in your function ?
EDIT: It's still giving an error message, even with braces... What now?
You must have done something wrong as your program compiles and runs fine with me :) ...
Why are you using two separate functions read
and write
then ?
> int findRows( string line )
: Why are you passing a string as argument, you can also declare a variable inside your function instead ...
> return 0;
: I would return -1, as a file can also contain 0 lines :) ...
>There's some unreachable code in your function:
if ( !inData )
cout << "Error in opening file" << endl;;
return 0; // this doesn't belong to the if statement (it will always execute)
change it to
if ( !inData )
{
cout << "Error in opening file" << endl;;
return 0;
}
(if you want to execute more than one function when an expression in an if-statement is true, you should always use a compound statement ( {}
) ...
Your program should work now :) ...
Did you try searching google (e.g: typeid
, c++ rtti
)?
And what's the result ?
Did it work ?
*Deleting a pointer twice is an udefined behaviour too.
Now is just the question left: To prevent deleting a pointer twice: how do we know a pointer has already been deleted :P ?
(I would solve that by implementing a new pointer type, but what's your vision on this?)
I think you'll need RunTime Type Identification (RTTI) for that purpose ...
tux4life - does this pass the grammar check ;)
I don't know as I'm not a "native speaker" :P ...
and you are reading the '+' sign in character form yo cannot perform any operations on it!.
And what's this?
char sign = '+';
int n1 = 5, n2 = 7, result;
if(sign == '+') result = n1 + n2; // will compile :)
Comparing a variable to another one, isn't that an operation?
BTW, Reading the expression as a whole line isn't very useful here as the expressions are always like a+b
, a-b
, a*b
, a/d
...
To the OP:
> When performing the division it might be useful to check first whether the second operand is a zero ...
> If you're reading the line which contains the expression using getline
, it might be useful to create a simple SkipSpaces function which skips over the spaces, so you can perform operations like this: a *b
or a -__________b
(where '_' is a space) or ... instead of only a+b
, etc ...
another way to solve this problem is to read the intergers in integers!
That's actually what he was doing right now :) ...
Yeah, siddhantes is right (look at the Bjarne Stroustrup's page) !
It isn't because it seems to work it's actually working (that's just C++) !
I hope the OP does get it, but I'm afraid I don't.
Didn't you mean: but I'm afraid he doesn't
?
In this bold part you say the next read value will be interpreted as an integer but where is the next read value, isnt there only one read value in this code (sentence) ?
Thanks a lot!
sentence
is the string where the sscanf
function reads from, in the string "Rudolph is 12 years old" you've 4 words and 1 number, but for the sscanf
function these are just 5 values:
1) Rudolph
2) is
3) 12
4) years
5) old
Edit:: "%s %*s %d"
says that the first value is a string (%s), the second value is also a string (but sscanf has to ignore this one) and the third value is a number (an integer (%d)))
Edit:: sscanf (sentence,"%s %*s %d", str
, &i
); the higlighted parts tell the sscanf
function where to store the read and non-ignored parts of the string sentence
...
can you tell me why I would ignore the read value with this " %*s"? I understood other parts, thank you very much...
That's just the function's syntax: the '*'-sign indicates that the function has to ignore the following value (not you :P) ...
Edit:: You can find more information on this in the table on this page ...
>> num1 sign num2 = answer;
is not a valid C++ expression
>> e.g.: (this code will never work)
char sign = '+';
int n1 = 5, n2 = 7, result;
result = n1 sign n2; // won't compile
>> This code is the correct way to do this:
char sign = '+';
int n1 = 5, n2 = 7, result;
if(sign == '+') result = n1 + n2; // will compile :)
You don't have to use cin.get
for that purpose, just do something like this: infile >> [I]yourvariable[/I];
:) ...
Edit:: num1 sign num2 = answer;
will never work (even if you had typed num1 + num2 = answer;
it won't work) ...
You'll have to use something like a switch statement to parse the expression:
switch(sign)
{
case '+':
/* Your code for addition here */
break;
case '-':
/* Your code for subtraction here */
break;
case '*':
/* Your code for multiplication here */
break;
case '/':
/* Your code for division here */
break;
default:
cout << "Unknown operator\n";
}
Yeah, if you really want a feature like that you might have to implement it yourself: Create a new pointer datatype and overload the delete operator for it in such a way that the pointer is always set to NULL after releasing the allocated memory :) ...
what do "i" variable and str[20] array do in this function?
Edit:: > The i and str[20] variables are declared to store the result of the sscanf function
> As already mentioned (or not) sscanf reads from a buffer (the str[20] string/array)
> sscanf is used to read "things" from a string and put them in variables ...
> I assume you aren't understanding the following line: sscanf (sentence,"%s %*s %d",str,&i);
>> sentence
is the string where the sscanf
function reads from, after the first comma you see the following: "%s %*s %d"
where %s
tells the sscanf function that the following part it will read will be a string, %*s
means that the function will ignore the read value and %d
tells the function the next read "thing" should be interpreted as an integer ...
>>After "%s %*s %d"
you encounter the following: ,str,&i
which tells the function where to put the read values (the first read value will be put in a string called str, the second read value will be ignored (note the asterisk *), and the third read value will be stored in an integer variable ...
>>> Hope this helps :) !
After deleting you could simply set your pointer manually to NULL:
int *ptr = new int;
delete ptr;
ptr = 0;
By the way, why should delete
reset the pointer to NULL
, C++ is a programmer's language and those programmers want to control every single aspect of the language, this brings with it that you'll have to do some things manually, but in this case, it's only one single line of code extra :) ...
Take a look at:
> http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/
or
> http://www.cppreference.com/wiki/c/io/scanf (sscanf is just like scanf, with the difference that the input is read from the buffer) ...
The program does not interact with the internet.
But i'm having problems, with reading from the csv file, put the line's into a vector and searching and combining username and password.
Does the account database have to be in a CSV-file ?
Edt:: Take a look at: http://www.dreamincode.net/code/snippet1316.htm
Can you please provide me with some more information about what your program actually does ?
According to your code you want to convert a value (integer) to a hexadecimal number, why don't you just declare value as a string and use stringstreams to convert the number to a hexadecimal number like here: http://www.daniweb.com/code/snippet1162.html :) ...
I wanna work with some 40 digit numbers here. (I know I'm using int up there)
Try the apfloat C++ library: http://www.apfloat.org/apfloat/ ...
Does anyone have a sample code, for User Authentication system with the basic functions, login/registration?
You could simply write something like that yourself, but as you're mentioning 'registration' I've to following question: has to program to interact with the internet to register, or is registering not explicitly required ?
Maybe you should read a C tutorial first, it's only a suggestion, but I think it will help you a lot :) ...
Strange the following is working: final += hex_string;
As far as I know hex_string
is a pointer and final
is a string, but I don't know whether the + operator
is overloaded for data of type char*
...