Doughnuts 0 Newbie Poster

My assignment is to make a program that can resize a dynamically allocated array. But when I delete a value, the program crashes (memory leak?). Here's my code.

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <limits>
using namespace std;

typedef char* charPtr;
typedef charPtr* strPtr;

strPtr delEntry(strPtr a, int &size, charPtr str);
charPtr getStr();
void printMenu();
char getch();
void resize(strPtr &a, int &size, int new_size);
void delStr(strPtr a, int pos, int size);
int search(strPtr a, int size, charPtr str);

int main() {
    strPtr a;
    char ch;
    charPtr str;
    int size = 5;
    a = new charPtr[size];

    str = new char[80];
    for (int i = 0; i < size; i++)
        a[i] = new char[80];

    cout << "Enter five names." << endl;
    for (int i = 0; i < size; i++)
        a[i] = getStr();

    cout << endl << "------------" << endl << endl;
    printMenu();
    cout << endl;

    ch = getch();

    while (ch != '4') {
        switch (ch) {
            case '1':
                cout << endl << "Enter string to add." << endl;
                str = getStr();
                resize(a, size, size + 1);
                a[size - 1] = str;
                break;
            case '2':
                cout << endl << "Enter string to delete." << endl;
                str = getStr();
                delEntry(a, size, str);
                break;
            case '3':
                cout << endl;
                for (int i = 0; i < size; i++)
                    cout << a[i] << endl;
            case '4':
                break;
            default:
                cout << endl << "Invalid choice.";
        }
        cout << endl << "-------------------" << endl;
        printMenu();
        ch = getch();
    }
}

