Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You can use resize() when you originally create the vector so that it will contain room for using [] to insert items

vector<int> ay;
ay.resize(10);
for(int i = 0; i < 10; i++)
   ay[i] = i;

or you can use an iterator

int main()
{
    int count = 0;
    std::vector<int> ay;
    ay.resize(10);
    std::vector<int>::iterator it;
    for(it = ay.begin(); it != ay.end(); it++)
        *it = count++;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I compiled your program with vc++ 2010 Express and, after making appropriate corrections, it worked ok for me. Here is my version of the program. If you are using Turbo C++ you will have to delete my version of clrscr()

#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
#pragma warning(disable: 4996)

using std::cin;
using std::cout;
using std::ifstream;
using std::iostream;
using std::ofstream;
using std::ios;
class stu
{
int rollno;
char name[20];
char grade;
float marks;
public:
void getdata(void)
{
cout<<"enter the roll no\n";
cin>>rollno;
cout<<"enter the name\n";
cin>>name;
cout<<"enter the marks\n";
cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
void putdata(void)
{
cout<<"RollNo\n"<<rollno;
cout<<"\n Name\n"<<name;
cout<<"\n Marks\n"<<marks;
cout<<"\n Grade\n"<<grade;
}
}s1;


void clrscr()
{
    system("cls");
}

int main()
{
clrscr();
ifstream filin;
ofstream filout;
filout.open("c:\\test1.dat", ios::binary);
s1.getdata();
filout.write((char*)(&s1),sizeof(stu));
filout.close();
filin.open("c:\\test1.dat",ios::binary);
filin.read((char*)(&s1),sizeof(stu));
s1.putdata();
filin.close();
getch();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>filin.open("c:\\test1.dat",ios::out | ios::binary);

use ios::in instead of ios::out. For ifstream you don't have to specify ios::in -- just ios::binary will do.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Sure -- if you know assembly language you can access the math coprocessor, but there's not much point in doing that because that'sa what C/C++ does anyway.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

filin was never opened. I always find it easier to use ifstream instead of fstream for input only strweams.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

None that I'm aware of.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

No answers? I'm not supprised. You have consistently failed to take anyone's advice. So why should be help you any more you just ignore our advice.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In this case memset() is a lot easier and faster than loops. That can't be used if you want to initialize with some othe value.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Ok -- the code I posted will work with that file. This is the test I ran on it

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

int main()
{

    string line = "123 d45.10 d50.45 d198.56 w45.67";
    char action;
    float value;
    int acctno;
    stringstream s;
    s << line;
    s >> acctno;
    while( s >> action >> value)
    {
        cout << action << " " << value << '\n';
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what I would do is this

class MyBasic
{
public:
   friend ostream& operator<<(ostream& out, const MyBasic& mb);
   friend istream& operator>>(istream& out, const MyBasic& mb);
// other class stuff here
}

Then in the *.cpp file just create two overloaded functions that do not contain the friend keyword.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Please copy and paste the actual contents of the first few entries in the transction file so we can see what it really looks like. If there is a space between d or w and the value then reading the file should be easy and straight forward

char action;
float value;
int accountno;
string line;
while( getline(in,line) )
{
   stringstream s;
   s << line;
   s >> accountno;
   while(s >> action >> value)
   {
     switch(action)
     {
       case 'd': // deposit
         break;
       case 'w': // withdrawal
         break;
     }
   }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The numbers are not rounded -- excess digits that won't fit in the size of the float or double are just dropped (truncated). Thus if the actual value is 1.236 but only three digits fit in the variable than the variable's value will be 1.23.

Here is a more thorough and accurate description of the problem.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>On a side note, what do you think about the Visual C++ Express compiler.

Best one on the market for MS-Windows operating system, although more difficult to learn than Code::Blocks because if it's many whistles and bells.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Another way to do it is to allocate one large block of memory that is x*y*z*sizeof(int) bytes, then calculate the index into it yourself. Freeing up the array is very easy -- since there is only one pointer there will be only one call to free().

int* allocMatrix(int x,int y,int z)
{
    int* ay = (int *)malloc(x*y*z*sizeof(int));
    memset(ay,0,x*y*z*sizeof(int));
    return ay;
}

void disp(int* ay,int x,int y,int z)
{
    int i,j,k;
    for(i = 0; i < x; i++)
    {
        for(j = 0; j < y; j++)
        {
            for(k = 0; k < z; k++)
            {
                int spot = (i*y*z)+(j*z)+k;
                printf("ay[%d][%d][%d] = %d\n",i,j,k,ay[spot]);
            }
        }
    }

}

int main()
{
    int i,j,k,m;
    int x = 5;
    int y = 3;
    int z = 4;
    int* ay = allocMatrix(x,y,z);
    int spot = 0;

    for(i = m = 0; i < x; i++)
    {
        for(j = 0; j < y; j++)
        {
            for(k = 0; k < z; k++)
            {
                int spot = (i*y*z)+(j*z)+k;
                ay[spot] = m++;
            }
        }
    }
    disp(ay,x,y,z);
    free(ay);
}
MarounMaroun commented: Helpful! Thanks Ancient.. always here to help. +1
mitrmkar commented: Effective +6
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>void Free(array *free)
Change the variable name from free to something else because free is a C standard function name, and the compiler will get confused.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

using '@' as a deliminator in getline() is not what you want to do anyway. To make a word counter use >> operator, not getline()

string input;
int word_count = 0;
while( cin >> input)
{
   if( input == "@@@" }\
     break;
   word_count++;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Oh -- see any old ascii chart and it will tell you the difference. A '0' has a decimal vlue of 48 while 'o' has decimal value of 111. 0 is used for numbers like 0,1,2, ...9. While 'o' is an alphabetic character as in "Hello". Don't confuse or mix the two.

Of course sometimes some people mix them just for grins -- like writing Windoz instead of Windows.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What does the subject of pointers have to do with the subject of data structures? Other than a structure can contain pointers.

Here is an interesting article about pointers written by DaWei

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Huh? You asked "u r cool.if u could find it... " and that's the answer I gave you. One of those google links is where to download VC++ 2010 Express.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Not sure what you are asking. Maybe you should post an example of what you want.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Don't you know how to use google

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

How do you think 4/2 is represented in memory? If you use doubles it will be 2.0000... Similarily 4/3 is 1.3333... in memory. Its NOT just a string -- that is the binary representation.

If you want to know how doubles are formatted in memory then you need to study iEEE standards, which is what most PC computers with Intell (or compatible) processors use.

When working with floats and doubles there is no such thing as exact value. Attempting to test for equality will fail in most cases.

jonsca commented: yep +5
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There is no such thing as Visual C++ 2o1o -- you probably mean Visual C++ 2010. If you want to program then you must know the difference between 'o' and '0' :)

Use VC++ 2010 -- its newer and supports current c++ standards better. The last time I used Borland Builder, several years ago, it looked like a cross between c++ and VB.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
struct matrix* InitMatrix(int x, int y, int z)
{
   struct matrix* m = malloc(sizeof(struct matrix));
   // initialize the structures elements

   <snip>
   return m;
}

Free it in reverse orderof allocating it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In this code it could be either one -- just a matter of programmer preference. Use which ever one you want because the outcome will be the same. I like to use ++i because with some compilers the code might be just a couple nanoseconds faster than i++.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 37: >> void pos_num(ifstream input, ofstream output)

streams have to be passed by reference, not by value as you have done.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>but I dont how to say to the assembler to make the number of that byte unsigned...
You don't. This is not C or C++ languages. In assembler you can not declare a data item to be either signed or unsigned like you do in C/C++. Any byte can be treated as either one.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

For strncpy() all you have to do is this: You don't have to clear the dest buffer because its just a waste of time and CPU cycles. Also you have to check on the end of src string, which may or may not be longer than max.

int i;
for(i = 0; i < max-1 && src[i] != '\0'; i++)
{
   dest[i] = src[i];
}
dest[i] = '\0'; // NULL terminate the dest buffer
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Whether a byte is signed or unsigned, depends on how you want to treat it. Maybe you want to study this thread. Whether the value of a byte in memory is teated as signed or unsigned all depends on the instruction you want to use. The value of the byte actually stored in memory can be considered sign-less until the program wants to use it for something.

>>is this correct?
Did you try to assemble it? If not, then you should and then you can answer your own question.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Welcome to DaniWeb -- glad to have you here. Almost never to old to get back to school and learn new skills or trades.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> wanted to edit and correct the post but can't figure out how.

You only have 30 minutes to make changes. After that you are just SOL ("Sadly Outta Luck" (polite form) for anyone who doesn't know that that means).

Your code snippet is in error. char a; should be char* a;

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You need to post most recent code. If you want to just check for a single character than if( argv[1][0] == 'n')

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There is a reverse() method in std <algorithm> header file

#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
    string name;
    cout << "Enter your name\n";
    getline(cin,name);
    reverse(name.begin(),name.end());
    cout << name << '\n';
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
const char * my_strchr(const char * s, char c)
{
    
    while(*s != '\0')
    {
        if(*s == c) 
            return s;
        ++s;
    }
    return '\0';
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

lines 21 and 22. open() method takes const char*, not std::string. You have to use string's c_str() to make the conversion. input.open(inf.c_str());

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

delete the loop on lines 4-7 because the dest buffer may contain some text that the program needs to keep. That loop destroys all that text.

The first thing the function needs to do is find the end of the text, if any, that is in the dest buffer.

while( *dest != '\0' )
{
   ++dest;
   --max;  
}

After that, it can copy the source to dest. The loop starting on line 8 needs more work too. Its too complicated. You should take no more than three lines of code for that loop. Hint: variable b is not needed.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 10. The break is unnecessary because of the return statement on the previous line.

line 9: wrong. You want return &s[i]; line 19: That will most likely display 0 if the function every returns NULL, which may not be what you intend.


Otherwise your program looks good :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>why if I change AllocString(strlen(s), &copy); to AllocString(strlen(s)-7, &copy); I can still get a good result?


AllocString() doesn't actually copy anything into the newly allocate memory but just returns a pointer to it. If you allocate fewer bytes than you try to copy into that memory the outcome will be unpredictable. malloc() will normally allocate more space than what you requested because from a memory management point of view it's just easier and quicket to do that. It will most likely round the amount of memroy up to the nearest 8 or 16 bytes, which guarentees proper alignment for numeric data. But your program can not, or should not, count on that because that behavior can change at any time. For example Microsoft C compilers will add more memory when compiled for debug than for release modes. That's because the compiler might add more code to the program to do runtime buffer overflow checking. Other compilers may or may not act similarily.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 22 destroys the value of all the grades. Delete that line

line 24: only grade[32] is used in that switch statement. And grade[32] is wrong because it accesses an element of grade array beyond the bounds of the array. In orderwords, there is no grade[32] element. Elements are numbered 0 to 31.

What I think you want there is switch( grade[i] )

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There are hundreds, maybe even thousands, of programs here that use them.

Here are some tutorials you might want to study.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

delete line 7 and move the return between lines 9 and 10. That else statement on line 7 is causing the function to return after the virst attempt to chedk the value of *s.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>char *post = "request=register&uniqueid=" + uniqueid;
You can not concantinate C style character arrays that way. It has nothing to do with libcurl -- just plain old C syntax error

char post[255];
strcpy(post,"request=register&uniqueid=");
strcat(post,uniqueid);

Of course you could use std::string to make it a little easier, and safer

std::string uniqueid = "whatever";
std::string post = "request=register&uniqueid=";
post += uniqueid;

In the libcurl function parameters which require const char* you can use post.c_str().

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You forgot to include <string> header file.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You have to have admin privledges to do what you want to do. It's a security thing. Security would be severly breached if just anyone could view passwords.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Because line 57 is assigning a pointer to one of the array elements. Can't do that unless child an array of pointers.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

single int array[10]; multidimensional: int array[10][3]; This is more like a spreadsheet where it has 10 rows and 3 columns per row.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>strcpy(array1[n],array);

Just use simple assignment array1[n] = array[i];

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 24: using an uninitialized pointer ptr You have to allocate memory for it before it can be used.

line 57: child is NOT an array of pointers to structures, but an array of structures. There is a difference. In the structure you need to declare child like this: struct node** child; Notice it has two stars, not one. Then in create() method, allocate the array of pointers like this: child = new struct node*[size];

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is an article about how to create managed threads in the current version of CLR. When I posted my example earlier today I didn't realize that the OP was using CLR.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is a simple example. The only reason it includes windows.h is because it uses the win32 api function Sleep() It will become more complicated if each of the threads must access the same global variable or common function. In that cast the threads have to be synchronized to avoid clashes while reading or writing at the same time.

#include <process.h>    /* _beginthread, _endthread */
#include <Windows.h>
#include <iostream>
using std::cout;

void MyThread( void *dummy );


int main()
{
    for(int i = 0; i < 5; i++)
    {
        _beginthread( MyThread, 0, (void *) (&i)  );

        /* Wait one second between loops. */
        Sleep( 1000L );
    }
}

void MyThread( void *param )
{
    int i = *(int *)param;
    cout << "Hello World from " << i << '\n';
}
cwarn23 commented: Great code in speedy time :=]] +6