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

>>I've already got two main
Programs can only have one main(). Delete one of the two you have.

Create another function and move the code you posted in main into that function. Then change main to just call that function. The code I posted is just an example of what your program should look like when you are done. foo() is just a generic name that means anything -- don't use it in your program but name it something else.

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

Here are just a few suggestions:

line 46: >> gets(str);
Never ever use gets(). What will happen if I enter more characters than the buffer can hold? Answer: Seg Fault. Use fgets() instead because it will limit the input to the max length you want.

line 49: has no value because the string is already NULL terminated.

line 64: Delete that goto statement -- there are a lot of better ways to do that. goto is considered very bad programming practice.

lines 44 and 69: delete the typecase. C language does not require a typecase for functions such as malloc() which return void *.

line 84: There is no need to allocate stack on every iteration of that loop and may fragment memory! That's just grossly inefficient. Allocate it once before the loop starts then free it once after the loop ends.

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

I have no idea how to do that.

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

Hi again!

Im not so good at this, I tried to transform the peudo code to regular c++ code and came up with this.


Can someone please help me with this :/

/adam

You already have all the code you need -- just put it in a function, return the result.

int foo(int x)
{
   // put code here
}

int main()
{
   int x = foo(5);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what "catch" ? There are lots of them on the computer. You can't get access to most of them because they are part of the os.

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

as I remember unix files uses \r\n and dos only uses \n.
so that's why you get an difference.

Your memory is a little fuzzy -- its just the opposite.
MS-Windows/MS-DOS: \r\n
*nix: \n
MAC: \r

You will get a extra single character or two depending on you're
computer platform or the operating system.

I don't get an extra character on MS-Windows. getline() does not append the line terminating character under that os. It might with *nix, I don't know.

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

Yes, Clinton, that is one way to do it -- the hard and grossly inefficient way.

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

Read the file sequentially from beginning to end, counting lines as you go along. If you want to read the 2d line then you will have to read lines 1 and 2. It is not possible to skip to a specific line in a text file.

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

I already posted example (not code). Just create a loop and keep running counter of current sum.

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

Must mean integers, because there are an infinite quantity of floats or doubles between two numbers. The word amount is ambiguous. Does it mean sum or quantity ? For example, there are 11 numbers betwen 0 and 10, but the sum is 1+2+3...+10 = 55

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

>>Thanks in advance !

Dump vc++ 6.0 -- its old, outdated and does not support c++ very well.

Another suggestion: Go to www.winpcap.org, click on their Support link and join their mailing list. Get answers to your questions from the people who should know.

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

Read both files at the same time as firstPerson mentioned, then compare the lines as they are read; no need to put them in a vector unless you intend to do something else with them.

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

What compiler do you use? The problem most likely is caused by not adding something at the end of the program that makes it wait for keyboard input so that you can see what's on the screen. Add cin.get(); just before the return statement.

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

My error, I had the parameters backwards. This will fix it getline(in, line1);

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

Forget Turbo C -- its too old. Code::Blocks might work since it used MinGW which is the same compiler that's probably used on *nix, only the compiler was ported to MS-Windows, and it supports many of the *nix standard libraries.

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

Post the entire program, not just little pieces.

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

Start here:

#include <stdio.h>

int main()
{
   // your code goes here
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

call malloc() to allocate memory before line 67.

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

You failed to include <string> header file.

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

Your head is wrapped around it wrong. I don't know how you got 19 mpg -- 160.9/3.27 is 49, not 19.

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

1) use ifstream, not fstream, to read a file. ifstream is easier to use.

2) use getline() to read an entire line that may or may not contain spaces.

