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

Nope. An array is not needed.

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

Don't write obfuscated code by defining standard C functions to have a different name. That's a very very bad coding style to do that, which might get you lower marks by your instructor.

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

It would depend on the size of the file and how the file was written. If the file is relatively small (less than 100 meg or so) and the computer has lots of free ram then reading the file into memory and searching would be the fastest.

If each record of the file contains multiple fields, such as name, age, street, city, zip etc. etc, then a fix-length binary file can be read a lot faster than a simple text file becuse an entire record can be read at one time. And if the file is already sorted by the search field then you can use binary search algorithms on it. But sorting the file before searching would probably not be very efficient unless there were multiple searches on the same field.

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

AD,
DevCpp died way back and its MinGW version is likely to be outdates.

I suppose you didn't notice that I posted that over two years ago :)

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

Not only are there two diferent sequences, but they need to be printed alternately. Printing all the letters abcd together will not solve the problem.

I suppose there are more than one way to solve the problem, the solution I wrote requires three loops, the mod operator, and the switch/case statements. I'm not going to post my solution until after the OP has solve it himself. But it is not quite as simple as first assumed.

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

This is an example of one way to use it. main() declares a vector, but makes no use of it.

#include <vector>
#include <iostream>
using std::vector;
using std::cout;

enum DataKind {unknown = 0, chartype,shorttype,inttype,longtype};

struct data
{
   DataKind kind;
   union type
   {
      char cType;
      short sType;
      int   iType;
      long  lType;
      // etc for each type you want to support
   } DataType;
   data() {kind = unknown;}
};

void filldata(data& d, char value)
{
    d.DataType.cType = value;
    d.kind = chartype;
}

void filldata(data& d, short value)
{
    d.DataType.sType = value;
    d.kind = shorttype;
}
void filldata(data& d, int value)
{
    d.DataType.iType = value;
    d.kind = inttype;
}
void filldata(data& d, long value)
{
    d.DataType.lType = value;
    d.kind = longtype;
}

