tux4life 2,072 Postaholic

void createAlpha(char **&data, int elements); Sorry, but that is strictly a C++ feature, not C.

Sorry, I'm not familiar with C, you'll have to work with pointers in that case ...

tux4life 2,072 Postaholic

In console (Windows) the following code will do the job:

#include <iostream>

using namespace std;

void beep(void);

int main(void)
{
    beep();
    return 0;
}

void beep(void)
{
    char ascii = 7;
    cout << ascii;
}

You just have to send the ASCII 7 - code to the screen ...

tux4life 2,072 Postaholic

Did you mean something like this:

#include <stdio.h>
#include <stdlib.h>

#define NUM_OF_ELEMENTS 2

void createAlpha(char **&data, int elements);

int main()
{
    char ** alpha;

    createAlpha(alpha, NUM_OF_ELEMENTS);

    alpha[0] = (char*) 'H';
    alpha[1] = (char*) 'e';

    fprintf(stdout, "%c%c\n", alpha[0], alpha[1]);

    free(alpha);

    return 0;
}

void createAlpha(char **&data, int elements)
{
    for(int i = 0; i < elements; i++)
    {
        data[i] = (char*) calloc(elements, sizeof(char));
    }
}

???

tux4life 2,072 Postaholic

I have nothing against Linux, I'm using it on one of my pc's (CentOS -> ultra fast and stable) it's just the ideal OS if you want to program, but since I'm stuck with some old P3's, I think W2K is the best for them, it's definitely not XP (or Virusses Intruders Spyware Trojans and Adware, with the abbreviation of VISTA ;) )

tux4life 2,072 Postaholic

For such a simple game I wouldn't create a whole class, it's maybe only 30 lines or so ...

(the estimate above is just for a 'high/low game')

Sorry, I didn't see you meant a 'high/low card game' ...

