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

Are you looking for c++ code or pseudocode?

declare three integers, one for age, one for sum of ages, and third for number of ages entered.

in a loop
    prompt for age
    update sum
    update counter
end of loop
average age is sum divided by counter
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

getline() will permit you to enter spaces in the string. If you don't want the spaces then use >> operator cin >> this->name; , but the problem with that is the >> operator will allow you to enter more characters than what this->name can hold.

>>would this lead to memory leaks or problems further down the line?
Probably if you fail to delete[] the memory.

In c++ it is better to use std::string instead of char* because you don't have to bother with memory allocation; the class will allocate memory as necessary.

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

if this->name is a pointer (i.e. char*) then you have to allocate space for it before copying the string. Just setting with = as in your example will not work.

Name::Name(char* nm){
    this->name = new char[strlen(nm)+1);
    strcpy(this->name,nm);
}

>>Ideally i would like to stay away from the use of strings and inputting via a for loop.
Why? Just for grins, then ok it might be a learning experience. But in practice, programs don't waste their time using a loop like that.

If you want to input the name via cin then you will have to set some sort of limit so that the program doesn't get data overflow

void addName()
{
   const int MAXNAMELEN = 80;
    this->name = new char[MAXNAMELEN];
    cin.getline( this->name, MAXNAMELEN);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Two things wrong with the if statement on lines 120 and 124
1) if statements must have ( and ). e.g. if( condition ) 2) = is an assignment operator, == is a boolean logic operator. You want to use == in if statements

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

Of course it can't be deleted because the program has it open. You have to close the file before attempting to delete it.

[edit]And what ^^^ said too.

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

Yes I could, but give it a try yourself. Its easy to do, just write the loop first and after the loop delete the array. If you wrote that code snippet to allocate the memory then I'm confident you can write the code to delete it.

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

delete them in the opposite order that they were allocated. use delete inside the loop, but delete[] to delete the array itself.

2) Not possible. You should create another method which can be called after the memory is allocated.

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

You can't really use a switch statement based on averages. Its better to use a series of if statements so that you can gest ranges of values

if( average > 90)
   return 'A';
else if( average > 80 )
   return 'B';
// etc etc
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you need to typecast the base class pointer into a derived class pointer before it can call functions unique to the derived class. Base class knows nothing about those functions. derived_class *pDerived = reinterpret_cast<derived_class*>(obj);

int main(void) {
  base_class *obj;

  obj = new derived_class (10);
  derived_class* pDerived = reinterpret_cast<derived_class*>(obj);     
  obj->func_1();
  obj->func_2();
  pDerived->func_3();
  pDerived->print_y();

  return 0;
}
C++NOOOB commented: Perfect! Solved my problem. +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is one way to do it -- use stringstream class

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

int main(int argc, char* argv[])
{
    int day = 20;
    int month = 1;
    int year = 8;
    stringstream str;
    str << setw(2) << setfill('0') << year << setw(2) << month << setw(2)<< day;
    cout << str.str() << "\n";
    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What do you mean by "it doesn't work"?

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

How do you know Sleep() doesn't work?? Recall the parameter is milliseconds -- there are 1000 milliseconds in one second.

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

You might as well delete the loops on lines 4-10 because that initialization is just undone (destroyed) by line 29.

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

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

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

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

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

Did you try deleting all object files and recompiling everything? If that doesn't work then post the offending source file.

NicAx64 commented: experienced can't be teached, it should obtained +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you are using MS-Windows then use win32 api console functions.

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

There are no standard C or C++ functions that will do what you want. Bu8t there are some non-standard functions in conio.h, if that has been implemented by your compiler. You could also probably use something like ncurses library, or other win32 api functions.

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

Function add() -- second parameter must also be passed by reference. You are just passing the pointer by value, which does nothing in main()'s copy of the pointer. Note the second parameter should have two stars, not one.

int add(int* index, int **a)
{

}

lint 49: The typecast is wrong so you might as well just remove it because malloc() does not need to be typecast in C programs.

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

1) you failed to read Aia's comment.

2) The printf() statement is incorrect. %s is for a charcter array, shape is not a character array. What you want os %c

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

>>if (length=breadth)

you are using the wrong operator -- use == boolean operator instead of = assignment operator.

>> shape= 'square';
Two problems with that line

  1. All strings (two or more characters) have to be enclosed in double quotes, such as "square"
  2. variable [shape] can only hold a single character, such just do this: shape = 's'; . If you want shape to contain the entire string then redefine it as char shape[15]; and use strcpy() to copy it strcpy(shape,"square");
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you are using a 32-bit compiler on either MS-Windows or *nix then forget those two functions because they are not implemented by any 32-bit/64-bit compiler. The operating system will not permit direct access to hardware or computer ports. You have to use normal operating system api functions. Those two functions were written for 16-bit MS-DOS which did permit programs direct access to hardware.

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

Not to be repeating myself. . but call of duty came out recently. .

I suppose that means playing a game is more important than doing homework.

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

>>The changes you mentioned have been made except for changing ! == to !=. For some reason, my compiler is not liking !=. I get the error "ISO C++ forbids comparison between pointer & integer". I'm using Cygwin, if that helps.


Your compiler is correct -- I was thinking names was an array of std::string, not characters. use strcmp() instead if( strcmp(names[i], "z") != 0)

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

line 7 and 8 of the read function: Line 8 is unnecessary because line 7 opens the file when the stream is declared.

line 12: if lastname never contains spaces then use use >> operator instead of getline().

line 13: doesn't make any sense. names is an array, but you are treating it as a simple string. And it is more common to use the != operator rather than ! and == as you have done. if( names[i] != "z") line 15: you are reading in 4 scores, but the array can only hold 3.