ifstream in("test.txt);
getline(line1, in);
// do the same for the other lines
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Of course you can't really delete an element in an array, just shift all remaining array elements up to write over the element you want deleted and leave an empty element at the end of the array. If you have an array with 5 elements and you want to delete the 2d one, then move 3 to 2, 4 to 3, and 5 to 4, leaving 5th element unused.

Another way to do it without all that shifting is to just replace the current value of the element with some special number that indicates it is deleted. Then when doing anything with the array just skip over the element that is marked for deletion.

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

You can't just simply add gui to a console app. Start out by creating an MFC application (not a console app that supports MFC, but a regular MFC program, then choose either dialog (probably what you want) MDI, or SDI. After the gui app is generated, compile and run it to make sure it's correct.

Next add whatever controls you want to the dialog box. Finally start adding the code you had for the console app, leaving out main(). You will most likely have to do a lot of cut and past to get the gui program to work with the code in the console app.

BTW: You can't use precompiled headers with C files (those with *.c extension). If you have any of those then turn off precompiled header option for those, but leave it on for the other *.cpp files.

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

string resides in std namespace and you have to tell the compiler what namespace it's in. You have several options (use only one of the options listed below)

  1. using namespace std; put that after the include files (least desireable option)
  2. using std::string; put that after the includes
  3. int extract_d(std::string s){ specify the namespace every time you declare an object of type string.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Where is the code you have written to solve that problem???

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

line 71 contains a syntax error. See if you can find it.

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

add using std::string after the include files

Move the code gards (lines 4 and 5) up before the include files.

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

Post the code that shows the error.

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

I wrote != (not equal to), which is the opposite of == (equal to).

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

You mean like this?

char c;
while( c != '\n' )
{
    // blabla
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You must be using old version of visual studio. The current version does not use pointers, but the ^ operator

List<String^>^ List1 = gnew List<String^>;
// etc

Upgrade to either 2008 or 2010 so that you can use newest version of C++/CLR language.

As for your question, probably do it like you would anything else. Iterate through the list to find duplicates and delete them when found. You would need two loops for that.

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

line 105: >> else if (j = 51)

You are using the wrong operator. Use boolean == instead of assignment = operator.

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

In getInput() the first thing you need to do is check if the file was opened. Next, don't use eof() because it doesn't work the way you think it does.

int getInput(studentType studentData[30], ifstream& indata, ifstream& infile)
{
	infile.open("data.txt");
    if( infile.is_open() )
    {
            index = 0;
            read = 0; 
	    while(indata >> studentData[index].idno >> studentData[index].test[0] >> studentData[index].test[1] >> studentData[index].a[0] >> studentData[index].a[1] >> studentData[index].a[2] >> studentData[index].a[3] >> studentData[index].a[4] >> studentData[index].a[5] >> studentData[index].a[6])
	    {
		    process(studentData);
		    read += 1;
		    index += 1;
	    }
    }
	return read;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is another way

if( file.is_open() )
{
    int n, x;
    float f;
    file >> n >> x; // get first line
    setM(n);
    setX(x);
    MatrixHandler data(n, n);
    while( file >> n >> x >> f ) // process remaining lines
    {

        // fill in the matrix here
    }
    

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

Yes because MFC doesn't know a thing about std::string. CString is the data type used by MFC to transfer strings from member variables to/from the MFC controls, such as edit box. There is a lot of code in the CView derived class that will be broken if you just simply replace CString with std::string. Until you learn how MFC works don't change a thing. Yes the compiler produces lots of code, and the MFC learning curve is about one year.

Carrots commented: Thanks buddy! :) +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Strange -- if I change options to "Compile as C code" then I get a several errors on line 4 of the original post. But no errors or warnings are produced if I change back to the default "Compile as C++ code", even though the filename has *.c extension. And I have the warning level set to 4 in both cases.

error C2093: 'p' : cannot be initialized using address of automatic variable

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
kvprajapati commented: N/A +6
William Hemsworth commented: 6505 down votes recieved? huh? :P -1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I did not get an error using vc++ 2008 express, compiled for both C and C++ and was ok. So if you have an error then either it is your compiler or you didn't post something.

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

The value could be anything -- I got 23 with vc++ 2008 express and 3346328 with Code::Blocks (MinGW). It all depends on what was in that memory location.

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

No.

Proof:

int main()
{
    int* n = new int;
    cout << *n << "\n";
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

To get a random number between 1 and 6: int num = 1+ (rand() % 6) . The rest of that function you should be able to do yourself.

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

but i need it in C although i am a beginner....

There are only two ways to get it like that:

  1. Pay someone to write it for you. That will cost you some $$$
  2. Learn to write it yourself.

I have not the time to write it for you.

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

File compression/decompression is a pretty complex task. Here are some free libraries.

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

hey dragon.
i want a program by which you can set and adjust the time...
consider your watch...it has two buttoms and now you want to set the hour, min, sec...
i want a program like this...
i worked alot on a for loop but nothing happens...
pls give me complete information as i am a beginner...
you should use one or two buttoms to set the time...for example when u set the hour, it should go to min, and after you set min, it should go to second...
obviously hour should be 23 by maximum and again after 23 it should go to zero...(or it can be in am-pm)
minute, second should be 59 by maximum and again after 59 it should go to zero....

Writing such a program is no trivel matter. If you are a beginner in C language then you have no business attempting that. Instead, learn the basics of the language first, then once you are comfortable with it you can branch out into gui programs, such as win32 api, xwindows, wxWidgets, etc. I would even forget going that program in C language and use C# or C++/CLR (Windows Forms) instead. C language is not a good choice anymore for gui programming.

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

Not surprised by your problem because the code you posted won't compile. You can't run a problem that is just full of crap.

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

You can not simply insert stuff into existing lines of a text file. Instead, you have to completely rewrite the file. Open the original file for reading, open a new temp file for writing. In a loop, read each line of the original file and rewrite it with desired changes to the output file. After that is done, close both files, delete the original, and rename the temp to be the same name as the original.

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

>>Should have come inside the constructor of this class ryt?.

No. static objects must also be declared globally just like any other global object. Those lines were stated correctly in the original thread of 3 years ago.

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

you could have just used stringstream to split the line into words without re-reading the file. And it would have been more useful to use vector to hold the lines/words.

[edit]I don't see any reason at all for reading the file one character at a time. Too much work just to extract individual words.

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

C++/CLR (Forms) is just another way to write windows gui programs. The original method, using win32 api functions, is still useful. Here's a common beginner's tutorial.

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

It's pretty simple, just create an infinite loop. Inside the loop get current time ( time() in time.h ), convert to tm structure ( localtime() ), then just print the hour, minute, and second).

How to set the computer's clock will depend on the operating system.