Sci@phy 97 Posting Whiz in Training

It says

If the space cannot be allocated, the object remains unchanged.

If that was your question?

Furthermore, read this, I think it should help you: realloc() info

Bottom point is, you can easily determine if memory has been copied to another place:

//ptr is already allocated with malloc or calloc
Type *temp;
int new_size = 1000*sizeof(Type);

temp = realloc(ptr, new_size);
if (temp == ptr)  
       printf ("Memory hasn't been moved\n");
else{
       printf ("Memory has been moved to another location\n"); 
       ptr = temp;
}
//do your buisness with ptr
Sci@phy 97 Posting Whiz in Training

I would advise you to look at more basic things first, you are jumping ahead.
File manipulation is learned well after loops, arrays and string manipulations... it's not without reason.

Sci@phy 97 Posting Whiz in Training

What's IDE?
I'm using Dev-C++ (version 4.9.9.2 if it helps...)

That's IDE. :)
IDE stands for integrated development environment.
Best to describe it as a compiler and a lot of useful stuff that comes with it, almost always including specific text editor.

Sci@phy 97 Posting Whiz in Training

ah thanks for pointing that out, i was forgetting there are characters that can cause eof to return true!

Chris

Furthermore, even if you reach end of file, eof won't be set to true until you try to read after the EOF. That will cause this kind of error:
FILE.txt
My
name
is
//end

//screen output
My
name
is
is
//end

Sci@phy 97 Posting Whiz in Training

The while loop won't work properly, eof is unsafe to use in looping.
This is better:

while (getline(locationtemp, line)){
         numbers[x++] = strtod(line);
}
Freaky_Chris commented: Thanks, always wanting to know new things about stability +1
Sci@phy 97 Posting Whiz in Training

Just use locationtemp as you would use cin.
Of course, that is if your file contains only numbers.

Sci@phy 97 Posting Whiz in Training

What is numberObs?
And I don't get what do you want exactly?

Sci@phy 97 Posting Whiz in Training

If you want to determine n-th digit (from the back, so second digit in 1234 is 3) in a number, you do this:
a = number%(10^n) (now your digit is in first place)
digit = a/(10^(n-1)) (now everything is stripped except your digit)

That's the algorithm

Sci@phy 97 Posting Whiz in Training

Thank you for your answers :)

Sci@phy 97 Posting Whiz in Training

Hi the code u posted is C code and not C++.

So u would get better help and more replies if u would post it in the right section.

Why do you think it is C++? :|

Sci@phy 97 Posting Whiz in Training

Hi gunjan,

Can you please tell me what is the difference between void function(int*d); and void function(int &d); both above are function declarations and both we do the changes to the original variable passed to the function. Then what is the exact difference.

Thanks in advance

The first one is "passing arguments to pointers". In that way, you can change original variable!
Second one is "passing arguments as references".
You cannot do that in C, C always passes arguments by value (yes, even if you use pointers, but then it passes pointer as value, not original value pointed by pointer :) )

So, the first one is ok in C and C++
Second one is only C++

devnar commented: Nicely put. :) +1
Sci@phy 97 Posting Whiz in Training

@Narue
I see you are using goto .

And I know programmers don't like to use goto, so if I may ask: why? :)
Did you just use it because it's fastest to write code like that?
Or would you still use it because it has more positive than negative sideeffects in this code?

Sci@phy 97 Posting Whiz in Training

If you'd be so kind to narrow down your problem to a small piece of readable code, please :)

Sci@phy 97 Posting Whiz in Training

You have to pass variables as an argument.
If you want to pass one argument of type char to a function that returns bool you declare function: bool Foo(char a_char) And you call that function like (let's say you already have char letter inside main): Foo(letter); BUT, if you want to catch (to see) what your function RETURNS, then write this: bool fnc_returned = Foo(letter); I hope you won't have a problem implementing this in your code. Have fun :)

Sci@phy 97 Posting Whiz in Training

int a[m][m]
int *ptr = a;
int **ptr = ptr[m];

This doesn't make any sense. If you have to assign dynamic memory, you cannot type int a[m][m]; .

Yes, you have to use pointers. Don't worry, they're not too scary.
And using 2D array of pointers is a little itchy.

First, you have to create pointer to array of pointers of size m:

int** array = new int*[m];

Now you have "outer shell, but you need for each pointer-to-pointer to make m new pointers to int:

for (int i = 0; i < m; i++)
      array[i] = new int[m];

And that's it. After that you can use it just like array: array[i][j]; etc.

Hope to help :)

Sci@phy 97 Posting Whiz in Training

Yes you are trying to ignore.
"cin>>something" is really weird function.
When you write anything in you input, and press enter, then everything is sent into input buffer, but with addition of ENTER sign ('\n').
cin>>option takes one character, but leaves '\n' inside, so when another cin>> tries to read something, it just picks up '\n'.
With cin.ignore() you ignore that '\n'

