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

Its obvious from the fle contents that atoi() won't work because the file contains a mixture of text and numbers. And since that is NOT a comma-separated file attempting to read up to the comma will fail. As Salem mentioned the columns must be separated by tabs or just simply spaces.

here is a way to do it without any knowledge of the number of words on a line. matrix_points must be defined as a 2d array of strings -- you have it declared as a 2d array of characters. You can't store an entire string in just one character.

const int ItemsPerRow = 8;
typedef std::vector<string> C;
int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<C> matrix_points;
	C words;
	std::ifstream input_file("C:/NDA/NissanNewApplication/Dev/CommonErrorManager/Src/ErrorMessageList.csv");
	int c = 0;
	std::string line;
	while( getline(input_file,line) )
	{
		// now bust the line up into individual words
		while(line != "")
		{
			int pos = line.find(' ');
			if(pos > 0)
			{
				words.push_back(line.substr(0,pos));
				line = line.substr(pos+1);
			}
			else
			{
				words.push_back(line);
				line = "";
			}
		}
		matrix_points.push_back(words);
		words.erase(words.begin(),words.end());
	}
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Following code gives me an error when I use text field in CSV file instead of float or integer fields

what compiler? post the errors. The compiler should give you warnings that it is converting int to char and may lose data.

what does the csv file look like -- can you post the first two or three lines? Are the values between -127 and 126 because that is the range of signed char. If the csv file contains larger numbers then you will have to redeclare the matrix as a 2d array of ints, not chars

int matrix_points[6][8];

and the while loop is incorrect. use of eof() is unpredictable.

// while(!(input_file.eof()))
//{
while( input_file.getline(line, sizeof(line), ',') > 0)
{
   // blabla
}

>> printf("%s", matrix_points[0]);
all these lines are incorrect -- %s expects a null-terminated string, and what you are passing is just one character. They should look like this

printf("%s", matrix_points[i]);

This assumes that the array is null-terminated strings. If not, then %s will not work -- probably crash your program. In that case, change %s to %c as your original post

printf("%c", matrix_points[i][0]);

Finally, you can use loops to make the program more efficient. You can replace all those getline()s with just this one.

int r = 0, c = 0;
while( input_file.getline(line, sizeof(line), ',') > 0)
{
   matrix_points[r][c] = (char)atoi(line);
   ++c;
   if( c == 8)
   {
        r++;
        c = 0;
   }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Hi,
I wanted to read text field and atoi is giving a problem. I cannot use
matrix_points[row][col] = atoi(line);
which is good for integer/float values

If I write
matrix_points[row][col] = line;
it gives an error

Please suggest me what changes could I make

how is matrix_points defined? what is line? you need to post more code than that. And you probably should have started a new thread instead of resurrecting this old one.

And the code Learner posted is partiallly wrong.

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

I couldn't access it all day today from where I work using IE6. Kept getting a strange error (I don't recall what it said). But at home using FireFox it was ok. Coincidence??

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
#include "stdafx.h"
#include <iostream>
using namespace std;

Also, add above line. When you attempt to compile, select menu item Build --> Rebuild Solution, this will cause the compiler to generate the precompiled headers that you mentioned. read this if you don't know what precompiled headers are.

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

>>int _tmain(int argc, _TCHAR* argv[]);

remove the semicolon at the end of that line

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

remove the X button altogether in the form's properties. I don't recall which one -- probably System property.

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

>>AD - Is C# really an option for a commercial type program?

Probably not for your program. If you are pretty speed-conscience then MFC may not be the best choice. Best speed would be achieved by writing pure win32 api program. MFC and Forms make windows programming considerable easier but at the cost of speed. Do you have a couple years or so to write a pure win32 program? If not then you might have to sacrafice speed for programming ease.

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

this should fix the problem

const int MaxN = 10;
...
...
	for(i = 0; i < MaxN; i++)
	{
		 for(j = 0; j < MaxN; j++)
			 a[i][j] = -1;
	}
