Thanks you have been quite helpful :)
and plz mark the thread as solved if u feel u are answered so that others dont waste their time in a solved thread. :)
Thanks you have been quite helpful :)
and plz mark the thread as solved if u feel u are answered so that others dont waste their time in a solved thread. :)
undefined reference to `fv(double)'
its because u haven't defined the function
double fv(double);
In line #3 of your code, its just a declaration which says that there exists some function as such. Its not definition. Define your fuinction and see.
it is also strange for me: fp = fv;
because they are different kind of type
read about function pointers in detail.
How ? Can you send me any example links :)
Thanks
char block[1000];
ifstream myFile ("data.bin", ios::in | ios::binary);
myFile.read (block, 1000);
see
http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html
/*original function*/
void my_fun(int);
/*pointer to function*/
void (*funPtr)(int);
/*assigning the function to the pointer*/
funPtr = my_fun;
/*calling the function using pointer*/
(*funPtr)(3);
//OR
funPtr(3);
checkout the link
http://publications.gbdirect.co.uk/c_book/chapter5/function_pointers.html
regarding increasing efficiency:
Every time u invoke getline() method to read something from the file.
If the file have say 10,00,000 words u are executing getline() that many time. Now getline() involves file-read operation which is a time consuming task and hence it decreases the efficiency.
What u can do instead is read a block of data into a string (of say 1,000 character) and do whatever processing u need to do with the words in it and when u are finished with that block read the next block.
U will see the improvement in efficiency in this way.
start with your code. Post here if u get any issues.
1> DO PROPER INDENTATION TO YOUR CODE.
2> line #7
It should be an assignment(=) statement and not a comparison(==).
the errors are pretty clear i guess. There is nothing that seem to be complicated about the error messages.
Also, this is for another assignment but what about arpa/inet.h or sys/socket.h? Are those also only supported by unix-like systems?
Yes.
in line #11
u are declaring the method set_values() which takes two characters as parameters.
But in line #28
U are calling the same function passing a string.
Try out yourself how to solve it.
Read how to store a string and operations on it.
That seems to be it. I forgot to try and use the read function. I guess that happens after staring at the same thing for long periods of time.
struct salesRecord* data; data = new salesRecord; ifstream inFile("salesbin.bin", ios::in|ios::binary); inFile.seekg(sizeof(salesRecord)*1); // edit to get desired record inFile.read((char*)data, sizeof(salesRecord)); inFile.close(); cout << (*data).custNo << endl;
Works now. Thanks for the help.
u r welcome.............
Well there must be a way that I'm not seeing, because that get_customer function was supplied with the assignment.
u have to use fread() to read the whole structure and not an individual member. Then u can use the particular member.
ok ok.
make the following change
(refer to line #19)
if(start==FALSE && spacePrinted==FALSE)
{
putchar(' ');
spacePrinted = TRUE;
}
change the above codes to
if(start==FALSE && spacePrinted==FALSE)
{
putchar(' ');
}
spacePrinted = TRUE;
int get_customer(ifstream& inFile, int width) { int i; char c; char temp[10]; for(i=0;i<width;i++) { inFile.get(c); temp[i] = c; } return atoi(temp); }
there is another concept called Endianness.
Do google about it.
Moreover, U are doing the conversion in a wrong way.
U are reading 4 bytes and not digits.
say there is a number 12 which is stored in memory as
0x0 0x0 0x0 0xC (in hex)
or it may be in the following pattern
0x0 0x0 0xC 0x0
(this depends on the endianness, big endian or little endian[READ MORE ON IT])
what u are doing is storing these in a char array. Hence the character array becomes
{0, 0, 0, [junk]} [junk] ---> whose ascii is 12.
now when u do atoi(). U will obviously get 0.
Another important thing:
How do u know that your integer data starts from the very beginning of the file ?
U need to read more on file operation ....specially binary files.
still nothing
hi, i tried the following program in my pc and it compiled successfully.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char encryptit(int x, char c[80]);
int key = 5;
char string[81];
gets(string);
encryptit(key , string);
system("PAUSE");
return 0;
}
char encryptit(int codekey, char stringydoo[80] ) /* Encrypt Header line */
{
int i,stringnums[80];
for(i=0; i<80; i++)
{
stringnums[i] = stringydoo[i];
if (stringnums[i]>=32 && stringnums[i]<=126)
{
stringnums[i] = stringnums[i]+ codekey;
if (stringnums[i]>=126)
{
stringnums[i] = 32 + (stringnums[i]%126);
}
stringydoo[i] = stringnums[i];
}
}
puts(stringydoo);
system("PAUSE");
}
this still add one space after the first letter :S
inputkjkkk kjkj jj
output
k jkkk kjkj jj
its a bit strange
hi, i figured it out. See below
int spacePrinted=FALSE;
change the above line to
int spacePrinted=TRUE;
It will not print the space now.....................
FILE* fd; fd=fopen("salesbin.bin","wb"); fwrite((struct salesRecord*) sales, sizeof(struct salesRecord), 196, fd); fclose(fd);
are u using fread() for reading your data.
Its obvious that when u open your file using some editor u wont see the exact values u are storing. U can see the characters because a character is stored as a byte and your editor(using which u open your file) will display it in original form only. But other data will be junks only or it wont mean what u see.
following is the complete program for your purpose
#define FALSE 0
#define TRUE 1
void VymazBiele()
{
int c;
int start= TRUE;
int spacePrinted=FALSE;
while ( (c = getchar() ) != EOF )
{
if (isspace(c))
{
spacePrinted = FALSE;
while ( (c = getchar() ) != EOF && isspace(c))
{}
}
if (c != EOF)
{
if(start==FALSE && spacePrinted==FALSE)
{
putchar(' ');
spacePrinted = TRUE;
}
putchar(c);
if(c == '\n')
start = TRUE;
else
start = FALSE;
}
}
}
things like this u should try on your own.
hi there is already one library method in unistd.h called encrypt() hence it is giving the problem.
Change the function name to something else like encryptit().
It should work.
try making your declaration before main (not inside main)
check line #6:
char encrpyt(int , char);
the parameters are int and char.
In line #20
char encrypt(int codekey, char stringydoo[80] ) /* Encrypt Header line */
now the params are one int and a char [].
correct the prototype at line #6.
pthread.h: no such file
pthread.h is generally for POSIX systems like FreeBSD, NetBSD, GNU/Linux, Mac OS X and Solaris, but Microsoft Windows implementations also exist. For example, the pthreads-w32 is available and supports a subset of the Pthread API for the Windows 32-bit platform.
If you are working in windows it wont work. U have to look for
pthreads-w32 .
checkout the link
http://en.wikipedia.org/wiki/POSIX_Threads
when u include a file in <> brace like follows
#include <mutex.h>
it looks in the include directory.
But your file mutex.h is in your current directory. To include such file u have to do it using ""
#include "mutex.h"
if(number = 0){}
what do u expect it do do?
That is an assignment statement. Read how to compare numbers.....
U are doing
number = number%16;
u are getting the least significant digit by that and u are loosing the remaining number.
U should have done
digit = number%16;
number = number/16;
/*compare digit now*/
here is how u can solve your problem in short
void itox(char hex[], int num)
{
int digit;
int i=0;
whle(num>0)
{
digit = num%16;
num = num/16;
if(digit<10)
hex[i++] = digit + '0';
else
hex[i++] = digit -10 + 'A';
}
if(i==0)/*if the number was less than 1*/
hex[i++] = '0';
hex[i] = '\0';
/*reverse the string*/
reverseStr(hex);
}
void reverseStr(char str[])
{
int i, j;
char c;
for(i=0, j=strlen(str)-1; i<j; i++, j--)
{
c = str[i];
str[i] = str[j];
str[j] = c;
}
}
1) The program malfunctions weirdly if the user does not enter a number.
its because scanf() is expecting u to enter some integer numbers.
If u want it to work for any input then better take your input as string, validate it and get its' integer value to your variable.
2) Large numbers, like 1000000000, are stored as negative numbers.
it's because u are storing a number greater than the capacity and by default the int data type is signed int hence the most significant bit is the sign bit and the number u are trying to store have that bit as 1 which says its a negative number.
If u want larger numbers u can use unsigned long datatype.
Thanks a lot for all your help :)
It was really helpful :)I have marked the thread as solved :)
Thanks :)
my pleasure :)
Will
multimap<string, string>
behave as this one is doing.Thanks a lot
yep it should.
it cannot be like
mymm.insert(pair<char,int>("andrew","50 years old"));
it had to be
mymm.insert(pair<string, string>("andrew","50 years old"));
because the values u are passing are strings.
Ya I was not removing the duplicates.
Thanks for your help :)
u welcome :)
ofcource u will get duplicates because u are doing
for(itv=vec1.begin(); itv < vec1.end(); itv++){...}
and this vector contain all those character more than once.
U must skip those character which are already printed.
Dont do the following
vec1.push_back('a');
vec1.push_back('b');
vec1.push_back('b');
vec1.push_back('b');
vec1.push_back('c');
vec1.push_back('c');
vec1.push_back('d');
vec1.push_back('e');
vec1.push_back('e');
vec1.push_back('e');
vec1.push_back('e');
vec1.push_back('e');
vec1.push_back('n');
vec1.push_back('k');
vec1.push_back('k');
push a character only once
vec1.push_back('a');
vec1.push_back('b');
vec1.push_back('c');
vec1.push_back('d');
vec1.push_back('e');
vec1.push_back('n');
vec1.push_back('k');
Interesting - can anyone else confirm?
I am using:
gcc version 4.4.1 20090725 (Red Hat 4.4.1-2) (GCC)
i used
g++ (GCC) 3.3.2 20040119 (Red Hat Linux 3.3.2-8) :)
I'm still getting the same error. Can someone try these files:
http://www.rpi.edu/~doriad/Daniweb/maxflow/with
g++ Example.cpp graph.cpp
I am not sure of why u are getting the error. I am compiling it as follows and m not getting any error in my machine.
myself@king [70]g++ Example.cpp graph.cpp
myself@king [71]
I dont think so ...
The constructor is actually:Graph(int node_num_max, int edge_num_max, void (*err_function)(char *) = NULL);
which is an optional parameter, no? Hence it should be fine to call it with only 2 ints?
hey sorry for that. I skipped it.
The file graph.h is getting included twice in graph.cpp beacuse u are including it from instances.inc also.
Remove the
#include"graph.h"
statement from the file instances.inc.
I tried it in my compiler and i didn't get any error.
NB: whenever u are making some header file its good to define a macro such that
example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
/*prototypes*/
#endif
so that the same doesn't get included more than once.
GraphType *g = new GraphType(/*estimated # of nodes*/ 2, /*estimated # of edges*/ 1);
u are calling constructor
Graph<captype, tcaptype, flowtype>::Graph(int, int);
but u only have a constructor with prtotype
Graph<captype, tcaptype, flowtype>::Graph(int , int, void (*err_function)(char *));
Hence prototype mismatch. Correct it.
Ok ok .. Thanks... I will try it :)
Any other suggestions ?
Now if I want to change my multimap from <char, int>
to <string, string > any suggestions ?Thanks
yes you can do it easily. Post here if you get any issues.
One more suggestion is read about whatever u want to use. Use google. There are lot of materials available on net. :)
enjoy
Sorry any example ?
see my prev post
error: changes meaning of 'Allocator' from 'class Allocator<OctNode<NodeData, Real> >'
what is the OctNode class. If it's a template class u have to write it as so
class Octree{
public:
static Allocator<OctNode<NodeData, Real>> Allocator;
How can I reset it.. any example ?
Thanks
do it just at the beginning of your loop as follows:
for (c='a'; c<='z'; c++)
{
i=0;
k=0;
/**remaining code/
}
Does any one have any clues for me?
Dave
are u compiling it as
c++ Example.cpp graph.cpp
If not then do it like that.
u are not resetting the variables i and k to zero and due to which the condition
if(i==0)
and
if(k==0)
fails after u have completely printed one character range.
Heya,
When I run this code, it spits out the minimum value 200, when I want it to spit out the minimum value from the function.Any pointers?
#define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> // You can assume no inputs will exceed these values: #define MAX_BOXES 50 #define MAX_PARCELS 50 int UnassignedMinimum(int allocation[], int value[], int numItems, int min[], int minimum); int main(void) { int numBoxes, numItems, minimum; int allocation[10] = {1, 0, 0, 0, 4, 5, 6, 7, 8, 9}; int value[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int min[10]; int i, sum; minimum = 200; numItems = 6; UnassignedMinimum(allocation, value, numItems, min, minimum); printf("%d", minimum); return; } int UnassignedMinimum(int allocation[], int value[], int numItems, int min[], int minimum) { int i, sum; sum = 0; //This function sums the unassigned minimum and checks if it is the smallest value. If it is it stores it in the min array. for(i = 0; i < numItems; i++){ if(allocation[i] == 0){ sum = sum + value[i]; } } if(minimum > sum){ minimum = sum; for(i = 0; i < numItems; i++){ min[i] = allocation[i]; } } return minimum; }
u can also do
minimum = UnassignedMinimum(allocation, value, numItems, min, minimum);
or u can use pointer for minimum.
the same solution in a simpler way
#include<iostream>
#include<stack>
using namespace std;
bool evalPostfix(char *postfix, double &result, char *error);
bool isop(char c);
int main()
{
char post[100];
char err[100];
cout<<"Enter postfix expr:";
cin>>post;
double result;
if(evalPostfix(post, result, err))
cout<<"result="<<result<<endl;
else
cout<<err<<endl;
return 0;
}
bool evalPostfix(char *postfix, double &result, char *error)
{
stack<double> stk_post;
double val1, val2;
while(*postfix)
{
if(isdigit(*postfix))
{
stk_post.push((double)(*postfix-'0'));
}
else if(isop(*postfix))
{
if(stk_post.size()<2)
{
strcpy(error, "INVALID POSTFIX EXPR.");
return false;
}
val1 = stk_post.top();
stk_post.pop();
val2 = stk_post.top();
stk_post.pop();
switch(*postfix)
{
case '+':
stk_post.push(val2+val1);
break;
case '-':
stk_post.push(val2-val1);
break;
case '*':
stk_post.push(val2*val1);
break;
case '/':
stk_post.push(val2/val1);
break;
case '%':
stk_post.push((double)((int)val2%(int)val1));
break;
default:
strcpy(error, "Unhandled operator.");
return false;
}
}
else
{
strcpy(error, "Unexpected character.");
return false;
}
postfix++;
}
if(stk_post.size()!=1)
{
strcpy(error, "Invalid postfix Expression");
return false;
}
result = stk_post.top();
strcpy(error, "NO ERROR");
return true;
}
bool isop(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%')
return 1;
return 0;
}
------------------------------------------------------------------------
Item No. Cost Quantity Amount
------------------------------------------------------------------------
1 40.00 1 40.00
2 150.00 10 1500.00
3 2000.00 20 40000.00
4 10.00 10 100.00
----------------------------------------------------------------------
Total 41640.00
i want to print this in c++
help me..i m new in programming...
start with cout.
read on string handling.
U are declaring a char to store a string. U are using
if(acc_name=x){}
whereas your intention is to compare string. The above statement is an assignment and the value will be true if the assignment was successful.
Read how to read and compare strings.
for compilation use
c++ main.cpp roster.cpp
I'm creating an expression tree that reads in postfix notation and converts it and prints it out into infix notation. I was wondering how would I go about correctly distributing parentheses and balancing them properly.
normal expression: a+b-(c*d)
postfix: ab+cd*-
input | stack values
-----------------------------------
a | a (push a)
b | b,a (push b)
+ | (a+b) (pop twice and push (a+b))
c | c,(a+b) (push c)
d | d,c,(a+b) (push d)
* | (c*d), (a+b) (pop twice and push (c*d))
- | (a+b)-(c*d) (pop twice and push (a+b)-(c*d))
this is the final expression
It is best if code is as proper as possible when posting to show how's done.
thanks for that suggestion. I will keep those in mind.
well but i will read multiple lines, so how to make this code right ?
set the start flag to TRUE when u get a newline ('\n') character.
I simply added another implementation of accept, which could take take a pointer and now it works.
void Element::Accept(Visitor& v) { v.VisitElement(*this); } void Element::Accept(Visitor* v) { v->VisitElement(*this); }
wel done. Its because a base class pointer can point to a derived class object also.
i triend the program in linux.
May be the reason for getting different values (171, etc.) is depending upon the compiler the boll varibales have default value initiated if not initialised explicitly.