So you can use classes if you want, you can also use normal functions, but don't put everything in your main function !!
I think inheritance is just overhead (in the code's length) in this case ...

tux4life 2,072 Postaholic

> What editor / compiler are you using?

I'm using Code::Blocks (not an editor but an IDE) with the MinGW compiler on Windows 2000 Professional (the best Operating System M$ ever made, has never crashed up until now and works smoothly)

For some people who ask themselves why I'm using W2K nowadays:

My reason of using Windows 2000 nowadays is:
-> 'If it ain't broken, don't fix it' -> I'm stuck with some old P3's
(I have a new(er) computer, which theoretically is better, but practically it's just badder)

What's your syntax highlighting like?
Check out the screenshots at: http://www.codeblocks.org/screenshots

tux4life 2,072 Postaholic

>Here is the shortest code for finding the fibo series
I think this code is shorter, don't you?

void Fib(int fmax)
{
    int i = 1, f[2]  = { 0, 1 };
    while (f[i] < fmax) {
        cout << f[!i] << '\n';
        f[i^=1] = *f + f[1];
    }
}

;)

The smallest and best optimized c++ code I ever saw for such a task !!!

tux4life 2,072 Postaholic

Here is the shortest code for finding the fibo series

I also found a short code (nearly exactly the same as yours):

#include <iostream>
using namespace std;

int main(void)
{
    int f0 = 0, f1 = 1, fn, repeat_times;
    cout << "Enter number of elements (the first two elements are always displayed): ";
    cin >> repeat_times;
    cout << endl << "Fibbonacci series: 0 1 ";
    for(int i = 0; i < repeat_times; i++)
    {
        fn = f0 + f1, f0 = f1, f1 = fn;
        cout << fn << " ";
    }
    cout << endl;
    return 0;
}
tux4life 2,072 Postaholic

Please do at least some effort to write some code ...
And when you're really stuck we'll help you ...

tux4life 2,072 Postaholic

For such a simple game I wouldn't create a whole class, it's maybe only 30 lines or so ...

In my opinion you really get benefit from classes when you've moderate - big C++ code ...

tux4life 2,072 Postaholic

...

Please Be specific.

...

Secondly, DO NOT USE VOID MAIN() http://cppdb.blogspot.com/2009/02/should-i-use-void-main-or-int-main-or.html

use #include<iostream> and #include<cstdio> Use a new compiler not a old one
(http://cppdb.blogspot.com/2008/10/why-you-shouldnt-use-you-use-old-c.html)

...

Totally agreed with that !!

tux4life 2,072 Postaholic

Q: What is a pointer?
A: A pointer 'points' to an address in your computer's memory ...

A pointer can not only point to a variable, but also to an object, a struct and much more other types of data ...

If you think in the following way: "A variable is stored in your computer's memory, but also functions, structs, unions, enumerations, objects, and so on are stored in your computer's memory, so a pointer can 'point' / 'refer' to this ... "

Hope this solves your problem !
(Together with Narue's explanation)

tux4life 2,072 Postaholic

Why are you writing ancient C++ code?
It's much easier to use the new C++ standard ...

I think scanf is more C-like than C++-like I would rather recommend using cin and cout for in- and output ...

tux4life 2,072 Postaholic

>What do you think cin and cout are?

==> Objects ;)

tux4life 2,072 Postaholic

how to create a w64 structure in c++?

That was his question, I don't understand it myself, but probably you and I won't ever need what he's asking for ...

tux4life 2,072 Postaholic

I wrote two simple C++-functions:
-> One wich raises a number x to the power of y ( apow(x, y); )
-> One which calculates the faculty of number x ( faculty(x); )

long long faculty(long x)
{
    long long y = 1;
    y = 1;

    for(int i = 1; i < (x+1); i++)
        y = y * i;

    return y;
}

long double apow(float x, int y)
{
    long double result = 1;
    if(y == 0)
        return result;

    if(y < 0)
    {
        y = -y;
        for(int i = 0; i < y; i++)
            result = result * x;
        return 1/result;
    }

    for(int i = 0; i < y; i++)
        result = result * x;

    return result;
}

And here's a full example where you can see these functions at work:

#include <iostream>

long long faculty(long x);
long double apow(float x, int y);

using namespace std;

int main(void)
{
    long double answer;
    int x = 1;

    cout << "6! = " << faculty(6) << endl;
    cout << "10^2 = " << apow(10, 2) << endl;
    cout << "10^-2 = " << apow(10, -2) << endl;
    cout << "10^0 = " << apow(10, 0) << endl;

    return 0;
}

long long faculty(long x)
{
    long long y = 1;
    y = 1;

    for(int i = 1; i < (x+1); i++)
        y = y * i;

    return y;
}

long double apow(float x, int y)
{
    long double result = 1;
    if(y == 0)
        return result;

    if(y < …
Rashakil Fol commented: How does that help the OP? -2
Salem commented: RashFool rep cancellation+++ +30
tux4life 2,072 Postaholic

Didn't know this but in C++ you can use pointers or references to achieve the same as you did ...

tux4life 2,072 Postaholic

What exactly do you want?
You should explain every detail to increase your chances of getting helped by forum members ...

tux4life 2,072 Postaholic

>but what's so wrong about it?
Destructors aren't called when you use exit. It's not a C++ friendly function.

You're absolutely right, but in this case it didn't make sense because I wasn't using any objects ...

tux4life 2,072 Postaholic

Don't stop C++ programs with exit() function (except on severy run-time errors).
Use return exit_code_value; in the main.

OK, agreed, but what's so wrong about it? I tought I only had to be careful with the 'abort()' instruction ...

tux4life 2,072 Postaholic

Maybe the following code will help you:

#include <iostream>

using namespace std;

int main(void)
{
    char answer;

    cout << "Do you want to run this program ? [Y][N] ";

    cin >> answer;

    if(answer == 'y' || answer == 'Y')
    {
        /* ... There has to be nothing here ...*/
        /* Only if answer is 'y' or 'Y' the program will run */
        /* Every other answer will cause the program not to run */
    } else {
        /* Don't run the program */
        cout << endl;
        exit(0);
    }

    /* Put your other instructions here */

    cout << endl;
    cout << "Running the program ..." << endl;

    return 0;
}

Hope this helps !

tux4life 2,072 Postaholic

The error is probably located where the function returns its value
(line 35 in the code I posted in the previous message) ...

tux4life 2,072 Postaholic

BUG REPORT!
Seems like your program doesn't want the following string: char str1[] = "test hello aatttttttttttttttttttttttttttttttttttttttttttttttt"; Here's the code:

#include <iostream>

using namespace std;

char *astrstr(const char *s1, const  char *s2)
{
    char *p = NULL;

    int x = astrlen(s1);
    int y = astrlen(s2);

    int o = 0, z = 0;
    for(int i = 0; i < x; i++)
    {
        for (int k = 0; k < y; k++)
        {
            if (s1[i] == s2[k])
            {
                o = 0;
                z = 0;
                for(int l = i; l < x; l++)
                {
                    if(s1[o+i] == s2[o])
                    {
                        z++;
                    }
                    o++;
                }


            }

            if (z == y)
            {
                return (char *)(s1+i);

            }
        }
    }
    return p;
}

int main(void)
{
    char str1[] = "test hello aatttttttttttttttttttttttttttttttttttttttttttttttt";
    char str2[] = "hello";

    cout << "C++ function: " << strstr(str1, str2) << endl;
    cout << "My function: " << astrstr(str1, str2) << endl;

    return 0;
}

The standard C++-function gives the right output, however your function's output is nothing ...

tux4life 2,072 Postaholic

Maybe you should try compiling with MinGW instead, your code is 100% working !!!

tux4life 2,072 Postaholic

With me your program just compiles and works perfectly, you're maybe using another compiler ...
You've luck you aren't running Win9x, otherwise you risk a system crash instead of a program crash ;) ...

tux4life 2,072 Postaholic

Hmm, this makes me thinking about an override somewhere in your program ...

What happens if you change:

int x=astrlen(s1);
int y=astrlen(s2);

To:

int x=astrlen(s1)-1;
int y=astrlen(s2)-1;

????

tux4life 2,072 Postaholic

Maybe you want to take a look at my code:

#include <iostream>

using namespace std;

void func(char **& ref_to_ptr); /* Function declaration */

int main(void)
{
    /* Declare the '2D Array' */
    char ** ptr = new char * [5];
    ptr[3] = new char[20];

    /* Put some data in the array */
    ptr[3] = "k";

    /* Print the first value on the screen */
    cout << "First value: " << ptr[3] << endl;

    /* Pass the array by reference to the function 'func()' */
    func(ptr);

    /* Again we print on the screen what's in the '2D Array' */
    cout << "Second value: " << ptr[3] << endl;

    /* Wait for the user to press ENTER */
    cin.get();

    /* Cleanup */
    delete[] ptr;

    /* Tell the Operating System that everything went well */
    return 0;
}

void func(char **& ref_to_ptr)
{
    /* This function demonstrates how to change the value of it's argument(s) */
    ref_to_ptr[3] = "tux4life";
}
tux4life 2,072 Postaholic

You can use them e.g. for a table of char-strings:

#include <iostream>

using namespace std;

int main(void)
{
    /* Declare the '2D Array' */
    char ** ptr = new char * [5];
    ptr[0] = new char[20];
    ptr[1] = new char[20];
    ptr[2] = new char[20];
    ptr[3] = new char[20];
    ptr[4] = new char[20];

    /* Put some data in the array */
    ptr[0] = "Hello ";
    ptr[1] = "wond";
    ptr[2] = "er";
    ptr[3] = "ful";
    ptr[4] = " world !!!";

    /* Print the array on the screen */
    for(int i = 0; i < 5; i++)
        cout << ptr[i];

    cout << endl;


    /* Cleanup */
    delete[] ptr;

    /* Wait for the user to press ENTER */
    cin.get();

    /* Tell the Operating System that everything went well */
    return 0;
}
tux4life 2,072 Postaholic

Now if I type in any word that begins with q or Q, it returns false.

Is there away to avoid this? Because I only want the letters q and Q to return false. Not words that begin with q or Q.

To avoid this problem you should just compare the two strings instead of comparing the first characters ...

Try something like this:

#include <iostream>

using namespace std;

int main(void)
{
    char input[51];
    cout << "Type something: ";
    cin.getline(input, 50);
    cout << endl;

    if(!strcmp(input, "q") || !strcmp(input, "Q"))
    {
        cout << "A \"q/Q\" was detected !!!" << endl;
    } else {
        cout << "You typed: " << input << endl;
    }

    cin.get();
    return 0;
}
tux4life 2,072 Postaholic

Maybe you should just try to call 'return <something>' in your code ...
After the return instruction no code is executed anymore so the function will go back to 'main()' (if the function is called from the 'main()' function)

P.S. : Can you please post us some code so we can take a look at what exactly you do mean?

tux4life 2,072 Postaholic

and gives a runtime error on the second one.....

With my code I don't get a runtime error on the second one ...
Success with your program !!!

P.S: Maybe you should post your code into a new thread and explain that there's a bug, but that you don't know how to fix it ...

tux4life 2,072 Postaholic

The following code is a working example of what you were trying to achieve:

#include <iostream>

using namespace std;

void addcustomer(char **& ref_to_ptr);

int main(void)
{
    char ** customers;

    addcustomer(customers);

    cout << "All the customer data from record 1 = ";

    /* Show Record 1 */
    for(int i = 0; i < 8; i++)
    {
        cout << customers[i] << " ";
    }

    cout << endl;

    delete[] customers;
    return 0;
}

void addcustomer(char **& c)
{
    int n = 0;
    const int ATTR = 8;
    const int MAX_STR_LEN = 25;

    cout << "-------- Add Customer Record --------" << endl << endl;
    cout << "Please Enter the number of records you want to Enter: ";
    cin >> n;
    cout << endl;

    delete[] c;
    c = new char * [(n*ATTR)];

    for(int i = 0; i < (n*ATTR); i++)
    {
        c[i] = new char[MAX_STR_LEN];
    }

    for (int i = 0; i < n; i++)
    {

        cout << "Please Enter Customer Name: ";
        cin.getline(c[i*ATTR], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer NIC Number: ";
        cin.getline(c[i*ATTR+1], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Telephone: Number ";
        cin.getline(c[i*ATTR+2], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Room Number: ";
        cin.getline(c[i*ATTR+3], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Entry Date and Time: ";
        cin.getline(c[i*ATTR+4], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Exit Date and Time: ";
        cin.getline(c[i*ATTR+5], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer Payments: ";
        cin.getline(c[i*ATTR+6], MAX_STR_LEN);
        cout << endl;

        cout << "Please Enter Customer …
arshad115 commented: thanks a lot for youy help! +1
tux4life 2,072 Postaholic

The following code illustrates what you asked for:

#include <iostream>

using namespace std;

void func(char **& ref_to_ptr); /* Function declaration */

int main(void)
{
    /* Declare the '2D Array' */
    char ** ptr = new char * [5];
    ptr[3] = new char[20];

    /* Put some data in the array */
    ptr[3] = "k";

    /* Print the first value on the screen */
    cout << "First value: " << ptr[3] << endl;

    /* Pass the array by reference to the function 'func()' */
    func(ptr);

    /* Again we print on the screen what's in the '2D Array' */
    cout << "Second value: " << ptr[3] << endl;

    /* Wait for the user to press ENTER */
    cin.get();

    /* Tell the Operating System that everything went well */
    return 0;
}

void func(char **& ref_to_ptr)
{
    /* This function demonstrates how to change the value of it's argument(s) */
    ref_to_ptr[3] = "tux4life";
}

In the above code we declare a '2d array' inside the main function, then we pass it to the function 'func()' which changes the value of it ...

Maybe it's also useful to read the following article about pointers and references: http://www.codeguru.com/cpp/cpp/cpp_mfc/pointers/article.php/c4089/

Hope this helps !

tux4life 2,072 Postaholic

The following program demonstrates the principle of passing an array to a function:

#include <iostream>

using namespace std;

void test(int array[]);
long arraysum(int array[], int el_in_arr);

int main(void)
{
    int numbers[] = {1, 2, 3, 4, 5, 6, 7};
    test(numbers);
    int sum = arraysum(numbers, sizeof(numbers)/sizeof(int)); /* this variable sum is another one than the one in arraysum() */
    cout << "The sum of all the elements in the array is: " << sum << endl;
    cin.get();
    return 0;
}

long arraysum(int array[], int el_in_arr)
{
    /* This function returns the sum of all the elements in the array
    passed as argument */

    long sum = 0;

    for(int i = 0; i < el_in_arr; i++)
        sum = sum + array[i];

    return sum;
}

void test(int array[])
{
    /* Change a value in the array */
    array[0] = 5;
}
tux4life 2,072 Postaholic

That's because you've declared 'c' in your function addCustomer() and not in main() ...

'c' is only accessible in function addCustomer(), when the execution of addCustomer() has finished, the array doesn't exist anymore ...

That's why it's better to declare the whole array in main and pass it to the function via a pointer or a reference ...

tux4life 2,072 Postaholic

Writing a Sudoku solver is a very interesting project, it's not as easy as it looks like I think...

Maybe the following link is useful for you: http://www.codeproject.com/KB/game/sudoku.aspx
(maybe just useful for the algorithm)

And this link might also be useful:
http://translate.google.com/translate?prev=hp&hl=en&u=http%3A%2F%2Fwww.keesmoerman.nl%2Fsudoku.html&sl=nl&tl=en
(translated from Dutch, you can download the source of it (in the source the comments are in English))

Have success with your Sudoku solver !

tux4life 2,072 Postaholic

I don't know much about C, but I think it's something like this:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    /* Create pointers */
    int * matrix_one;
    int * matrix_two;

    /* Dynamically allocate memory for our matrices */
    matrix_one = (int*) calloc(50, sizeof(int));
    matrix_two = (int*) calloc(50, sizeof(int));

    /* Free up the allocated memory */
    free(matrix_one);
    free(matrix_two);

    return 0;
}
tux4life 2,072 Postaholic

Definition (?) of a compiler:

-> A computer program which reads source code and outputs assembly code or executable code
-> A computer program that translates highlevel code into machine code
tux4life 2,072 Postaholic

so if i need to get input of 5 records,how would i know that the record no.2's name and all the other attributes?

By reading your posted code I assume you want to store 8 attributes (telephone number, name, etc.) per customer ...

So if you need 5 records, you declare an array with 40 (5*8) elements in it, please make sure you put the attributes always in the same order, otherwise it won't work...

e.g: If you want to read out record 2, you do the following: the first element of record 2 is located in the array at position 2*8 = 16, now you increment this value (16) by the number of the attribute and you have the data you want ...

BTW, Why do you need a two-dimensional array?
You can also do it with a one-dimensional ...

tux4life 2,072 Postaholic

arshad115, are you putting all the user input into a 2D array?

-> I can say only one thing about that: It will become a big mess ...

tux4life 2,072 Postaholic

I personally think it's much more simple to use an array of structs, and dynamically reallocate it ...
You don't need an array of pointers in that way...

tux4life 2,072 Postaholic

I also would like to advise you to put the data of every customer in a struct (you can create an array of structs) ...

A struct looks like this:

struct customer_struct
    {
        string name;
        string nic_number;
        string telephone_number;
        int room_number;
        string entry_date_time;
        string exit_date_time;
        string payments;
        string status;
    };

The following you have to do is:

. . .
    customer_struct customers;
    customers.name = "John Smit";
    customers.telephone_number = "0123-456-789";
    . . .
tux4life 2,072 Postaholic

I wrote a snippet of code wich can dynamically allocate an array
(! doesn't preserve the existing data !)

void redim(int *&ptr_to_data, int newdim)
{
	delete[] ptr_to_data;
	ptr_to_data = new int[newdim];
}

This is a full example where you see the redim() function working:

#include <iostream>

using namespace std;

void redim(int *&ptr_to_data, int newdim);

int main(void)
{
	int * testptr = new int[1]; // reserve some memory

	testptr[0] = 23;

	cout << testptr[0] << endl;

	redim(testptr, 5); // change the dimension

	testptr[4] = 203;

	cout << testptr[4] << endl;

	delete[] testptr;

	return 0;
}

void redim(int *&ptr_to_data, int newdim)
{
	delete[] ptr_to_data;
	ptr_to_data = new int[newdim];
}


REMARK: You have adapt the code to your needs, this example only shows you how to do this with a one-dimensional array ...

tux4life 2,072 Postaholic

-> You get input using 'cin' (e.g. : 'cin >> var;')

-> If you store your results you got from the user into an array declared in the function, it is only accessible by the function, not the whole program, you can declare a global array (NOT RECOMMENDED!) but it could work, you could also declare the array into your main() function and pass it via a reference / pointer to your function...

-> A 2-dimensional array is declared as follows:

[I]type[/I] [I]array[/I][[I]x[/I]][[I]y[/I]]; /* where 'x' and 'y' are the indexes of the array */
tux4life 2,072 Postaholic

I know there's also a seperate thread about C++ Books, but 'C++ Black Book' is a very beginner-friendly book, it explains nearly the whole C++ syntax in detail, step by step ...

But it might not be an appropriate choice for you because you only need to learn the basics...

The following site has also some C/C++ tutorials: http://www.cprogramming.com/tutorial.html#c++tutorial

tux4life 2,072 Postaholic

I like your way of thinking ;) , but in most cases the compiler also mentions where (on which line) the error is located...

Didn't the compiler give the line where there was a missing ')' ?

tux4life 2,072 Postaholic

Replace..

if(usrInput.c_str()[usrInput.length()-1]=='%')
...

with

if(usrInput[usrInput.length()-1] == '%')
...

Thank you for mentioning that, I didn't know that was possible...

tux4life 2,072 Postaholic

Maybe you should take a look at the following code:

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
    string usrInput;
    double dbl = 0;
    cout << "Enter a percent: ";

    getline(cin, usrInput);
    cout << endl;

    if(usrInput.c_str()[usrInput.length()-1] == '%')
    {
        usrInput.replace(usrInput.length()-1, 0, "");

        cout << usrInput << endl;

        dbl = atof(usrInput.c_str());
    } else {
        dbl = atof(usrInput.c_str());
    }

    cout << "The double is: " << dbl << endl;

    return 0;
}

Using Ancient Dragon's method:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main(void)
{
    string str;
    double dbl = 0;

    cout << "Type a number: ";
    cin >> str;
    cout << endl;
    stringstream ss(str);
    ss >> dbl;
    cout << "The number is: " << dbl;

    return 0;
}
tux4life 2,072 Postaholic

Have you already tried to move all your project's include files to the directory of your compiler's include files?
(Please make sure you don't overwrite or delete any existing compiler files !);)
Maybe not the best solution, but it's only to see if it works ... :?:

tux4life 2,072 Postaholic

The following code will do the job:

#include <iostream>

using namespace std;

int main(void)
{
        const int MAX_NUMS = 5;
        int value_array[MAX_NUMS] = {0}; // initialize to zero
        int value_pos_array[MAX_NUMS] = {0}; // initialize to zero
        int number_to_find;

        cout << "Enter " << MAX_NUMS << " values: ";

        for(int i = 0; i < MAX_NUMS; i++)
        {
            cin >> value_array[i];
        }

        cout << endl;

        /* Check user input */
        for(int i = 0; i < MAX_NUMS; i++)
        {
            if (value_array[i] == 0)
            {
                cout << "Not enough numbers entered !!!" << endl;
                exit(1);
            }
        }

        cout << "Enter a number to search: ";
        cin >> number_to_find;

        /* Count the position(s) of number_to_find */
        int count_match = 0;
        for(int i = 0; i < MAX_NUMS; i++)
        {
            if (value_array[i] == number_to_find)
            {
                value_pos_array[count_match] = i+1; // store the position of the number
                count_match++; // increment by one
            }
        }

        /* Display the result on the screen */
        if(count_match == 0)
        {
            // no numbers found
            cout << "There were no matching numbers !!!" << endl;
        } else {
            // matching numbers were found
            // display the positions of those numbers on the screen

            cout << "Found " << count_match << " match(es)" << endl;
            cout << "On the following positions: ";

            for(int i = 0; i < count_match; i++)
            {
                if (value_pos_array[i] != 0)
                {
                    cout << value_pos_array[i];
                    if(value_pos_array[i+1] != 0)
                    {
                        cout << ", ";
                    }
                }
            }
            cout << endl;
        }

        return 0;
}

Thanks to ArkM to …