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

Oh, now I understand -- reading from keyboard not a data file. It makes sense now. But should use getline(), not read. And use record.name.

while (getline(cin, record.name))
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

And make sure "Solution Configurations" is set for Debug, not Release

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

is record.name a std::string object? If it is, I might be wrong but I don't think you can do binary reads and writes like you are doing with that string because all that is getting read/written is the number of bytes in the class itself -- the string is not part of the class. If std::string == "Hello World", then write() will only write sizeof(std::string), not the text. For binary files, you will have to write/read a fixed-length structure that contains C character arrays.

class record
{
  . ..
  char name[80];
 ...
}

There are other more complex ways to do it that will allow you to use std::string in the class record, but the above is the simplest.

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

Yes, the size of the file does not necessarily reflect the amount of data a program has actually stored in it -- there are win32 api functions that can increase the file size to an arbitrary size without doing any writes at all. I don't know if *nix or MAC has similar functions.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
for (i =0; i <sizeof(line)  ; i++

The above ^^^^ is wrong, use strlen(line) instead because sizeof(line) may not be the same as the string length.

char line[1024] = "Hello Word";

sizeof(line) = 1024, but strlen(line) = only 11!

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

how can i remove my username from this list if i want to

pm Narue and I'm sure she will be happy to ban you if that is what you want.

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

works for me -- what makes you think it isn't working?. And, BTW, main always returns int, never void. I know some older textbooks and some M$ examples use void main() that that is incorrect.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	char line[] = "james smith and son";
	char *ptr = strstr(line,"james smith ");
	if(ptr != 0)
	{
		if( ptr == line || isspace(*(ptr-1)))
		// if ptr is at beginning of line, or previous character
		// is a white space
		{
			 cout << "Found" << endl;
		}
		else
			cout << "Not found" << endl;
	}
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

one way to do it is to use strtok() to split the original string up with the semicolon. Then use strstr() in the pointer returned by strtok(). That way you only have to work with one statement at a time.

*(ptr-1) does nothing more than back up the pointer one character and reference the char at that location.

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

To keep the thread clean for relevant discussion, please direct any further off-topic comments to my private message box.

Peace :lol:

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

google for "definition of file size" and your definition is not included

http://www.google.com/search?hl=en&lr=&oi=defmore&defl=en&q=define:File+size

The amount of space that a file takes up when stored on disk. File size is usally measured in bytes, kilobytes (K), megabytes (MB) or gigabytes (GB).
www.bized.ac.uk/educators/16-19/business/marketing/lesson/sup_glossary.htm

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


>reading the file from beginning to end in MS-Windows will result
>in incorrect file size if the file is opened in text mode.
Perhaps if you want a byte count, but more commonly a character count is what people want. And if they want a byte count, they open the file as binary. So I say again, with complete confidence: The only way to portably get the size of a file is to read it from beginning to end and count what you're looking for.

Why do you assume you can read people's minds? The guy said he wants the file size, not the count of the number of characters in the file. If you want the character count they you are right -- the entire file has to be read. But that is not what "file size" means.

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

>oThe only portable way to determine the working size of a file is to open the file and read it, from beginning to end.

bullpucky! prove it by example. use of standard library file i/o functions are guarenteed by the language to be portable across platforms that support them. Open the file in binary mode and reading the file from beginning to end in MS-Windows will result same file size as returned by moving the file pointer to end-of-file and getting the file position. Open the file in text mode and reading file file will result in incorrect file size, but setting file pointer to end-of-file will result in the same file size that is returned when the file is opened in binary mode.

Text and binary mode opens are only relevant on MS-DOS/MS-Windows platforms. Other platforms result in identical reads/writes.

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

>> I can 't figure this out

Didn't your mother ever tell you that can't never did anything? try a little harder, and don't expect to do it all in one line of code. After getting pointer from strstr, check the character just before it to see if it is either the beginning of the line or a space. If it is neither, then it isn't the "int" keyword.

char *ptr = strstr(line,"int ");
if(ptr != 0)
{
  if( ptr == line || isspace(*(ptr-1)))
  // if ptr is at beginning of line, or previous character
  // is a white space
  {
         // int keyword found
  }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

open the file, seek to end, the get current file position. How to do that depends on C or C++. There are also other ways, such as stat(), win32 api GetFileSize(), and others.

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

No -- I did not post a solution, just mentioned it as something YOU need to consider. I left the solution up to the programmer (you!)

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

>> for (i =0; i <sizeof(ch) ; i++

ch is a pointer, so sizeof(ch) will ALWAYS return 4 (assuming a 32-bit compiler). what you want is length of the string -- using strlen(ch)

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

how did you test that?

int main( void )
{
    char line[] = "int a,b; int c;";
    char*p = strstr(line,"int ");
    cout << p << endl;
    p = strstr(&line[1],"int ");
    cout << p << endl;
    cin.ignore();
  return 0;
}

or using strtok

int main( void )
{
    char line[] = "int a,b; int c;";
    char *str = strtok(line,";");
    while(str)
    {     
       char*p = strstr(str,"int ");
       cout << p << endl;
       str = strtok(0,";");
    }
    cin.ignore();
  return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

your program will not work if there is a variable name that contains the string "int" but is not an int data type. Example: "float myint". your program will see myint as the int. you need to check for a space following int -- "int ", and also check for a space preceeding or beginning-of-line. example: "float myint " is not what you want even though there is a space following text "int".

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

here is one way to do it. It just has the string hard-coded, but you should be able to easily make it work with data file. Also assumes int is at the beginning of the string -- you should be able to change it to recognize int in the middle of the string too.

int main( void )
{
    string line = "int a,b,c,d; double k;";
    string tmp;
    int pos;
    if( line.substr(0,4) == "int ")
    {
        line = line.substr(4);
        pos = line.find(';');
        if(pos > 0)
           line = line.substr(0,pos);
        int counter = 0;
        while(line != "")
        {
           int pos = line.find(',');
           if(pos < 0)
           {
              tmp = line;
              line = "";
           }
           else
           {
               tmp = line.substr(0,pos);
               line = line.substr(pos+1);
           }
           counter++;
           cout << counter << ": " << tmp << endl;              
               
        }    
    }
    cin.ignore();
  return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

data = &flag;

that is incorrect. data is a pointer to a variable inside main(). If you want to change it, then you need to do something like this (typecast data to int pointer)

*(int*)data = flag;

That should fix the immediate problem, but then you need to add code that will syncronize access to the variable inside main() so that main() will not read a partially written variable when the read begins to change it. There is no guarentee that the operating system will not switch execution threads in the middle of read or write operations. You can implement system-wide variables called Semaphores.

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

there are methods of sorting text files, but they are generally pretty complicated. If the file is small enough, just read all the strings into an in-memory array, sort the array, then rewrite them back to the file. If you are writing c++ program that is pretty easy -- read the strings into a std::vector<std::string> array, then use std::sort() to sort it.

Use a pretty recent compiler such as Dev-C++. The code you posted uses iostream.h which has been obsolete for several years, so I suspect you are using a pretty ancient compiler such as Turbo C++.

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

I thought you meant that the text file contains a mixture of words and integers, and you wanted to count the number of integers in the file. If that is not the case, then you need to post an example of the text file and a better (more detailed) explaination of what you want to do with it.

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

write a function to test the string to see if it contains only numeric digits. Fucntion should return true if the string contains only numeric digits or false otherwise.

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

if you are using C instead of C++ use sprintf()

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

>>n y is c++ a dead compiler

Now, what the hell does that mean? n could stand for any of several dozen words in the dictionary that begin with the letter n. y could also. Those are two very very good reasons people should spell out words -- I can understand bad english, but not that kind of crazy abbreviations. I hope you don't write like that in high school or college English classes!

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

you can't. Like the book says, derived class inherits ALL members of the base class. But, what you can do is make members of the base class not accessible to members of derived class by making base class members private. In the code below, class derived cannot access variable x in class base.

class base
{
...
private:
   int x;
}

class derived : public base
{
...
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>f it was, Narue, can you explain the reason atoi() is bad

There is very little "bad" about atoi() -- you were using it incorrectly. It does have one problem in that it does not detect integer overflow. If you are going to use a function and don't really understand how it works, then RTFM! You don't drive a car by guesswork, so what makes you think you can guess at the parameters to functions.

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

1. where is menuItemList object declared?

2. why all those useless lines that access the structure but does nothing with them? For example: menuItemList.BaconandEggs; If it is supposed to be setting them to 0 -- it doesn't.

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

if all you want to do is change the $ to _, you can just change argv[] and not copy to another buffer (see Narue's earlier comment).

for(i = 1; i < argc; i++)
{
    char* p = strchr(argv[i],'$'); // find the $ symbol
    if(p != 0)
        *p = '_';
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I meant to escepe it on the command-line so that the shell does not interpret it. If you have no control over that, then escaping it is not an option. Doing it after it reaches your program argv[] will do nothing more than put the liberal '\' in argv[] string.

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

>>Solaris I get a fault. I have traced it down to the "$" character on the command line.

does Solaris shell interpret that $ symbol (is it a special character like '>' and '<')? try putting it in quotes
_VARIABLE=SRC_SERVICE.IDN.me _HOST=chtsapdbu3 "$TYPE=CHI_PRD"

or if that doesn't work, can you escape it?
_VARIABLE=SRC_SERVICE.IDN.me _HOST=chtsapdbu3 \$TYPE=CHI_PRD

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

what compiler (and version) are you using? What is (are) the error messages?

after including the C++ stl header files you need to declare the namespace

#include <iostream>
... other header files here
using namespace std;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

are the argv[] strings in read-only memory? Maybe you need to copy the string to a temp buffer and use strtok on that temp buffer.

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

The clues in the question are modify data and ++ptr. Both these need non-constants. So the only possible answer is a).

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

they all have resolution of about 1 ms. There are high resolution counters

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

The functions in time.h are only accurate to 1 second -- so no matter how you divide it you still have only 1 second accuracy. You need to use other functions, such as clock(), or win32 api function GetTickCount(), or GetSystemTime() that provide more accuracy.

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

set a boolean flag to indicate if the word is found in one of the two files. If not set then it must be a noun. Something like this sudo-code. Note: search second file ONLY if the word is not in the first file.

set flag to false
search verb.txt file for word
if word found in verb.txt then set flag to true
if flag still false
begin
    search adjective.txt file for word
    if word found in adjective.txt file set flag to true
end if
if flag still false
   word is a noun
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
std::string isbn;
getline(fin,isgm);

if you want to read them all into an array

std::vector<std::string> isbn_list;
std::string isbn;

while( fin >> isbn)
  isbn_list.push_back(isbn);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

your program is littered with uninitialized pointers! set all pointers to 0 when declared and it will make your debugging a lot easier. Replace all those pointer arithmetic with normal array indexing. Make sure the program allocates memory before using it.

// replace this
 *(a+j)=new char*[w_len]; // exception attemtping to dereference uninitialized pointer
// with this
  a[j]=new char*[w_len];

// using uninitialized pointer and unallocated [b]p[/b]
  cin.getline(p,150,-1);

and replace obsolete <iostream.h> with <iostream>

Unless you are required to use char pointers, you should consider replacing them with std::vector and std::string and eliminate all that messey memory allocations.

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

compiles ok with VC++ 2005 Express. post your entire program.

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


int main()
{
 ifstream history( "inputData.txt" );

istream_iterator<double> begin( history );
istream_iterator<double> end;

vector<double> input( begin, end ); 
return 0;    
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I stripped all those fstream objects out of the program so that I could see easier what was going on. The below version works -- for menu items 1 (depesit) and 2 (withdrawal) only.

If you want to do both input and output on the same file you should use only one fstream object, not two.

After reading the whole file, the stream has the eof error bit set, and that needs to be cleared before the stream can be used again. See example that I posted.

when ios::app is used it is not necessary to specifically seek to end-of-file before appending new data -- fstream will do that for you.

You don't need conio.h or stdio.h -- so I deleted them.

str[] and str1[] buffers were too small, strftime() was causing buffer overflow.

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include <ctime>
#include<iterator>
#include<vector>
using namespace std;

int main()
{
	fstream stream("inputData.txt", ios::in | ios::out | ios::app);
	if(!stream.is_open())
	{
		cout << "stream not opened" << endl;
		return 1;
	}

	ifstream history( "inputData.txt" );

istream_iterator<double> begin( history );
istream_iterator<double> end;

vector<double> input( begin, end );

cout<<fixed<<setprecision(2);

float amount,total,totalTwo;
string copyright="©";
int selection;

struct tm *ptr;
time_t tm;
char str[126];
char str1[126];

tm = time(NULL);
ptr = localtime(&tm);
strftime(str ,100 , "Today is %B %d, %Y. The current time is: %I:%M:%S %p.\n",ptr);
strftime(str1,100,"%B %d, %Y - %I:%M:%S %p",ptr);
cout<<str<<endl;

cout<<"Welcome to the Automated Teller Machine Program."<<endl;
cout<<'\n';
cout<<"What would you like to do today?"<<endl;
cout<<"(1)Withdraw"<<endl;
cout<<"(2)Deposit"<<endl;
cout<<"(3)View Withdrawal …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

first, this is wrong

while(inputOne.eof() == 0)
{ 
inputOne>>total;
}

It should be coded like this because eof() doesn't catch other errors that might occur which will cause an infinite loop in the program.

while(inputOne>>total)
{
  // do something
}

Not sure about the other question(s) -- don't have time right now to look at it.

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

then the problem is elsewhere. try test.is_open() instead of if test.fail(). Use your compiler's debugger and look at what exactly is in the file buffer, or use cout to display it.

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

1. please use code tags when posting a lot of code.

2. you forgot to ask a question. Or did you just want us to see your gorgeous code?

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

when you enter the filename from the keyboard do not use double backslash -- double slashes are only needed for string literals in your program. So you should type c:\fred.txt , only one backslash. Reason: the compiler does not interpret anything you enter from the keyboard, it only interprets string literals.

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

If you are running Norton Antivirus turn it off during development. Sometimes it causes problems like that. Don't forget to turn it back on when you are done.

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

what compiler are you using? I just tried it with VC++ 2005 Express and had no problems.

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

Where is the printer name, What is a printer hadle and printer defaults. I am not sure what to pass into these parameter : OpenPrinter(PrinterName ,PrintHandle,&PrinterDefault)

Sorry, just have never had to print directly to a printer other than in COBOL.

MuthuIVS

You have to name the printer when you installed it. Start --> Printers & Faxes dialog will show all the printers that are installed on your computer.

There are some printer MFC and non-MFC c++ classes at www.codeproject.com that you can use as examples. Printing can become pretty complex, especially if you want lines such as on a form.

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

suggestion: read the 100 numbers from a file instead of entering them from the keyboard.

#include<iostream>
#include<iomanip>
using namespace std;

	//function prototypes
void initialize (int numbers[], int listSize);
void readNum (int numbers[], int& listSize);
void selectSort (int numbers[], int listSize);

int main()
{
	int Numbers [100];
	int count= 0;
	int listsize = 100;
	int temp;
	

	initialize (Numbers, listsize);

	cout <<"Enter a maximum of 100 positive integers ending with -999"<<endl;
	readNum (Numbers, listsize);
	cout <<endl;
	cout <<fixed<<showpoint<<setw(7)<<"Number"<<setw(9)<<"Count"<<endl;

	selectSort(Numbers, listsize);

        temp = Numbers[0];
        count = 1;
	for (int t=1; t< listsize; t++)
	{
               if(Numbers[t] == temp)
                  count++;
              else
              {
                   cout << "temp = " << temp << " total = " << count;
                  temp = Numbers[t];
                   count = 1;
              }
	}
        // display count of final number
        cout << "temp = " << temp << " total = " << count;

	return 0;
}

void initialize (int numbers[], int listSize)
{
	int index;
	int Num = 0;
	for (index=0; index < 100; index++)
use listSize in above loop
		numbers[index] = 0;
}

void readNum (int numbers[], int&listSize)
{
	int index =0;
	int num =0;
	listSize=0;
You don't need variable index.  use listSize instead
	while (num != -999)
	{
		cin >>num;
		numbers[index] = num;
		index++;
		listSize++;
	}
}

void selectSort (int numbers[], int listSize)
{
	int index, minIndex, smallestIndex, temp;

	for (index=0; index < listSize - 1;index++)
	{
		smallestIndex = index;
		for (minIndex = index + 1; minIndex < listSize; minIndex++)
			if (numbers[minIndex] < numbers[smallestIndex])
				smallestIndex = minIndex;
			
			temp = numbers[smallestIndex];
			numbers[smallestIndex] …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

see the last line of main()