line 20: Where is sum initialized to 0? It needs to be done just before the loop starts on line 15.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
void swap(int* int1, int* int2)
{
    int temp = *int1;
    *int2 = *int1;
    *int1 = temp;
}


void sort(int int1, int int2, int int3)
{
if( int1 > int2)
{
   swap(&int1, &int2);
}
// now do something similar for the other integers
// Do NOT use else statements.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

better off replacing that fprintf() with c++ fstream class so that the compiler won't complain so much.

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

Why did you use fgets() instead of fscanf()? fgets() gets the entire line without regard to spaces, which is not what you want. Look at the simple code I posted, it does what you need. All you have to do in that loop is add the word to the linked list or increment the word's counter if its already in the list. There is no need for that is_separator function because fscanf() will take care of that for you.

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

Its a lot easier to sort them if you use an array of 3 integers instead of 3 individual variables.

int arry[3] = {0};
for(int i = 0; i < 3; i++)
{
   cout << "Enter an integer\n";
   cin >> arry[i];
}
// now you can easily sort the array using one of the 
// many sorting algorithms.  The bubble sort is the easiest
// to code.  Google for it and you will find it.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

@twomers

"The #include thing only works on compile time. Not run time" - so you mean the binary will have all the contents of the the included file???

Yes. Its not really all that big a deal since the program has reserved space for the data anyway. Even though you have the arrays declared in if statements the compiler will allocate memory for all of them at the same time somewhere in global memory (since they are declared static). The if statements just limit the scope of those arrays.

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

>>The program is supposed to take in a file provided through standard input
That means get the name of the file from standard input, not the lines of the file. You have to open the file and read each word, something like this:

int main(int argc, char* argv[])
{
   FILE* fp = fopen( argv[1], "r"); // open the text file
   char buf[255];
   while( fscanf(fp,"%s",buf) > 0) // get a word
   {
    // populate the linked list with this word
   }
}
fclose(fp);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Modern 32-bit compilers to not support that header file because it was intended for 16-bit MS-DOS 6.X and earlier operating systems. The alternative is to use win32 api functions OpenFile() to open a comm port, ReadFile() and WriteFile() to read/write to it. MSDN has a large article about serial communications.

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

@gerard4143 :
@Dave : The first example is working, the structure gets populated with values in the inc file.

Yes it does work.

Ok, if its not possible then is there any other way to populate this structure using the inc files without using #include?

Read the file into the array just like it was any other normal text file.

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

>>Is there anyway to achieve this?
No because the #inc lude directive is processed at compile time, not runtime.

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

In that case go back to the code you originally posted here. inportb() only takes one parameter. It returns the byte read.

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

Does it use memory models, such as small, medium, compact and large? If yes, then it is a 16-bit compiler.

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

Yes, the 0x03f8 is the com port address for MS-DOS 6.X operating system. If you read through the links I posted you will find examples how to open the com port.

Are you using the 16-bit version of TurboC++ or the newer 32-bit version ? You can not use win32 api functions from the old 16-bit version of that compiler. My recommendation is to ditch that compiler and get a new one, such as Code::Blocks or VC++ 2008 Express, both are free.

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

you never implemented the class constructor.

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

The problem is that you are leaving the '\n' in the input buffer before calling getline(). call ignore() just before getline()

if(savedGame.is_open())
		{
			savedGame >> stamina;
			savedGame >> health;
			savedGame >> mana;
			savedGame >> level;
			savedGame >> strength;
			savedGame >> constitution;
			savedGame >> dexterity;
			savedGame >> newGame;
            savedGame.ignore(1000,'\n');
			getline(savedGame, weaponName);
			savedGame >> attack;
			savedGame >> strReq;
			savedGame >> dexReq;
            savedGame.ignore(1000,'\n');
			getline(savedGame, armorName);
			savedGame >> defense;
			savedGame >> strReq2;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don ot think erase() would do the trick.. why not use strtok()?

Bad idea. strtok() will crash the program you posted because it will attempt to change the string literal.

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

>> How to compare file creation date and current system date

I would probably first call GetSystemTimeAsFileTime() to get the current system time into a FILETIME structure. Then just compare the two FILETIME structs.

It is not recommended that you add and subtract values from the FILETIME structure to obtain relative times. Instead, you should copy the low- and high-order parts of the file time to a ULARGE_INTEGER structure, perform 64-bit arithmetic on the QuadPart member, and copy the LowPart and HighPart members into the FILETIME structure.

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

you want to use the substr() method, not erase(). And delete line 8 because length isn't needed (unless you want to use it somewhere else).

string type = temp.substr(0, pos);
string value = temp.substr(pos+1);
AdRock commented: Exactly what i needed +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you can not use inport and outpout with any 32-bit or 64-bit compiler. Those functions are no longer supported by the operating system. On MS-Windows you will have to use win32 api functions, starting with OpenFile() to open the com port, ReadFile() to read from it, and WriteFile() to write to it. Here is a complete list of communications functions.

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

1) line 15: you need to check of strtok() returns NULL.

2) line 19: memory leak. You allocated a character on line 17 then toss it away at line 19.

3) line 22: temp has only been allocated to hold one character, yet you are attempting to copy an entire string into it. This is probably what is causing your program to seg fault.

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

why the pointers?

int function(user* users, int id, char name[], char surname[]){
strcpy(users[id].name,name);
strcpy(users[id].last,surname);
users[id].id=id2;
}

And in main() you need to initialize the strings and int before passing them on to that function. If you don't then your program will just be so much crap.


It will also make is much more clearer that users is an array of user structures instead of a pointer to one structure if you code this: int function(user users[] ...