Sci@phy 97 Posting Whiz in Training

Well, simply instead of using cout use file_out.

Sci@phy 97 Posting Whiz in Training

And where exactly are you outputing your data?
cout<< will output it to screen

Sci@phy 97 Posting Whiz in Training

As far as I see here it should be: notepad = FindWindow(NULL, "Notepad");

Sci@phy 97 Posting Whiz in Training

for(j=10;j>=0;j--)
{
cout<<kavi;
}

Do you expect your array to be size 10 always?

This works, try to see why:

char kavi[]={"my shiny sword"};

cout<<kavi<<endl;

cout<<"now reversing the word"<<endl;

int i=-1;
while(kavi[++i] != '\0');
i--;
cout<<"Length is "<<i<<endl;

for(int j=i; j>=0;j--)
    cout<<kavi[j];
Sci@phy 97 Posting Whiz in Training

And what is the question?

Um, ok... I'll give you a hint... something about your for loop is wrong. It will always return that number is prime. Try to backtrack you program if you enter for example number 4. It shouldn't be prime, but... :)
And for all those if-statements... try using some else if too

Sci@phy 97 Posting Whiz in Training

If we want to go dynamic, why not use new instead of malloc()? :)

Sci@phy 97 Posting Whiz in Training

And you don't need these two lines :)

#include <cstdio>
#include <cstdlib>

I wouldn't recommend writing always the same, although it's the easiest way

And one thing more. I realise you are just starting in C++, but try not to use system("PAUSE"); Just enter this instead of that: cin.get(); In that way you won't be able to press "any key", you will have to press enter, but for starting programs it shouldn't bother you. And in that way the code is much more standard.
HTH

Sci@phy 97 Posting Whiz in Training

Several syntax mistakes:
1. Missing opening bracket after main()!
2. Missing opening bracket after for (instead you have closing bracket on line 13.)!
3. Missing closing for-bracket.
4. Missing closing main() bracket!

Sci@phy 97 Posting Whiz in Training

Yes, yours does look much nicer. I always seem to write in a sloppy manner which will hurt me with longer programs. My teacher gives us examples, which never seem to help, and he didn't use bools so neither did I. I really appreciate the help Sci@phy. I'm sure I'll be posting more in the near future. These assignments are getting difficult for me.

You don't have to use bools for simple stuff, like:

if (a >= b) //code here
else //code here

I used bool for two reasons:
1. To assing values to stuff that is calculated more than once
You had four checks if (age >= 18) and each time computer has to calculate once again if it is true (to protect myself from experts: maybe :) ). In this way it calculates only once.
2. To make code more readable
There's no much explanation to this, but as I said earlyer, don't overuse bools

Sci@phy 97 Posting Whiz in Training

Ok, I like this style more (it's the same concept), but it's just a suggestion:

bool male = (gender == "m");
bool is_for_army;
bool was_in_army = (answer == "yes");

if (age>=18){
    if (male){
        if (age <= 30) is_for_army = true;
        else if (age <= 35 && (was_in_army || pushups >=50)) is_for_army = true;
        else is_for_army = false;
    }
    else{
        if (age <= 32) is_for_army = true;
        else if (age <= 40 && (was_in_army || pushups >=30)) is_for_army = true;
        else is_for_army = false;
    }
}
else is_for_army = false;

if (is_for_army)
    cout << "Yes, " << name << ", you may apply." << endl;
else
    cout << "Sorry, " << name << ", you are not eligible." << endl;
Sci@phy 97 Posting Whiz in Training

No :)
But I believe it does what you want it to do.

Sci@phy 97 Posting Whiz in Training

Don't use the same template!!!
Especially using namespace std; And yes, you could use: int main(int argc, char* argc[]) But if you don't need arguments you can use: int main()

Sci@phy 97 Posting Whiz in Training

It goes like this:
n(0) = 1;
n(1) = 1;
n(2) = n(1), n(2) //so it's actually two numbers
n(3) = n(1), n(2), n(3)...

Bottom line is: First two numbers just copy.
The print others in a for loop

Sci@phy 97 Posting Whiz in Training

And what does fflush(NULL) do?

Flushes everything (like a damn good toilet :) )

Sci@phy 97 Posting Whiz in Training

Just some literature from wikipedia:
"In C99, there is a bool type, along with the values true and false, defined in the <stdbool.h>"

Sci@phy 97 Posting Whiz in Training

1. You are making memory leaks all over the place. When you operate with dynamic memory allocation you have the responsibility to free every part of memory that you don't use.

2. Instead of void main() always write int main() 3. myRemove() isn't working. Why are you creating a new memory space for temp? And then when you have created new mem space, TEMP->info is empty!

Sci@phy 97 Posting Whiz in Training

Here's what I came up with:

