tux4life 2,072 Postaholic

Thank you for replying!

I'll rewrite my code, I forgot about 'cin' (that it also can read normal numbers, that's why I was using chars[] etc ...)

I think with the knowledge you provided me, my problem is as good as solved...

tux4life

tux4life 2,072 Postaholic

Maybe you should try doing a Windows XP repair installation ...
Your data is left intact...

Maybe you also want to backup your data first, use a linux live cd for that purpose...

tux4life 2,072 Postaholic

Hello,

My problem is that the program always gives the same output:

There were no matching numbers !!!

The source of my program looks as follows:

#include <iostream>

using namespace std;

int main(void)
{
        const int MAX_CHARS = 500;
        const int MAX_NUMS = 5;
        const int MAX_NUM_CHARS = 25;

        char values_input[MAX_CHARS];
        char number_to_find[MAX_NUM_CHARS];
        char num[MAX_NUM_CHARS];

        char chr_digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\0'};
        int value_array[MAX_NUMS] = {0}; // initialize to zero
        int value_pos_array[MAX_NUMS] = {0}; // initialize to zero

        cout << "Enter " << MAX_NUMS << " values: ";
        cin.getline(values_input, MAX_CHARS);
        cout << endl;

        cout << "Enter a number to search: ";
        cin.getline (number_to_find, MAX_NUM_CHARS);

        /* Save the user input in an array */ // I think the error is in this part
        for(int i = 0; i < MAX_CHARS; i++)
        {
            for(int j = 0; j < 10; j++)
            {
                if (values_input[i] == chr_digits[j])
                {
                    // we read a normal number
                    // add the character to the num c-string
                    num[j] = values_input[i];
                } else if (values_input[i] == ' '){
                    // we read a space
                    // store the final number and reset num
                    value_array[j] = atoi(num);
                    //num = '';
                } else {
                    // we read an 'unknown' character
                }
            }
        }

        /* Count the position(s) of number_to_find */
        int count_match = 0;
        for(int i = 0; i < MAX_NUMS; i++)
        {
            if (value_array[i] == atoi(number_to_find))
            {
                count_match++; // increment by one
                value_pos_array[i] = i; // store the position of the number …
tux4life 2,072 Postaholic

You have a internet connection. So go and get "Decent Book"
Be with the Standard C++ and just pour all that crap on your answer sheet in the examination , what your teacher expects from you.

Totally agreed with that:$, it's better to learn the new C++ standard instead of the old.

BTW, The code Narue provided you with is 100% standard C++ code, if that old crappy compiler can't handle this, you should quickly get a new one, such as the free Borland compiler (or MinGW) (as already mentioned, you've internet access)...

tux4life 2,072 Postaholic

I'm just doing what djextreme5 advised you:

You can make a variable and put it as the last line in your loop and output the variable that you created AFTER the loop has finished.

Try the following code:

#include <iostream>
using namespace std;

int main(void)
{
    int i;
    
    for(i = 0; i<10; i++)
    {
        /* This loop is repeated 10 times */
    }
    
    cout << "Times the loop has been repeated: " << i << endl;
    
    return 0;
}
tux4life 2,072 Postaholic

Maybe you should take a look at the following link: http://msdn.microsoft.com/en-us/library/et4zwx34(VS.71).aspx

Can you also post all the includes you did in word.cpp?
Can you also list the contents of your 'c:\data\dev\cms\3.0.0.0\code\word\src' ?

tux4life 2,072 Postaholic

Sorry for the late replying, but thank you all for your help...
I'm using MinGW and Code::Blocks now, works like a charm!

tux4life 2,072 Postaholic

What you need is a good book about C++ (and you also have to read it, otherwise it won't be a good practice).

Please check the following thread about C++ books: http://www.daniweb.com/forums/thread70096.html

I personally can recommend the "C++ Black Book", one of the best C++ books ever written, if you can get your hands on a copy of it ...

I tried to interpret what exactly you were trying to do and I came up with the following code:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string getFileName(void);
void dispData(string filename);

int main(void)
{
    string filename; // create variable to hold the filename

    filename = getFileName(); // get the filename from the user

    dispData(filename);

    cin.get();
    return 0; // tell the operating system that everything went well
}

string getFileName(void)
{
    /* Get the filename from the user */

    string filename; // create variable to hold the filename

    cout << "Enter the file name: ";

    getline(cin, filename); // store the filename

    cout << endl; // send a newline to the screen

    return filename; // return the filename
}

void dispData(string filename)
{
    /* Get the data from the file and display it on the screen */
    /* Caution: Please make sure that there are only numbers in the file !!! */
    /*          Otherwise the program will do strange! */

    ifstream file; // create an ifstream object

    file.open(filename.c_str()); // open the file for input

    if(!file.is_open())
    {
        // there was an error while opening the …
tux4life 2,072 Postaholic

I guess he's using a Microsoft compiler, maybe you should try: '#include <time.h>' instead...
Or maybe you should use MinGW to compile this code snippet, as you said it's only for a test...

tux4life 2,072 Postaholic

Hmm, seems like it can't find the file 'sys/time.h'

What IDE and compiler are you using?

(With me this program just compiles/runs perfectly, I'm using the latest MinGW compiler together with the latest Code::Blocks IDE)

tux4life 2,072 Postaholic

The latest and greatest free compiler is a part of Borland's Turbo C++ Explorer project.

Is that the exactly same C++ compiler used in Turbo C++ Explorer Professional?
Is it really true that it's * fully * compatible with the ANSI/ISO C++ standard?

tux4life 2,072 Postaholic

Me >Have you ever seen the best free IDE - - MS VS 2008 Express?
You>>Ok, agreed with that Microsoft crap...
??? ;)

I meant that I agree with your statement about it was heavier than the Code::Blocks IDE...

tux4life 2,072 Postaholic

Ok, agreed with that Microsoft crap but if you use an old(er) IDE (such as Dev-C++) with the newest MinGW compiler, does it in that case make sense if you use Dev-C++ or Code::Blocks, I don't think so, please tell me if I'm wrong...

With that Code::Blocks is too heavy I meant that it has too much options I don't use, I don't like such bloated user interfaces (this applies also to the M$ ones), sorry for that...

tux4life 2,072 Postaholic

While searching the internet I always come across several posts where they're saying that Bloodshed Dev-C++ isn't for advanced use (rather for newbies), why are they saying that?
Is MinGW not an advanced compiler? Or is it just because they aren't developping Dev-C++ anymore...
I know there's Code::Blocks, but I just find the program too heavy...

Can someone please give his opinion about the free available borland compiler, is it good to use nowadays?

Thanks in advance!

tux4life 2,072 Postaholic

First of all a few recommendations:

- Use '#include <iostream>' instead of '#include <iostream.h>' unless your compiler doesn't support it

- Your main function should return a value: for example put 'return 0;' at the end of the main function

Ok, now we're taking a look at what exactly you did wrong...

-> You declared an array with 6 elements: that means you've index 0 until 5 and index 6 doesn't exist, that's just an override (you're overwriting memory)

-> In the first for-loop the condition you mentioned was: 'a <= NUM_DAYS', you should change it to 'a <= NUM_DAYS-1', you should do the same in the second for-loop
(You hard-coded the value 6?)

-> If we interpret the the for-loop you wrote in your code:

for (a = 0; a <= 6; ++a)
{
    if (max < sales[a]) max = sales[a];
    else if (min > sales[a]) min = sales[a];
}

The max value works, but the min value not, why?
Well, in the beginning of your program you did the following:

double min = 0;
double max = 0;

To let the code work, you should set the max value as low as possible (zero) and the min value as high as possible (Maybe you say: how do I know the value min has to get assigned?)

Simply look at the following code:

for (a = 0; a <= NUM_DAYS-1; a++)
 {
     if (max < sales[a])
       { …
tux4life 2,072 Postaholic

Maybe you should try the following code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
system("cls");

double distance, mph, hours;

//Get the data
cout << " What is the speed of the vehicle in MPH? ";
cin >> mph;
cout << " How many hours has it traveled? ";
cin >> hours;

cout << endl;

//Display the heading
cout << " Hours\t\tDistance Traveled " << endl;
cout << " ---------------------------------- " << endl << endl;

//Input validation
for (int i = 1; i <= hours; i++)
{
if (mph < 0 && hours < 0)
{
cout << " Enter a positive number for mph and a value more than 1 for hours. \n";
}
else
cout << " " << i << "\t\t" << (i * mph) << endl;
}
return 0;
}

Hope this helps...

tux4life 2,072 Postaholic

Thank you for the link, that's exactly what I was looking for!

Ok, the last bug is fixed (I hope so)...

Here's the source for the last (?) time:

#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];
}
Salem commented: Looks good, and well done on the code tags. +29
tux4life 2,072 Postaholic

First of all, thank you for replying so quickly...

Ok, I've changed my code and actually it compiles with the Borland compiler... :)
But could you please take a look at it to ensure the code below is correct?

#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[5] = 203;
	
	cout << testptr[5] << endl;
	
	delete[] testptr;
	
	return 0;
}

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

Thanks.

tux4life 2,072 Postaholic

I'm fairly new to C++ programming and I would like to ask you a (maybe strange) question...

The code below compiles correctly with both:

- the latest MinGW-compiler
- Borland C++ Builder Compiler 5.5
(from the free Command Line tools)

However if I run the program (after compiling with the Borland Compiler I get a Windows error message: "... has encountered a problem and needs to close. We are sorry for the inconvenience."

This problem doesn't seem to appear while using the MinGW-compiler...

This is the output of the Borland Compiler:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
test.cpp:
Warning W8004 test.cpp 29: 'ptr_to_data' is assigned a value that is never used
in function redim(int *,int)
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Has this to do with a compiler switch I need to pass or isn't it recommended using this good old Borland compiler nowadays??

The code snippet below is the code I'm working with:

#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[1] = 23;
	
	cout << testptr[1] << endl;
	
	redim(testptr, 5); // change the dimension
	
	testptr[5] = 203;
	
	cout << testptr[5] << endl;
	
	delete[] testptr; /* If you remove this instruction ('delete[] testptr;') and compile this source with Borland, the program doesn't crash, it shuts down properly, but it doesn't clean up the allocated memory ... */ …