int main()
{
    vector<data> items;
    data d;
    char c = 'A';
    short s = 123;
    int i = 234;
    long l = 456;
    filldata(d, c);
    filldata(d, s);
    filldata(d, i);
    filldata(d, l);

    // display the value
    switch(d.kind)
    {
    case chartype: cout << d.DataType.cType << '\n'; break;
    case shorttype: cout << d.DataType.sType << '\n'; break;
    case inttype: cout << d.DataType.iType << '\n'; break;
    }
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Show us the code you have tried. No one here is going to do your homework for you.

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

clear() does nothing to the input steam. Here is how to delete everything up to '\n' from the input stream.

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

you could use a while statement

int len = 0;
token = strtok(buffer," ");
while( token )
{
   len = strlen(token);
   token = strtok(NULL,":");
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I think the easiest was is to use a union then create a vector of those unions.
In the structure below, member "kind" is an int that tells what data type is valid -- one of the defines listed.

enum DataKind {unknown = 0, chartype,shorttype,inttype,longtype, floattype};

struct data
{
   DataKind  kind;
   union type
   {
      char cType;
      short sType;
      int   iType;
      long  lType;
      float fType;
      // etc for each type you want to support
   } DataType;
   data() {kind = unknown;}
};

int main()
{
   vecto<data> items;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>what should i do to make it Y>X work as well

Simple -- just swap them.

int sum = 0;
if( x > y)
{
   int tmp = x;
   X = Y;
   Y = TMP;
}

for(x = x + 1; x < y; x++)
{
  sun = sum + x;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is another article I just ran across you might want to read.

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

If you don't want to convert that VB program to C++ then you can find may c++ registry classes. Here's one of them.

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

You have to associate the file extension with some program. Here is how its done in VB. If there is no program that can read the file then you will have to write the program yourself.

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

Don't you people ever talk to your school consolers??? Or don't they have them any more? You might also find employment by asking at a local head hunter (employment agency).

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

use a loop.

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

Interesting. On MS-Windows I get the same results that you got on Solaris, which is correct. That book is wrong. The operating system may read into memory any amount of the file it wants to (buffering), but the file pointer only moves to the location requested in the program. There is little, or no, connection between the operating system's buffer size and the location of the file pointer.

Why the difference on RHEL, I don't know. Maybe there was some other kind of error.

initial file handle :: 0
child read :: abcdefghij
child file handle :: 11
parent file handle :: 11
parent read :: lmnopqrstu
end file handle :: 22
Press any key to continue . . .

#include<stdio.h>
#include <Windows.h>

FILE* fp = 0;
char buff[11] = {0};

DWORD WINAPI ThreadProc(void* lpParameter)
{
 printf("initial file handle :: %d\n",ftell(fp));
  fread(buff,sizeof(buff),1,fp);
  buff[10]='\0';
  printf("child read :: %s\n",buff);
  printf("child file handle :: %d\n",ftell(fp));
  return 0;
}


int main()
{
    fp = fopen("TextFile1.txt", "r");
    DWORD dwThreadID = 0;
    HANDLE hThread = CreateThread(0,0,ThreadProc,0,0,&dwThreadID);
    WaitForSingleObject(hThread,INFINITE);
  printf("parent file handle :: %d\n",ftell(fp));
  fread(buff,sizeof(buff),1,fp);
  buff[10]='\0';
  printf("parent read :: %s\n",buff);
  printf("end file handle :: %d\n",ftell(fp));

}
Jishnu commented: Thanks a lot! +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Oh Good Grief :@

memcmp()

size_t

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

File associations are in the registry under HKEY_CLASSES_ROOT. Read this article.

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

I'd like to see you change the rules on PFO like they are here so that they are consistent. That would make my life a little easier on PFO too.

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

>>invoking the compiler from the editor must not be a problem

Not at all, afterall both Code::Blocks and VC++ do it, and both of those are nothing more than editors that invoke the compilers.

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

you can also just use the pragma, located near the beginning of the *.cpp file, after the includes #pragma comment(lib,"user32.lib")

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

If all you want is an editor then use Notepad++. It has the features, and more, than you want.

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

Look at that link I gave you, scroll down the page and it will tell you what libraries you need.

You don't have to include winuser.h when you include windows.h

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

what compiler are you using? I used Visual Studio and had no problems. Using VC++ 2010 Express I created a C++/CLR Windows Forms project then added that code.

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

win32 api function GetKeyState()

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

>>for(i=0;i<sizeof(num)/2;i++)

Wong. You fail the test. sizeof(int) may or may not be 2, depending on your compiler.

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

your own interface for what?

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

price range: what currency? Can't be USD because here they are only about $500.00 or so.

RAM: 2 mg is way too small. Did you mean 2 gig?

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

neither do I but you have not posted enough code for anyone to figure it out. We need stuff like structure/class declarations and function prototypes.

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

I just discovered that you have re-written the Member Rules. I like that :) Very brief, clear, and to the point. I don't know how long its been posted, but all members, both old and new, should be encouraged to read them again.

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

why? Read the Rules. We want you to start your own thread to ask a question instead of hijacking someone else's thread.

Do not hijack old threads by posting a new question as a reply to an old one

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

Its idential to any other c++ program.

#include <fstream>
...
...
<snip>
            std::ofstream out("test.txt");
            if( out.is_open() )
            {
                out << "Hello world\n";
                out.close();
            }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Welcome to DaniWeb

>>since the technologies keep changing I always feel like a beginner!
I know that feeling well.

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

It compiled ok with vc++ 2010 express, so any problems you had might have been compiler-dependent.

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

>>i'm using windows 7 now.. how's that?

You are doing yourself a disservice because you are cheating yourself out of an education. Turbo C compiler was created over 20 years ago (1987 to be exact), long before any of the current C standards were even though of (1989). It was a great compiler in its day, but has been dead for many years now.

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

scanf() reads from stdin, fscanf() which I suggedted reads from the file. You need to learn to read a little better.

>>I don't exactly know how many columns will be there
doesn't matter. fscanf() will convert everything up to the first non-numeric character (for %f that includes the decimal point if there is one).

>>Could you show some pseducode on how to read the numbers from the file or explain scanf?
I already did that. Just read my previous post.

Many times it is educational to write and test small programs that do what you don't understand. For example, take the code I posted and put it in a C program all by itself, compile it, and run it. Of course you will have to add the little bit of code that you posted which declares the FILE* and opens the file. Now inside the while loop just replace that comment I made with a printf() statement which just dispays the value of the float variable. When you do that you will understand better how fscanf() works.

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

why not just call fscanf() to read in those numbers

float number;
while( fscanf(file_to_be_read, "%f", &number) > 0)
{
   // do something with this number, such as put it into an array of floats
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
#include<stdio.h>
#include<stdlib.h>
#include <string.h>

#define NUMBERS_PLAYED 6



enum MATCHED
{
    NONE = 0,
    THREE = 10,
    FOUR = 1000,
    FIVE = 10000,
    SIX = 1000000    
};

struct players
{
    char last[19];
    char first[19];
    int nums_played[6];
    int count;
    MATCHED prize;
};

int main (void)
{
    FILE* ifp;
   char filename[1024];
    int winners[6];
    int i, j;
    int k;
    int ticketsbought = 0;
    struct players* player = 0;
   
    //Ask user for the name of the file to read from
    printf("Please enter the name of the file with the ticket data. \n");

    //Read in the file to read from
    fgets(filename, sizeof(filename), stdin);
    if( filename[strlen(filename)-1] == '\n')
        filename[strlen(filename)-1] = '\0';

    //Open file for reading
    ifp = fopen(filename, "r");
    
    //The first line will contain a single integer n, the total number of 
    //tickets bought. Now we will read in that first line
    fscanf(ifp, "%d ", &ticketsbought); 
   
    player = (players *)malloc(ticketsbought * sizeof(struct players));
   
    
    //The first line will contain the last name of the ticket buyer, followed by 
    //a space, followed by the first name of the ticket buyer
    for (i = 0; i < ticketsbought; i++)
    {
        fscanf(ifp, "%s ", player[i].last);
        fscanf(ifp, "%s ", player[i].first);
        for (j = 0; j < NUMBERS_PLAYED; j++)
        {  
            fscanf(ifp, "%d ", &player[i].nums_played[j]);
        }           
    }
    //Close the input file
    fclose(ifp);
    
    //Ask the user for the winning combination of numbers
    printf("Please enter the winning lottery numbers:\n");
    scanf("%d %d %d %d %d %d", &winners[0], &winners[1], &winners[2], &winners[3], &winners[4], &winners[5]);
    
    for (i = 0; i < ticketsbought; i++)
    {
        player[i].count = 0;
        for (j …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I just saw that new Solved Thread message on a thread -- looks great :) If anyone misses that then they must be blind.

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

typecast it pointer = (int **)stuff;

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

Post the text file.

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

Of course the way to stop such emails is to set the appropriate flag in the PROFILE :)

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

lines 18-22 will never get executed because of the return statement on line 17.

In main() keep track of the largest value that is return by sign() function.

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

>>We get posters asking questions on threads that is still open from as far back as 2005!

Marking threads as solved will not solve that problem. Mods need to actually close the threads to fix that.

If you like such emails then that's great -- for you.

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

Whether its C or C++ depends on the file extension. *.cpp is compiled as a c++ program and *.c is compiled as a C program.

Remove the * in the structure as I previously mentioned. And it compiles without error as *.c file using Code::Blocks, which is a newest versin of the compiler used with Dev-C++.

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

something like this

void compute(int n)
{
    int iterations = 0;
    printf("n = %d -- ", n);
    while( n > 1)
    {
        if( (n%2) == 0)
        {
            n /= 2;
        }
        else
        {
            n = (3*n)+1;

        }
        printf("%d ", n);

        ++iterations;
    }
    printf("\niterations = %d\n",iterations);

}

int main()
{
    for(int n = 1; n <= 10; n++)
        compute(n);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what compiler are you using? Most compilers will consider line 58 an error and C compilers will not let you declare variables anywhere except at the beginning of a block surrounded by { and }.

line 67: That is the first place where your program crashes. Its because the players struct just decares pointers for the first and last names. Remove the * from those structure items so that the structure will contain character arrays instead of pointers.

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

put the values into two arrays so that they can be easily accessed to create the table.