charPtr getStr() {
    charPtr str; …
Doughnuts 0 Newbie Poster

Which edition of visual studio 2008 do you have? I think one of them includes a program called ".NET Obfuscator" or something like that.

EDIT: It's called Dotfuscator.

"Dotfuscator Community Edition does only very limited obfuscation and is included in Microsoft's Visual Studio.[3] By registering for free, users can have the Enhanced Edition, which includes a better integration into Visual Studio, in addition to more obfuscation features. However, the Community Edition and the Enhanced Community Edition are designed for hobbyists, not professionals, because they lack most of the features. Furthermore, the license limits use to the work products of a single person"

Doughnuts 0 Newbie Poster

Thanks!

n1_len > n2_len ? n1_len : n2_len

Can you explain this part please?

EDIT: Never mind, found out.

Doughnuts 0 Newbie Poster

I am trying to add two numerical char arrays such as:

"124" and "589"

I wrote functions to reverse the string and add the strings, and it works for strings like "123" and "456", but when the last digits are greater than 10, I don't know how to carry it, since the string is in reverse. Here's my code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

char* addNum(char* n1, char* n2, int max_size);

void reverseStr(char* str);
bool isEven(int i);
void flipChar(char &ch1, char &ch2);

int main() 
{
    char *n1, *n2, ch, *res;
    int max_size;
    
    cout << "Maximum number of digits: ";
    cin >> max_size;

    max_size += 2;
    
    n1 = new char[max_size];
    n2 = new char[max_size];
    
    while ((ch = cin.get()) != '\n' && ch != -1);
    
    cout << "Enter first number: ";
    cin.getline(n1, max_size);
    
    while ((ch = cin.get()) != '\n' && ch != -1);
    
    cout << "Enter second number: ";
    cin.getline(n2, max_size);
    
    res = addNum(n1, n2, max_size);
    
    cout << "Result: ";

    cout << res;
    
    return 0;
}
 
char* addNum(char* n1, char* n2, int max_size)
{
    int max, greater_len, n, carry, i;
    char *res;
    
    carry = 0;
    res = new char[max_size + 3];
    
    if (strlen(n1) > strlen(n2))
    {
        max = strlen(n2); 
        greater_len = strlen(n1) - strlen(n2);
    }
    else if (strlen(n1) < strlen(n2))
    {
        max = strlen(n1);
        greater_len = strlen(n2) - strlen(n1);
    }
    else
    {
        max = strlen(n1);
        greater_len = 0;
    }

    
    reverseStr(n1);
    reverseStr(n2);
    
    for (i = 0; i < max; i++)
    {
        n = (n1[i] - '0') + …
Doughnuts 0 Newbie Poster

You should never do input/output in a function, unless it is an input/output function. Otherwise, your sort function is fine. Here is my sort function:

void Sort(int Array[], int NumUsed)
{
    int temp, i, j;
    
    for (i = 0; i < NumUsed - 1; i++)
    {
        int Smallest = i;
        
        for (j = i + 1; j < NumUsed; j++)
        {
            if (Array[j] > Array[i])
            {
                Smallest = j;    
            }
        }
        
        temp = Array[i];
        Array[i] = Array[j];
        Array[j] = temp;
    }    
}
Doughnuts 0 Newbie Poster

Sorry for the title, i messed up...

Doughnuts 0 Newbie Poster

Hi everybody, I have a problem with output files. I have installed Code::Blocks and MinGW, replacing my old Borland compiler. I am trying to make a program that uses arrays and files:

#include <iostream>
#include <fstream.h>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include <cassert>

using namespace std;

void Save(ofstream& f, int Array[], int NumUsed, char fileName[], int Cursor);
void Load(ifstream& f, ofstream& g, int Array[], int NumUsed, int& Cursor, char fileName[]);
void Insert(int Array[], int& NumUsed, int& Position, int Num);
void Delete(int Array[], int& NumUsed, int Position);
void Sort(int Array[], int NumUsed, bool Ascending = false);
int Search(int Array[], int NumUsed, int Num);
int SmallestIndex(int Array, int NumUsed, int Index);
string getBoolString(bool B);
void displayArray(int Array[100], int numUsed, int Cursor);

int main()
{
	char fileName[27] = "Num.txt", ch;
	ifstream Input;
	ofstream Output;
	int numUsed = 0, Cursor = 0;
	int Array[100] = {-1};
	bool Circulating = false, Descending = true;

	Load(Input, Output, Array, numUsed, Cursor, fileName);

	while (true)
	{
      clrscr();

		cout << "[F]ilename   [I]nsert   [S]earch   [D]elete   S[o]rt   [C]ircular? "
				<< getBoolString(Circulating)
				<< "   Descending? "
				<< getBoolString(Descending)
				<< "   E[x]it"
				<< endl << endl;

		cout << "{";

		while (Array[numUsed] != -1)
		{
			numUsed++;
		}

        displayArray(Array, numUsed, Cursor);

      cout << "}" << endl << endl;
		ch = getch();
		switch (toupper(ch))
		{
		case 'C':    //Circulating?
			if (Circulating)
				Circulating = false;
			else
				Circulating = true;
			break;
		case 0:    //Special key pressed
			ch = getch();
			switch (ch)
			{
			case 'P':     //Down
				Descending = true;
				break;
			case 'H':     //Up
				Descending = false;
				break;
			case …
Doughnuts 0 Newbie Poster

Thanks. How do i use WinMain?

Doughnuts 0 Newbie Poster

Oops, wrong.

Doughnuts 0 Newbie Poster

strfilename.data() returns a pointer. Instead, use strcpy and c_str to convert to c string.

Doughnuts 0 Newbie Poster
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    int x, y;
    
    if (WM_LBUTTONDOWN)
    {
        x = LOWORD(LPARAM);
        y = HIWORD(LPARAM);
    }
}
Doughnuts 0 Newbie Poster

Bump!

Doughnuts 0 Newbie Poster

Does the compiler support the append function?

Doughnuts 0 Newbie Poster

Hello, I am trying to get the coordinates of the cursor when the user left clicks the mouse. I read that you can use this:

if (WM_LBUTTONDOWN)
{
     int x = LOWORD( LPARAM );
     int y = HIWORD( LPARAM );
}

However, when i compile, it gives me this error:
expected primary-expression before ')' token

I have included <windows.h> (don't really know if i need to), and it still doesn't work. I am using Code::Blocks and MinGW. Any ideas?:?:

Doughnuts 0 Newbie Poster

never mind, it works now!

Doughnuts 0 Newbie Poster

Hi, I am trying to use strings, and even a simple program like this doesn't work.

#include <iostream>
#include <string>

int main()
{
	string Hello;
   Hello = "Hello world!";
   cout << Hello;
   cin.get();
}

I am using Borland C++ 5.0, and it gives me this error.
"String does not name a type."

Help?

~Doughnuts

Doughnuts 0 Newbie Poster

Thanks, guys! It worked!

~Mr. Doughnuts

Doughnuts 0 Newbie Poster

Hello all, I am working on an assignment and came across a problem.
The assignment is to take a file with integers and calculate the average. Here is my code:

#include <iostream>
#include <fstream>
#include <stdlib>
#include <conio.h>

double calculateAverage(double Number, double Sum);
void trimSpaces(char Filename[]);

int main()
{
	ifstream InputStream;
   double Numbers = 0;              // Number of Numbers
   double Total = 0;                // Sum of all numbers
   double n;                        // Temp variable
   double Average;
	char FileName[30];

   cout << "Enter file name. ";
   cin >> FileName;

   InputStream.open(FileName);
	if (InputStream.fail())
   {
   	cout << "Error opening file.";    // Error
		getch();
      exit(1);
   }

   while (!InputStream.eof())
   {
   
      InputStream >> n;
      Total += n;
   
      cout << "\nCurrent sum: " << Total;
      Numbers++;
   }

	Average = calculateAverage(Numbers, Total);

   cout << "\n\nAverage: " << Average;

	getch();
}

double calculateAverage(double Number, double Sum)
{
   return (Number * 1.0) / Sum;
}

The file I am using contains, "1 5 7 2 5", so the answer should be 4. Instead, the program adds the last number "5" twice and gives me a totally different answer. Any ideas?

Doughnuts 0 Newbie Poster

Wow, that is smaller, masterful, flawless code. :|
Thanks, firstperson !!!

~mr. doughnuts

Doughnuts 0 Newbie Poster

firstperson, thanks for the quick reply.
What is a bool function? :)

~Mr doughnuts

Doughnuts 0 Newbie Poster

Hello, I am a newbie a C++. I was doing a program where you calculate prime numbers, and it prints prime and non-prime numbers. Here is my code,

#include <iostream>
#include <conio.h>
using namespace std;

main()
{
    int n = 1;
    int i = 2;
    bool IsPrime;
    int Max;
    int x = 0;
    
    cout << "Maximum number: ";
    cin >> Max;
    
    while (x < Max)
    {
        IsPrime = true;
    
        while ((i < n) &&)
        {
            if (n % i == 0) 
            {
                // Is not prime
                IsPrime = false;
                
            }
            else
            {
                // Is prime
                i++;
                
            }
        }
        
        if (IsPrime)
        {
            cout << "Prime: " << n << endl;
        }
        
        n++;
        x++;
    }

    getch();
}

Thanks,
~mr. doughnuts