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

The code you posted won't (and can't) work. You need to put buttons on another window -- normally on dialog box but they can also be put on standard windows or toolbars. Here is a thread that may help you.

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

There is no standard way to accomplish that. But there are at least a couple non-standard ways. (1) use kbhit() from conio.h -- but the problem here is that conio.h is non-standard and may not be supported by your compiler so we normally discourage its use. (2) create another thread for keyboard input so that the primary thread can do something while waiting for input.

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

Here is what I used. What you posted isn't a complete program -- this one is.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    char str[500] =  {0};


    char error[]="Windows IP Configuration"; //error string to find
	system("ipconfig>output.txt");
    fstream file("output.txt",ios::in);
	
    int i=0,l=1,counter=0;
	
    while( file.getline(str,500) )
    {
        cout<<"\nLine #"<<l<<" (length:" << strlen(str) << "):\n\n";

        while(str[i]==error[i])
        {
            cout<<"[ "<<str[i]<<" ]"<<" = "<<"[ "<<error[i]<<" ]\n";	
            i++;
            counter++;				
        }

        if(strcmp(error,str)==0 || counter==24)
        {
            cout<<"\nerror found!\n";cin.ignore();
        }

        i=0;l++;counter=0;
    }
    file.close();
    return 0;
}

And here's the output, which I think is what you want.


Line #1 (length:0):


Line #2 (length:24):

[ W ] = [ W ]
[ i ] = [ i ]
[ n ] = [ n ]
[ d ] = [ d ]
[ o ] = [ o ]
[ w ] = [ w ]
[ s ] = [ s ]
[ ] = [ ]
[ I ] = [ I ]
[ P ] = [ P ]
[ ] = [ ]
[ C ] = [ C ]
[ o ] = [ o ]
[ n ] = [ n ]
[ f ] = [ f ]
[ i ] = [ i ]
[ g ] = [ g ]
[ u ] = [ u ]
[ r ] = [ r ]
[ a ] = [ a ]
[ t ] = [ t ]
[ i ] = [ i ]
[ o ] = [ o ]
[ n ] = [ n ]
[ ] = [ ]

error found!

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

>>and it did not work for me
The code you posted is not what I used. Line #1 is not the same, see my post #33 for the correction.

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

It worked for me (famous expression) -- VC++ 2008 Express. Before the change the program was iterating beyond the end of the string and displaying junk characters. That stopped after I made that change. Instead of just saying you don't think it will work you should try it to find out for yourself.

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

Looks like the problem is unitialized buffer. Change this and it works ok char str[500] = {0}; . Now then why its considered an error to find that string is another matter.

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

Are the line lengths the same?
Is capitalization, spaces and punctuation the same?

Have you called stricmp() to see if that resolves the problem ?

Will you zip up the file and attach it to your post so I can view it on my computer. Don't post it as *.txt file so that DaniWeb doesn't change it

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

>>if(strcmp(error,str)==0 || counter==24)
jamthwee is correct about the error in the above line. It should be if(strcmp(error,str) != 0 || counter==24)

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

whats confusing is that when i create the output.txt file manually and not by having it created using the system("mycommand>output.exe") function and then type in the the same output i would get from mycommand.exe , the strcmp(error,str) would then detect the matching lines properly!

That indicates the text generated by the system() function is not the same as what you type manually, could be as simple as a space, tab character, or capitalization. Load both files up into Notepad and compare them side-by-side to see what are the differences.

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

You have to write a windows GUI program and add dialog boxes with edit controls. Tutorial here.

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

fgets() adds the '\n' to the string because its in the keyboard buffer along with all the other keys that you type.

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

fflush(stdin) is non standard -- the fflush() function is only intended for output streams, not input streams, although some compilers use non-standard extension. In any event, fflush() does not affect the input by fgets() function because fgets() removes everything up to and including the '\n' character assuming there is room for the characters in the input buffer.

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

line45: buffer2 = fopen("students.txt", "a+");