//a lot of code

Here are the 2 errors- error C2296: '+' : illegal, left operand has type 'int (__cdecl *)(int)' and cpp(27) : error C2082: redefinition of formal parameter 'i'

On line 22 you are declaring function. There's no semicolon in the end.
Main is also a function. Do you write: int main();

Sci@phy 97 Posting Whiz in Training

I mean... how do I calculate with the size in pounds entered by the user, if it is not used in the function?

Thanks a lot for the help!!

Confused :confused:

Sci@phy 97 Posting Whiz in Training

It shouldn't happen, but this code isn't giving anything

Sci@phy 97 Posting Whiz in Training

It will work. Comparing char with int is allowed.

Worst case is a compiler warning (eg mismatch of precision of operands). A compiler warning means the programmer should check and make sure the code has the intended result, not that it is disallowed.

Yes, but does he want to compare test with '1' or with 1? Because that's different thing!

Sci@phy 97 Posting Whiz in Training
while(soldiers.size()==1)

This while statement is ill

Sci@phy 97 Posting Whiz in Training

Hm... try this:
after line cin>>option add: cin.ignore(80, '\n'); .
It should work. And I trust you ommited all "void" keywords from calling function?
When you want to call void Pattern(void), you just type Pattern();

Sci@phy 97 Posting Whiz in Training

That's because... let's say you write 123c.
First for loops finds error, and inserts "invalid" into result. But then it simply continues to another loop. You have to either return inside that if statement or insert some flag that checks if "invalid" is, and if it is, don't go with second for-loop. (by the way, try tagging code with program name: , that way your code will be numbered, so I can tell you: for loop in line 3, switch in line 13... etc)[code=cplusplus], that way your code will be numbered, so I can tell you: for loop in line 3, switch in line 13... etc)

Sci@phy 97 Posting Whiz in Training

No, no...
n := sale/100;
And then INSIDE for loop that counts from 0 to n insert cout<<'*';

Sci@phy 97 Posting Whiz in Training

and sorry but this gives error too

ifstream& FileHandler::getFileIn()const{
return fileIn;
}

compile error:
invalid initialization of reference of type 'std::ofstream&' from expression of type 'const std::ofstream'

Huh? ifstream function cannot give ofstream error.
Be sure that you've changed declarations in header file too!

And no, you don't need to make private variables references. Only the function returns reference

Sci@phy 97 Posting Whiz in Training

So you want to print '*' n times, where n is a number of hundreds of dollars?
$300 - n = 3
$630 - n = 6
$690 - n = 6? (or n = 7?)

If so, divide each sale by n and store it in int (in that way $630 will end up being 6.3, but stored in int: 6!)

Then add another loop inside row-loop that will print cout<<'*' for n times.
Like this pseudocode:

for rows loop
     for n loop
          print *
     end n loop
     print newline
end rows loop
Sci@phy 97 Posting Whiz in Training

The problem isn't in your code anymore, it's something to do with compiler and OS that you're using.
I'm clueless

Sci@phy 97 Posting Whiz in Training

Like twomers said, just try to return them as references:

ifstream& FileHandler::getFileIn()const{
return fileIn;
}
Sci@phy 97 Posting Whiz in Training

Ok, first, the code won't compile without #include <iostream> As for the shadowing. What have you written is:

int myfunction(int a_number){
    int a_number = 4;
    //do stuffz
    return some_num;
}

See the problem? You are creating a variable a_number at the moment the function starts (in line 1 of my code). Immediately after that you are declaring another variable with same name (in line 2).

EDIT: and for the worst, you aren't declaring the same thing. Function parameter is string type, and your declared variable is string[] type.

Sci@phy 97 Posting Whiz in Training

Post a small code where you are using your function, please

Sci@phy 97 Posting Whiz in Training

Try to remove
#include<stdafx.h>
It's obsolete.

But I'm not sure if it will fix your problems...
As you said, you are a beginner. Maybe try with something more simple?

Sci@phy 97 Posting Whiz in Training
case 'C': case 'c':
    void Convert(maxTemp, minTemp, unit);

What is this? You call void function like all others:

case 'C': case 'c':
    Convert(maxTemp, minTemp, unit);
Sci@phy 97 Posting Whiz in Training

You need something like this?

void Totalinfo( fstream & information, Class& academic)
Sci@phy 97 Posting Whiz in Training

Hi,
When I used scanf instead of scanf_s, it printed the output properly... I am not aware of scanf_s... googled it and found it out its some secure version of scanf or something... i use unix and this is available only for MS C I guess...

scanf("%d %d %d %d ", &a1,&a2, &a3, &a4);

I doubt that IT printed out properly. Maybe you retyped it, because on my compiler (gcc) problem is with blank space after last %d that exists in his code, and so scanf asks for continuation of input... Or something :/ pretty weird function