...
...
... // code omotted
...
	for (i=1; i<=noofelements; i++)
	{
		do {
			rowpos=rand() % n;
			colpos=rand() % n;
		} while( a[rowpos][colpos] >= 0);
		a[rowpos][colpos]=i;
	}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

And what compiler and version of that compiler are you using? Not all compilers are created equal.

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

If you pre-fill all elements of the array with -1 then you will more easily see the cells that were never toched. And the reason is because the program generates random values for rowpos and colpos, and sometimes those values have already been generated. So instead of generating (5*5)-2 = 23 random values it generates something less than that.

One way to correct that would be to check the array to see of the values have been previously generated, and if they have generate new random values. If a[rowpos][colpos] has a values other than -1 then you need to generate new random values.

Probably a faster way to do it is to use two more int arrays, one for rowpos and the other for colpos, initialize each array with numbers from 0 to n, then randomly shuffle the rows. After than you don't need to generate any more random numbers, just use the arrays from row 0 to N the indices into array a[][],

Enter the size of the square (+ve integer, max 257): 5
Enter the number of holes (max 2): 2
-1      -1      8       19      11
16      -1      17      12      -1
-1      -1      -1      2       21
-1      -1      4       -1      20
10      18      22      15      23
Press any key to continue
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

your do-while loop is wrong. It doesn't even compile so I'm not supprised it has runtime errors.

do
	{
			strcpy (sDirName, TEXT(" -i 264\\")); 
			lstrcpy(sFileName,FindFileData.cFileName );
			strcat(sDirName,sFileName);
			// other stuff here

	} while( FindNextFile(hSearch,&FindFileData) != 0);

fix that and it works.

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

I prefer MFC only because I fairly well versed in it -- and because, like an old dog, its difficult to learn new tricks. But for younger people, learn about MFC only because there is a lot of code out there that you may encounter on-the-job. New projects would probably be better off with Forms or maybe C#.

Java?? only for web-based applications (and yes you will need to learn that too). Its too slow for normal desktop apps.

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

>> if (n<1 || n>256)

your array is only 10 rows and columns. If I enter a value of 255 then your program will crash bigtime. Instead of hard-coding a value like 256, create a define or const int to declare the array size and its limits

const int MaxSize = 10
int a[MaxSize][MaxSize];
...
...
if (n<1 || n>MaxSize)

now remember to decrement the value of n by one (range is 0 to (but not including) MaxSize) before using it as an index into the array.

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

why are year, month and day doubles? why not int or long? if you use the functions in <ctime> it would make your class a lot smarter -- for example the formatting you desire can be easily accomplished with strftime(). Once you know year, month and day, fill in a struct tm object, call mktime() to get the size_t time variable then call strftime() to format it however you want. make sure you memset() the struct tm object with all 0s before using it or mktime() may return errors or wrong information.

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

The functions in conio.h are non-standard and not supported by very many compilers. Just a warning because your teacher may not be able to compile your program if he/she uses a compiler that does not support those functions. Most new programmers should not use them so that you learn the generally accepted way to program.

>>char names[100][20] ={ 10 };

what is the significance of 10? why not simply initialize it to 0 like most programmers do?


>>char tmpName[50] = {40};
ditto

>>tmpName[0] = 40;
tmpName is a character array of 40 characters, not an array of pointers. Why are you setting the first element of tempName to 40? What good will that do? _cgets() will just ignore it anyway and fill the buffer with whatever you type, even if you type too many characters. fgets() is a better (and standard) function because it will not overflow the input buffer.

fgets(tmpName,sizeof(tmpName), stdin);

If function mystrcmp() is going to sort the array, then you should name the function that is more descriptive of its true purpose. It contains many uninitialized variables and array. what is the purpose of word array? It isn't initiailized to anything but yet you are attempting to sort it.

There are several variations of the bubble sort, here is the one I use probably because it was the first one I learned. Using the two parameters to the function you posted:

int i,j;
for(i = 0; …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

in MS-Windows NT/W2K/XP, cmd.exe is the shell emulator, Win95/98 it was (is) command.com. So if you want to lock out the command prompt you will have to write your own version of either cmd.exe or command.com. It would require a firm understanding of C language and win32 api functions to accomplish that. Definitely not a project for new students.

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

There are lots of ways to do it, here is just one of them. The class will need public access to the private variables only if something outside the class needs access to those variables, and that's the purpose of the Get() and Set() methods.

#include<iostream>
using namespace std;

class Job
{
public:
  Job() 
  {
     JobNum = Hours = 0;
     Rate = 0;
  }
  int GetJobNum() {return JobNum;}
  int GetHours() {return Hours;}
  double GetRate() {return Rate;}
  void SetJobNum(int jn) {JobNum = jn;}
  void SetHours(int h) {Hours = h;}
  void SetRate(double r) {Rate = r;}
private:
 int JobNum;
 int Hours;
 double Rate;
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

On MS-Windows, use FindFirstFile() and FindNextFile(). There are lots of examples how to use those two functions floating around, just use google to search for them.

>>I cannot use win32 because the program would be used as an example in a class introducing the C programming language

How to get those filenames is an os-specific task -- you will have to use win32 api functions. Yes, they can be used in console applications, just include windows.h. And it does not require any knowlege of c++. But it does require a little more then absolute beginner's knowledge. Need to know about loops, and structures and character arrays. If that is too much then you probably should pick a different example.

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

Did you post your exact program? Because the code you posted doesn't fit the results you posted.

>>inFile >> fName >> lName[i++] >> age >> sex >> wTime;

lName array is ok, but on every iteration of that loop the program just simply overwrites whatever was read on the previous loop for the other variables. I'd suggest you use a structure to contain all the information, then sort the structures.

struct person
{
   string lName;
   string fName;
   int age;
   // etc etc
};

struct person array[MaxPeople];

//put the next two lines inside the read-data loop.
nFile >> array[i].fName >> array[i].lName>> array[i].age >> array[i].sex >> array[i].wTime;
++i;

Now all you have to do is use selection sort to sort the structures by last name. use memcpy() to exchange the entire structure, not just the individual elements of the structure will make it work faster.

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

The in MS-Windows the default heap is not static -- the os will expand it as needed. But since malloc() takes an size_t integer as the parameter the largest amount of memory malloc can allocate at one time is the largest value that can be stored in the size_t integer (see limits.h). And yes, all the computer's available and unused memory can be used by malloc with the previously mentioned restriction.

malloc() normally fails today for one of two reason: (1) attempt to allocate more memory then is available, or (2) memory has been previously trashed (most common reason) such as buffer overflows and using uninitialized pointers (although there are a whole host of other causes).

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

I am supprised that even compiled -- C is surrounded by double quotes, not single quotes

// DLL code
extern "C" {

	__declspec( dllexport )double _cdecl area(int& a, int& b)
	{
		return a*b;
	}
};
// application program
#pragma comment(lib,"Releasetest4.lib");
extern "C" {

	__declspec( dllimport )double _cdecl area(int& a, int& b);
};


int _tmain(int argc, _TCHAR* argv[])
{
	int a = 1;
	int b = 2;
	double x = area(a,b);
	return 0;
}

When you run this, the dll must be either in the current working directory or in one of the directories in your PATH environment variable.

I have no clue how to make that dll work with C#.

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

>>I've tried the same with my VS 2005 but I didn't worked! I was misssing this .lib file

that compiler will not generate a .lib file unless there are some exported symbols (either functions and/or objects). __declspec( dllexport ) will export them

__declspec( dllexport ) int foo;

>>what do we need .lib file for?
In some C/C++ programs you don't need it at all, the alternative is to use LoadLibrary().

using .lib files is most commonly used by the compiler's linker to resolve the functions that are called from your program when generating the .exe file.

>>when one would use MFC
MFC is a collection of c++ classes for developing GUI programs. Normally used for business applications, but can be used for simple games. www.codeproject.com has a huge collection of free MFC programs and helpful libraries that you can plug into your project. It also has a little bit of C# stuff.

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

I don't get that error. Check your file against the one you posted in the previous thread. Or post the offending line of that file.

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

look at that line in your copy of aspstring.h -- it contains a semicolon where it shouldn't.

I have VC++ 2005 Express and get a ton of errors/warnings on apstring.cpp add the below line near the top of apstring.h to stop all the complaining about deprecated functions in string.h

#pragma warning(disable: 4996)
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

apstring is not set correctly for modern compilers shuch as the one you are using. It is still using the old, obsolete version of iostream. Make these changes in apstring.h

//#include <iostream.h> << delete this line
#include <iostream>  << add this and the next line
using namespace std;

Then in bank.h you must declare the return type of the methods. Below I made them void, but I didn't look at the code to see if that is actually what they should be.

//modifiers
		void deposit (const double &amount, apstring savcheck);
		void withdraw(const double &amount, apstring savcheck);
		void transfer(const double &amount, apstring savcheck);

The above will take care of the compiler errors/warnings. Next your compiler should complain about missing/unresolved errors. You will have to implement the missing methods.

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

I don't know how to put the ATM into my program

neither do we if you don't post the rest of the program. Zip up all the source file and header files (bank.h and account.h) and attach them to your post.

also tell us what compiler and operating system you are using, and post the compiler errors.

[edit]Just noticed -- you must be using MSVC++ compiler because you include stdafx.h. That header must be the very first one listed. And remove the .h extension from the other standard headers because they are obsolete. Your program should look like this[/edit]

#include "stdafx.h"
#include <iostream>
#include <fstream> 
#include <iomanip> 
#include <cassert> 
#include "apstring.h" 
#include "account.h" 
#include "bank.h" 
using namespace std;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

my next question has to do with changing the contents of the array of structures. How do I keep the original items from being altered?

make a duplicate copy of the array (or save it in a data file) before it is sorted.

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

This is what I get.

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

Whenever (or most of the time) I hit the bookmark to come to this site I get a File Not Found error from your server -- I know its your sever because it has a DaniWeb banner across the top. Click the bookmark again and everything works normally. This is more of an annoyance over the past 3 or 4 days then a real problem. Any way to fix it??? BTW: I use Firebox browser on XP, if that makes any difference.

Thanks

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

google for "splash screens". Here is how to do it for MFC program.

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

you are attempting to use c++ techniques on C strings. use strcpy() to copy one string to another

char tmp[LEN];
    strcpy(tmp, flight[i].first);

However, that is not really what you want to do anyway -- it takes to much time to swap individual members of a structure. You want to swap the entire structure in one shot.

struct data temp;
...
...
  // now swap the structures
   memcpy(&temp,&flight[i], sizeof(struct data));
   memcpy(&flight[i], &flight[j], sizeof(struct data));
   memcpy(&flight[j], &temp, sizeof(struct data));
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

,back to the real subject.
if the use of char(x) wasent correct then why did it work when i put it through my compiler im confused now?

I didn't say char(x) was not correct -- char(int x) is incorrect.

int x = 'A';
printf("%c", char(x)); <<< this is ok
printf("%c", (char)x); <<< and is same as this
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

My bad dragon. I assumed it was correct since it worked ;)

It worked because it assigned the value 1000001 decimal to the integer, not 1000001 binary, which has a completly different decimal value.

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

>>Everyone can be wrong

I've only been wrong once in my life -- that was when I thought I was wrong, but I was wrong about that :cheesy:

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

int n = 1000001 is NOT the binary representation of 1000001 Binary. Use your scienticic calculator -- 1000001 Decimal is 11110100001001000001 Binary! C and C++ cannot do auto convertion of binary numbers as it does decimal, hex and octal.

char( int n )

[edit]The above is invalid statement

that is more commonily written like this (a cast)

(char)n
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Its not as easily accomplished in *nix -- you have to do lots and lots of programming to get that to work in graphics mode. Motif open source package helps a little. Or you might use console-based curses library functions.

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

>>if you convert binary to decimal the computer will have to convert decimal back to bianary so why not use bianary

No it doesn't -- what you see on the screen is not the same as how it is represented internally by the program. The program only sees binary -- doesn't know a thing about visual stuff. The number 01000B is only visual -- what you see on your monitor. The program cannot deal with that directly, it has to be converted to its internal format. 01000B occupies 5 bytes of data (just count them -- '0', '1',. '0' ... is 5 digits). That all has to be converted down to the size of an int (4 bytes on 32-bit processor).

After the conversion, if you use a debugger and look at value of the 4 bytes beginning at the address of the integer you used, you will find that their vaue (in Intell computers) is 08 00 00 00 in Hex format, which is the original converted value.


I am well aware of how the hardware works. Unfortunately C and C++ languages do not have automatic way of converting a string that contains a binary numbers such as 100011 into an integer using the assignment operator, such as

int n = "0100011";<<< WRONG

but you can use strtol()

char *p
int n = strtol("010011",&p,2); // 010011 Binary == 19 Decimal

Now, if you use a scientific calculator you will find that 'A' …

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

dude i have already read about that stuff that wasnt my question at all

The only way to answer your question is to convert the binary digits to decimal, as shown in those posts and other places you have probably read. Afterall, all characters, such as 'A', are nothing more than integers. 'A' = 64, 'B' = 65, etc. google for "ascii chart" and you will find them all. just simply convert the binary value to integer and you will have the answer to your question.

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

Yep it's pretty simple:

int main(int argc, char *argv[])
{
    int x = 1000001;
    cout << char(x) << endl;
    getchar();
}

That isn't right -- it just assigns the number 1000001 decimal to an integer. binary digits have to be converted.

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

there is a lot of answers on the web if you would use Google Look here for one of them.

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

I don't have any problems like that using FireFox 1.07.

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

>>whats i/o mean

A general term that means Input/Output -- such as writing to and reading from files or some other device such as sockets, keybord, serial ports, cd drives, databases, etc.

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

with some exseptions

and there are a lot of exceptions :cheesy: you can't even do file i/o with it. the only useful thing HTML can be used for is displaying stuff in browser windows. (I'm not an HTML programmer).

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