the semicolon after remove have error. :(

see my post #13 above

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

got error after remove it the ';' semicolon...

sorry, wrong line number. should be line 42.

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

The reason for the wierd behavior is because fgets() appends the '\n' from keyboard input to the end of the string, so you need to remove it. Here is one way to do it

void fixup(char *s)
{
	if(s[strlen(s)-1] == '\n')
		s[strlen(s)-1] = 0;
}

...
			fgets(newS[i].iN,(sizeof newS[i].iN),stdin);
			fixup(newS[i].iN);

Just call fixup() after each fgets() to strip out the '\n' character.

void main()
That is a big no-no. main() always returns an int so it should be declared int main() . Just because your compiler may accept void doesn't mean it is correct.

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

line 45: remove the semicolon at the end

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

see lines 71 and 72 -- you have way too many spaces there. Remove all those spaces and I'll bet the gender will appear on the same line as the name. Its only a display thing -- the lines are too long so the os wraps them around.

If you want to create fixed length fields for the data then use the field-width specifiers in the print statement. printf("%15s", name); will print the name in a field of 15 characters left padded with spaces. use "%-15s" and it will be right padded with spaces.

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

hi, i am to planning for a device driver please if u have the info about that please reply me

Didn't you even bother to read the replies to this thread??? Salem already provided you the links and all you have to do is read them.

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

This is the contents of the file that I got after correcting the program

Index Number          Name                         Gender
11228ADIA             Alice Lim                       F  
12233ADIB             Mike Chan                       M  
13244BASD             Bobby                           M  
43134QWER             Cheh Yen Lun                    M  
56354VBFG             Choong Wen Loong                M  
47344FGKJ             Chin Pau Soon                   M  

                              Melvin
                                                             M

                              Sandra
                                                             F
#include<stdio.h>
#include <ctype.h>
#define size 99

void maintenance();

struct newI{
	char iN[9];
	char name[50];
	char gender;
};

int main()
{
	FILE *buffer1;
	buffer1 = fopen("students.txt", "w+");

	fprintf(buffer1,"Index Number          Name                         Gender");
	fprintf(buffer1,"\n11228ADIA             Alice Lim                       F  ");
	fprintf(buffer1,"\n12233ADIB             Mike Chan                       M  ");
	fprintf(buffer1,"\n13244BASD             Bobby                           M  ");
	fprintf(buffer1,"\n43134QWER             Cheh Yen Lun                    M  ");
	fprintf(buffer1,"\n56354VBFG             Choong Wen Loong                M  ");
	fprintf(buffer1,"\n47344FGKJ             Chin Pau Soon                   M  ");
	
	fclose(buffer1);
	printf("File students.txt created!!!\n");
	maintenance();
             return 0;
}

void maintenance()
{
	struct newI newS[size];
	int i=0;
	char response;
	char response2;

	printf("To add new information key \'Y\' else press any key to skip>>>");
	response=getchar();
	if(response == 'Y' || response == 'y')
	{
		FILE *buffer2;
		buffer2 = fopen("students.txt", "a+");

		do
		{
			i++;
			printf("\nFor student #%d",i);
			printf("\nEnter student's index number>>>");
			fflush(stdout);
			fgets(newS[i].iN, sizeof(newS[i].iN), stdin);
			printf("\nEnter student's name>>>");
			fflush(stdout);
			fgets(newS[i].name, sizeof(newS[i].name), stdin);
			printf("\nEnter student's gender(either *M* for male or *F* for female only)!!!>>>");
			fflush(stdout);
			scanf("%c",&newS[i].gender);
			getchar(); // flush stdin of the '\n' character
			newS[i].gender = toupper(newS[i].gender);
			while(newS[i].gender!='M' && newS[i].gender!='F')
			{
				printf("\nInvalid input for gender.\nPlease reenter a proper gender as shown>>>");
				fflush(stdout);
				scanf("%c",&newS[i].gender);
			}

			printf("\nDo you still wish to add more student's information");
			printf("\nTo add new information key \'Y\' else press any key to skip>>>");
			fflush(stdout);
			response2=getchar();
			fprintf(buffer2,"\n");
			fprintf(buffer2,"%s", newS[i].iN);
			fprintf(buffer2,"                              %s", newS[i].name); …
Aia commented: Must report you as a hard worker. ;) Hopefully poster will understand what are the changes. +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 39: remove the semicolon at the end

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

post your code. The cells should be separated by comms, spaces or tabs. Each row separated with '\n'. I would use a combination of stringstream and getline() to read the file and separate its cells.

Something like this which assumes cells are separated with white space. A little different when separated by commas.

std::string line;
std::ifstream in("filename here");
while( getline(line) )
{
    vector<string> cells;
    stringstream str(line);
    std::string cel;
    while( str >> cel)
    {
        cells.push_back(cel);
    }
    // now do something with the individual cells
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Its nice that you used code tags but you really should have formatted that code better before posting it. No one but a big masochist is going to read through all that stuff.

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

If someone doesn't know the meaning of a term such as "DHCP" then just use google to look it up. Of course that implies the person knows enough about computers to use google. I didn't know what DHCP was either until just a few minutes ago when I googled for it -- there are lots of good references to it.

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

>> it doesn't work
What exactly does that mean ? And post your most recent code because the code you originally posted doesn't do anything at all.

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

Yes, you need to add more comments -- everywhere. First you need an explaination at the top of the program to tell us what the program is supposed to do. Even "glancing" at main() I don't know what it is trying to do.

If in doubt -- comment it.

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

you have to make the function yourself. Is it a GUI program? If it is then just make a dialog box with the login information. If not then just print out the text and get input just like any other console program. The more difficult part will be how to verify the login information. Professional programs store user ids and passwords in encrypyted form either in simple text files, a cookie, or a database

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

This is what we mean by inline functions in the header file

class Vertex {

public:
	Vertex() { x = y = z = 0.0; }
	~Vertex() { }
	float getX() { return x;}
	float getY()  { return y;}
	float getZ() { return z; };
	void setX(float newX)  { x = newX; };
	void setY(float newY) { y = newY; };
	void setZ(float newZ) { z = newZ; };

private:
	float x, y, z;

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

The code you posted a week ago is obviously not the same code you are using today. So post code (I can't see your monitor very well) and zip up the data file and post it too. stramp is case sensitive so maybe the string that's in the data file is not the same case as the string you want to compare. To do a case-insensive compare you can convert both strings to either upper or lower case before calling strcmp(). Some compilers have a case-insensitive compare function, such as stricmp() in Microsoft compilers and compareNoCase() (or something like that) in some other compilers.

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

gets() ignores spaces too :)

Use the >> operator to ignore spaces cin >> word;

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

>>This, was working till i changed the input style. From gets to cin.get
gets() was wrong to begin with. and cin.get() does not do the same thing as gets(). What you probably wanted was cin.getline() or std::getline().

BTY I fixed up the code tags in your post. [icode] is intended for only one line of code so that it can appear on the same line as the describing text.

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

That problem has nothing to do with the compiler you are using. On line 37: when you enter a numeric value cin will not remove the <Enter> key from the keyboard buffer. You need to strip that out. One way is to call cin.ignore() after line 37.

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

I don't know -- I always use the IDE. But I do know its a lot more complicated than simply putting cin.get() at the end of main().

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

When you compile using the command prompt you first have to open a command window. That window doesn't disappear after compiling or running your program.

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

First, thanks for taking the time to read the rules about code tags :)

>>By the way, how come code tags aren't working properly... Shoulden't this be numbered now
you have to add the language like this:

[code=c++] // your code here

[/code]

>>Unfortunately, when I use this button, after the program is finished, the command prompt automatically clos
If you want the program's window to remain open after the program finishes then you have to add another line just before main() returns to prompt for keyboard input so that the program will pause at that point.

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

warning: implicit declaration of function ‘get_current_dir_name’

It's defined in <unistd.h> when _GNU_SOURCE is defined in the preprocessor.


This is not the solution.

you mean there is a function in unistd.h named get_current_dir_name? Standard library function names are not normally so long. But, if the function is declared in that header file then you are right.

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

I think you misunderstood the other poster.

It was a joke :)

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

So where is get_current_dir_name() defined? Functions must always be declared before they can be used. Since the C compiler does not know any better it will assume the fucntions returns an int, and that is not what the fprintf() statement expects as the parameter. The solution is to prototype it near the top of the program, somewhere above main().

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

Inline solution below

int __inline count_digits(int n)
{
   // blabla
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In Face::addPoint() you should verify that location < the allocation size of numberOfPoints.

Actually, you don't need data.cpp at all. Just use inline code in the header files.

Can't really help you much more without example data file to work with.

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

>>Is there a short way to determine how many digits are in an integer
In a loop keep dividing it by 10 until the result is 0

while(n > 0)
{
   n  /= 10;
   count++;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>I downloaded Visual C++ 2005 Expres Edition ,why i can't make a project ,what to do reslove that?

To create a project: select menu File -->New -->Project, then set Project Type to Win32 and Visual Studio Installed Templates to Win32 Console Application. At the bottom of the screen enter the project name and select the desired location. Finally on that window press the Ok button.

The next window to appear is the Application Settings window. Select Console Application and press the Finish button.

All done.

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

>>i am feeling dumb
Don't feel too bad -- we all go through that learning curve.

Please post your most recent code. You probably need to add a line at the bottom of main() just before return to make it wait for you to see the output.

int main()
{
...
   cin.get(); // get key 
   return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

compile was successful with 0 errors

Great going! :) Now you should see the correct output on the screen.

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

>>duplicate symbol _main
you are getting a blank sceen because there were compile errors. You can not execute the program until after your compiler produces 0 errors and links successfully.

If you get that error message then you are not posting your entire program. You can have only one main() function in a program -- actually that really applies to every function that have the same list of parameters.

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

>>Quotes are generally used to reference headers that require an absolute path or headers in your project
Most (but not all) compilers use angle brackets to reference header files that are in the compiler's known directories, such as the compiler's include directory and double quotes are used to reference all other header files, such as those you write yourself and are in your project's source code directory. On the otherhand, I have used compilers that treat angle brackets and quotes the same.

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

>>Have I got this right so far?
Yup -- that's all there is to it. Hopefully you are not writing a multi-threaded program :)

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

Read the Read Me threads at the beginning of this board because they contain a wealth of information about books, compilers and other related resources.

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

I don't use LoadLibrary() for most DLLs that I write. Instead, I just use __dllimport and prototype it in the *.c or *.cpp application program, then link with the *.lib file that the compiler generated when the DLL was compiled and linked. But of course if you don't have a *.lib file then this method will not work.

// prototype the function that is in the DLL
void __declspec (dlimport) function1();

int main()
{
     function1(); // call the function in the DLL

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

Of course the user could always use Notepad to view the file.