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

Moved to C forum because it is a C program, not c++.

>>fflush(stdin);
Non-standard use of fflush(). There is no standard way to flush the input stream, and fflush() is only defined to flush output streams to the file on disk (e.g. your hard drive).

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

Here is one way to do it -- uses a vector of structures instead of individual arrays.

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

struct person
{
    string name;
    float weight;
    string location;
    string color;
};

//inside main
int main(void)
{
    vector<person> people;
    string wt;
    person p;
    ifstream in("data.txt");
    while( getline(in,p.name,',') )
    {
        getline(in,wt,',');
        p.weight = (float)atof(wt.c_str());
        getline(in,p.location,',');
        getline(in,p.color);
        people.push_back(p);
    }
//
// now just display all the data in the vector
//
    vector<person>::iterator it = people.begin();
    for(; it != people.end(); it++)
    {
        cout << (*it).name << " "
            << (*it).weight << " "
            << (*it).location << " "
            << (*it).color << "\n";
            
    }

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

Hello:
Anyone know of an easy way to cast the unsigned char in[16] in the below code to an LPCWSTR using just the CRT? I tried swprintf but it wasn't working. I've been googling this for a week and appreciate the help.

Sorry but I should have mentioned this before. Why did it take you over a week to come up this this simple solution? Note that "%S" (capital S) will convert from char* to wchar_t*. There are, of course, other functions that will also do the conversion.

int main(void)
{
    char world[] = "Hello World";
    wchar_t wworld[40];
    swprintf(wworld, L"%S", world);

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

I thought all the console stuff was commented out? This isn't stdin, is it?

std::wstring r(buf);

Could you tell me what line you see the console stuff at? It should be commented.

No -- what you posted is C++, not C. C doesn't know a thing about STL.

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

Why are you bothering to count the number of integers in the file before actually reading them into the array? Your program doesn't do any dynamic allocation of the array, so what's the point of doing that? Just put the data in the array the first time the file is read and be done with it.

sequence_length = 0;
while( sequence_length < 100 && infile >> number_array[ sequence_length++]) 
{
         // do nothing
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

0 is the same as '\0' -- use which ever one you want. I just find 0 easier to type :)

typecasting is required in c++, but not in C. Since this is c++ board the typecast would be required.

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

Something like that.

char* p1 = (char *)malloc(strlen(name)+strlen(p)+1));
char* p2 = p1;
char p3 = p;
while(*p3)
   *p2++ = *p3++;
p3 = name;
while( *p3 )
   *p2++ = *p3++;
*p2 = 0;
free(p);
p = p2;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Did you select menu Build --> Build Solution? You have to do that before doing anything else.

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

what is line 6 supposed to do? Its not a function call, so I don't know what you intend here.

What I think you need to do there is create a loop and iterate through the array

bool found = false;
for(int i = 0; i < MaxSize; i++)
{
   if( studentID[i] == id)
   {
        found = true;
        // now delete the rows in all arrays
        studentID[i] = "";
        studentMark[i] = 0;
        studentGrade[i] = '\0';
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What?

That 16# was written by me. And it didn't work.

oops! Looks ok to me. What makes you think it doesn't work? Did you read the comment in RED in the very next post (#17) ?

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

>>p += name;

No, it doesn't work that way because p is a pointer, not a std::string object. And I would suggest you replace malloc() with new and free() with delete or delete[] because this is a c++ program. But if your teacher insists on using malloc() then change the code I have posted below accordingly.

if( p != NULL)
{
   char* p1 = new char[strlen(name)+strlen(p)+1);
   strcpy(p1,p);
   strcat(p1,name);
    delete[] p;
   p = p1;
}
else
{
    p = new char[strlen(name)+1];
    strcpy(p, name);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Then I guess you are taking a c++ class. I'm moving this to the c++ board.

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

lines 37-80: That is c++ code, not C. Are you writing a C program (afterall this is the C forum) or a c++ program ?

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

See post #16 -- he showed you the code how to do it.

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

>>is there a difference between call by reference and pass by reference??
No. They both mean the same thing.

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

There is nothing wrong with trying to provide better ways to do something, as long as it meets the OPs purpose. If I'm trying to learn French there is no use speaking to me in Chinese.

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

You need a way to keep track of how many rows in the studentID array are actually used. One way to do that is to just count up the number of rows where id is not blank, or "". When you want to delete a row just make its studentID = "".

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

P.S: Guys the person "san gabriel" doesn't know about string classes I suppose may be new to c++ so wouldn't the new info be better?

No, at least not for the purpose of his assignment. He is probably learning about character arrays, not c++ STL classes.

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

I merged the two threads about the same problem so that people don't get confused about which one to respond do.

csurfer commented: Thanks for organising Daniweb !!! You always do it ! :) +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The code marked in RED is for the menu item statistics and I have no idea what that's supposed to do.

>>Also can you tell me how to make a new menu item "DELETE MARK"

See the function do_again(). Just add another menu item to what's already there and you will also have to add another option function like the others.

option_3(): >>cin >> studentID[actualNum];
That's wrong. It should be: cin >> id; >> studentMark[actualNum]=mark;
That is also incorrect. First you will have to search studentID array for the id previously entered, then, if found, use that value of the loop counter to index into array studentMark. For example:

bool found = false;
for(int i = 0; i < MaxSize; ++i)
{
   if( studentID[i] == id )
   {
        studentMark[i] = mark;
        found = true;
   }
}
if( found == false)
{
    // add new row in studentID and studentMark arrays
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

And what did Larson do with his money? He paid the taxes on his winnings ($35,000) and invested the rest, most of it in vacant land in a real-estate deal to build homes back in Lebanon. "It didn't work out," he says. "We had a cash-flow problem, and I lost everything."

He pressed his luck once too often!

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

>>values.erase (values.begin());
That should erase the entire array.

Here is one way to print all the integers in the array

for(int i = 0; i < values.size(); i++)
   cout << values[i] << "\n";

Here is another way using an iterator

vector<int>::iterator it = values.begin();
for( ; it != values.end(); it++)
   cout << *it << "\n";
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you really don't need to use strtok() for this problem. All you need is strchr(). First locate the ')' charcter, copy that to areaCode character array. Then find the '-' character, copy that to exthange character array. Finally copy the remaining characters to the ext array.

If you know how to use pointers, then you don't need strchr() either.

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

Well, that's obvious, she's says it's very simple. It is obviously below her to solve such a simple problem.

That may be true -- and in that case learn. Its the "give a man a fish" principle.

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

1. Just attach the file to your post here. Nobody will download that file for fear it might contain viruses or worms.

2. Why don't you translate it yourself.

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

line 4: move it up above liine 1

line 13: for (int Hour=1; Hour <= 24; Hour++)

That is wrong. Arrays are numbered from 0 to (and including) 23, so it should be written like this: for (int Hour=0; Hour < 24; Hour++) line 15: Emp = Cust/20 + 3;

Cust is an array, so it can't be divided. Maybe what you want is to index into the array: Emp = Cust[Hour]; line 16 has similar problem as line 15. You can use the mod operator on an array. if( Cust[Hour] % 20 == 0)

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

>>to get a compiler-independent solution?
No. If you are using command-line builds then add -DDEBUG in the makefile. If you using IDE, how to do it will depend on the IDE.

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

delete that while loop -- no loop is necessary with the code you wrote because it reads and writes the entire file in one big swoop. That isn't practical for large files, but ok for fairly small ones.

And you need to open both files in binary mode -- just add ios::binary as second argument to the open function.

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

read the two files into memory then call memcmp() to compare them. Or, if MS-Windows, use the COMP program (create a command window, then type c:>Help <Return> and view the available commands.

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

It is compiler dependent. Some compilers might define DEBUG, others _DEBUG, and still others might not define it at all. IMHO the best way to do it is to define the macro yourself in command-line options, such as -DDEBUG.

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

Probably your compiler because it worked ok for me after correcting the header files as previously explained.

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

but the memory has not been allocated, so if you try to modify it the resulting behaviour is undefined.
.

The problem is NOT that memory was not allocated for the string (actually it was), but that the string resides in (probably) read-only memory, depending on how the compiler chooses to handle it. Its never advisable to try and modify string literals.

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

dear Ancient Dragon,
I moved pointer to file ending (line 28). I checked the problem you mentioned but be not wrong.

Yes I realized that, and I deleted my comment. Please re-read my post as it contains the correct solution.

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

you need to allocate one more byte than file size for the string's null-terminator, then add the null terminator after the read() operation.

char* data_block=new char[(int)size+1];

		file.seekg(0,ios.beg);

		file.read(data_block,size);
        data_block[size] = 0;
		file.close();
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You might want to put code guards in that header file to prevent the problem you describe.

#ifndef POINTS_H
#define POINTS_H
struct Points {
int xCoordinate; 
int yCoordinate; 
int zCoordinate;
}; // end of struct Points definition
#endif
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I'm not having any problems now -- and part of my problem was on my end, not yours, which was also fixed yesterday.

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

I heard she was eliminated from the competition already..

already?? She was 2nd place in the contest, which is now finished. She didn't faint because of that, she had a nervous breakdown because she couldn't take all the pressure of the public life she had acquired. Its the case of "be careful what you wish for" -- she wanted to be a star but I doubt she knew what she was getting herself into.

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

In my school a computer class was not for teaching computer science. But they made us play games on the computers.

Nothing wrong with that either -- there are lots of people who have never used a computer, so that type if class may be necessary to get them introduced to computers. We have those types of classes here in USA also.

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

GCC/g++ doesnt seem to have <sstring> , I used <sstream> instead.

Yes, sstring was an error and should have been sstream.

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

>>I have Turboc C++ ver 3.0

Don't use that crappy compiler because its too old. google for Code::Blocks or VC++ 2008 Express. Even Borland C++ Builder 5.0 would probably work.

I'm not going to write it as if this were 30+ years ago. Get into the 21st Century like everyone else on the planet.

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

Here is correction

float *save_array(int N)
{
    float *a;
    int counter=0;
    a = malloc(N * sizeof(*a)); // allocate memory for the array
    printf("Please type 10 float numbers :\n");
    for(counter = 0;counter < N;counter++)
   { 
         scanf("%f",&a[counter]);
    }
    return a; 
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Well, it's not irrelevant. Strings can be configured to handle large numbers.

For the purpose of the discussion in this thread the OP does not need to resort to writing string functions to work with those huge numbers -- the C language already has 64-bit integers which are more than suffient for the OPs use. If he needed something larger than what can be held in a 64-bit integer (such as the national debt of the USA government) then you are right about those string functions. I also think there are existing libraries that handle integers of unlimited size.

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

Will both you guys please stop fighting and try helping me out?

Agree.

but I guess my compiler is the culprit. Guys. - Let me know of a decent compiler and where to download it

You never did say what compiler you are using, or even what operating system (*nix, MS-Windows, or something else)

If MS-Windows, then google for Code::Blocks or for VC++ 2008 Express. Both are excellent compilers, and there are others too. Stay clear of Turbo C because its just simply too ancient.

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

I assume you want to pass the entire array to that function, then don't specify the array size. I'd also pass the element number that you want used in the sprintf() call.

void DisplayPoints(Point point[], int index){ 
    sprintf(xCoordinateText,"%.0f", point[index].xCoordinate);
    BasicFunctionsCode.render2DBitmapText(1080, 100, font, xCoordinateText);
} // end of DisplayPoints
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
for ( int i=0;i<row;i++ )
{
      if (custn == customers[i].custno)
     { 
<snip>
     }
     else
          errorMsg("Invalid Option");

}

It would appear the logic is wrong. Unless the very first item in the array is always the custn its looking for then the program will always display the error message.

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

>>for example if I enter the file name as "1234567" (only digits), It should create "0001234567_OP.txt"

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

int main()
{
    int num = 12345;
    string filename;
    stringstream str;
    str << num; // converts num to a string
    filename = "000";
    filename += str.str();
    filename += "_OP.txt"";

   ofstream out(filename.c_str());
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Any help ??

Yes, lots of it. But you first have to post the code you have tried.

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

DaniWeb is very sloooow today. Are you doing server maintenance -- again?

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

anyone? help please ! :(

sorry, I haven't done managed code.