tecknickly it is a programing launguage

Narue is right --No HTML specification has ever called HTML a programming language, or anything like that. It is most commonily called a markup-language

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

see my previous post for example how to use the brackets. And put them on a separate line to make them easier to see -- helps understand the code better.

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

But, don't do that. You won't be able to show your code to very many people before they lynch you. ;)

You are absolutely right about that! I once worked on a large-team project where one of the programmers came from PASCAL -- he created almost everything in macros so that his c++ program looked just like a PASCAL program. After he left we all cursed him for that and rewrite his programs to remove those macros.

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

I think there there is a *nix shell conmmand that will do it (tee ??) but not in MS-Windows. you might put the output lines into a function then call it twice

void foo(ofstream& out)
{
   out << "Hello World " << endl;
}

int main()
{
   ofstream out("file.txt");
   foo(out);
   foo(cout);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> if its a character array then initialize it with '\0' and if its an integer array then initilize it with 0

I'm not about to revive that old thread you linked, so I'll do it here because I just can't resist the urge :twisted: . In the above, '\0' and 0 are identical because '\0' == 0. Personally, I prefer just plain 0 -- why? because its less typing.

char array[255];
memset(array,0,sizeof(array));

int array[255];
memset(array,0,sizeof(array);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

c and c++ does not use the "then" statement as some other languages do. The next statement or block following the if statement is executed when the if statement is true. Notice the use of the double '=' symbol which is the logical equal. A single '=' means assignment. Lots of people get tripped up on those differences -- even some old timers like me :o

if( g == 3282)
{
    // true condition
    g = k;
}
else
{
   // false statement
}