Narue 5,707 Bad Cop Team Colleague

You're storing the factorials in an array, but that doesn't change how you calculate the value. Do you know how to calculate a factorial?

Narue 5,707 Bad Cop Team Colleague

>For example if i convert 255 from base 10 to base 2
>i am getting 11111110 instead of 11111111.
That particular example works fine for me. Though your code has some...interesting aspects.

Narue 5,707 Bad Cop Team Colleague

...

Narue 5,707 Bad Cop Team Colleague

>The problem is solved by replacing getchar(); with getch();
Fine. Use getch then. It's not like I went out of my way to point out that getch is non-portable and offer a perfectly valid alternative. Oh wait, I did. I guess some people just insist on refusing to learn and doing stupid things. Next time I won't waste my time being so helpful.

Narue 5,707 Bad Cop Team Colleague

You don't change a file in-place, you either read the file into memory and make your changes before truncating the file and rewriting it, or you use a temporary file so that only one record is in memory at a time.

Narue 5,707 Bad Cop Team Colleague

>can a class allow a friend method of a friend class to use it's protected or private members
Just because you're my friend doesn't mean your other friends are also my friends.

>can a friend method of a derived class use base class's protected members
If you're my child, your friends aren't necessarily my friends.

Narue 5,707 Bad Cop Team Colleague

>i must tell my other friend join in("_")
Joy.

Narue 5,707 Bad Cop Team Colleague

>Borland C++
That makes sense. Borland likes to complain about unused results. You can safely ignore the warning (because getchar really does something here), or you can cast the result to void to silence the warning:

(void)getchar();
Narue 5,707 Bad Cop Team Colleague

>compiler shows a warning..."code has no effect"
What line and what compiler?

Narue 5,707 Bad Cop Team Colleague

>printf("Enter Number :");
You should call fflush ( stdout ) after this. The stream buffer is only guaranteed to be flushed when it's full (which you can't portably determine), when a newline is printed (which you aren't doing for this prompt), or when fflush is called. If stdout isn't flushed before the scanf call, your user may not see the prompt.

You also need to include stdio.h. Your code presently exhibits undefined behavior.

>scanf("%d", n);
scanf takes a pointer to int so that it can store the value in your variable. Because n isn't already a pointer, you need to pass the address of n. You should also be checking the return value of scanf in case the user can't type or follow instructions.

>getch();
You need to include conio.h, but getchar is better because it's standard while getch is not.

Here's a better example:"

#include <stdio.h>

int main ( void )
{
  int n;

  printf ( "Enter Number: " );
  fflush ( stdout );
	
  if ( scanf ( "%d", &n ) == 1 )
    printf ( "Hexadecimal Equivalent %x\n", n );

  while ( getchar() != '\n' )
    ;
  getchar();

  return 0;
}
Narue 5,707 Bad Cop Team Colleague

>it didnt work what to do?
If you keep saying "it doesn't work", I'll stop helping you. Explain in detail how it doesn't work and I'll tell you why it doesn't work. I may even tell you how to fix it. But my knee jerk reaction to "it doesn't work" is "bummer".

Also, your code is awful. Learn how to format it properly and then use code tags when you post it here. Furthermore, if you're going to write C, you might as well just go with full blown C instead of some bastardization of C and C++.

>for(i=0;i>5;i++)
i will never be greater than 5 if you initialize it to 0. Your loop does nothing.

Narue 5,707 Bad Cop Team Colleague

>in this program there is for loop and while loop and
>arrays... just like turbo c++ or its totally different?
You're still programming in C++, the fundamentals aren't going to change drastically. The big difference will be in the library features you use because I'm reasonably sure you've been using pre-standard C++, and the library has grown and improved since the language was standardized in 1998. I recommend you get a book written after 2000 for a description of the current language.

Narue 5,707 Bad Cop Team Colleague

>alwayz i write int main()= just like void main()
Yes. void main() is wrong, int main() is right.

>getchar() = just like return (0) ?
No. getchar is a separate entity entirely. The reason I didn't put in a return statement is because C++ allows you to omit it and 0 will be returned automatically when you fall off the end of main. getchar simply pauses for input so that the window isn't destroyed when the program ends. If you feel better returning 0 explicitly, you can do so:

#include <iostream>

int main()
{
  std::cout<<"Hello, world!\n";
  std::cin.get();
  return 0;
}
Narue 5,707 Bad Cop Team Colleague

>isnt working
"Isn't working" doesn't work. Be more specific. Does it fail to compile? If so, what compilation errors do you get? Does it fail to run? If so, what errors do you get? Is the output wrong? If so, how?

My guess is that the window pops up, prints the output, and then closes before you can read it. In that case, change your code like so:

#include <stdio.h>

int main()
{
  int x=0;
  printf("%d",x);
  getchar();
}
Narue 5,707 Bad Cop Team Colleague

>are u saying that this program is totally different from turbo c++
I'm saying that you learned bad C++ on a compiler that encouraged bad C++. Now it's time to learn correct C++.

Narue 5,707 Bad Cop Team Colleague

>there might be problem here when i am opening after void main ()
An immediate problem is void main. main returns int, and any decent compiler will complain if you use void. Use this as a test program:

#include <iostream>

int main()
{
  std::cout<<"Hello, world!\n";
}
Narue 5,707 Bad Cop Team Colleague

Have you tried Turbo C++? Or perhaps Visual Studio Express 2008? Rumor has it that even the latest version of Dev-C++ works on Windows Vista, so if you can't get any of these to work, the problem isn't in the programs.

Narue 5,707 Bad Cop Team Colleague

Well, if you were better at describing your problem, I'm sure someone would be willing to help you. But not me, because I don't care about you anymore.

Narue 5,707 Bad Cop Team Colleague

Then why do you keep replying with increasing irritation? :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

>but what is the purpose of the retrieval operation?
I would assume it's a search member function that gives you the node at a particular index or with a particular value.

Narue 5,707 Bad Cop Team Colleague

>I don't know why the compilers aren't working on Vista...
He doesn't seem to want a compiler. I already suggested that by "c++ program" he meant "C++ compiler", but he made it clear that I was wrong. Why are you trying to fool him that you know what he needs? :D

Narue 5,707 Bad Cop Team Colleague

>thx in advance on trying to fooling me that u know what i need
So you don't need a compiler? Fine, what exactly does this mysterious "c++ program for vista" of yours do? If I suggest Word 2007 will you accept that as a valid answer?

>hehe dont worry in the end someone kindly not like u will help lol
I doubt it. Not only is your writing style painfully obtuse, you don't even seem to know what you want.

Nick Evan commented: Ah.. the beautifull sound of the chainsaw +2
Narue 5,707 Bad Cop Team Colleague

>OK i dont care what is ur problem with my writing
Okay, then I don't care what your problem is either.

>all i need is c++ program
After deciphering your textcrap, I've determined that you're looking for a C++ compiler that works on Windows Vista. If you said that in the first place, maybe I would have actually helped you.

>is this hard on u
Nope, I don't care about your problem, so it's not hard at all. All I need to do is plonk you.

*plonk!*

Narue 5,707 Bad Cop Team Colleague

i r nt knowing wut t3h hell ur talkng abt lol rofl jejeje!!!!!111

So far you've committed every noob sin in the book. How about trying to ask an actual question, asking it in the form of a complete thought, and not using lame abbreviations and chat speak. This forum isn't a cell phone, you can afford to take your time and write properly.

I'm pretty good at translating gibberish, and I still have no clue what you're trying to say.

Narue 5,707 Bad Cop Team Colleague

>I suppose you could do this, just to see who's awake
You're a cruel, cruel man, Salem.

Narue 5,707 Bad Cop Team Colleague

>I started learning C# but I became annoyed at the need for the
>.NET enviroment on every machine the program was run on
C# has no such restriction. It's a standardized language that can run outside of the .NET framework[1]. However, you're going to find this annoyance everywhere you go (with any language) if you intend to write any kind of non-trivial program. Eventually you'll have to rely on platform-specific features that aren't portable or suffer abstractions like .NET or JVM.

>I am hearing talk about C++ dying out
To answer that I'll mention that FORTRAN is still in fairly widespread use, and it's a good 30 years older than C++ and vastly less popular. C++ isn't likely to die in your lifetime.

>and C# is better
"Better" is extremely subjective. If someone tries to tell you something is "better", ask them to prove it.

>and is where all the jobs and money is
It really depends on what you want to do and whom you want to work for. But I can safely say that a C# programmer will be more valuable if he knows C++ too.

>The reason I was woried about the .NET enviroment is that
>I wanted my programs to work on nearly all machines
That kind of portability is extremely difficult. You'd be much better off realizing that portability isn't being able to compile code everywhere, it's the ease with which you can convert …

Narue 5,707 Bad Cop Team Colleague

We have a forum for job offers. Perhaps you should post there instead of here.

Narue 5,707 Bad Cop Team Colleague

>I actually changed all int to float and it worked aswell
The only one that needs to be changed is sum, but you can change them all if you want. Though I would recommend getting into the habit of using double instead of float when you want floating-point values. The default floating-point data type is double, so it'll make your life easier.

>This probably is a very stupid question but witch button toggles average ?
Well, since you seem to be on a Windows machine, you press and hold down either ctrl key, and then press z. This signals end-of-file in the Windows console.

Narue 5,707 Bad Cop Team Colleague

>but Im not sure what should I float in code like this.
Change int sum = 0; to double sum = 0; . Easy, huh? :)

>And when I input 1+2= and press enter
That's a completely different input mechanism. The code you've been shown so far expects only numbers, separated by whitespace. So your execution should look like this:

enter your numbers 1 2 3 4 5
^Z
average is: 3

Remember that if you type a non-digit, cin pukes? Well, '+' is a non-digit. ;) I'd recommend getting it to work first, then you can add fancy input parsing.

Narue 5,707 Bad Cop Team Colleague

What operating system?

[edit]
For rep happy the morons who can't tell the difference, I'm asking if the OP is using Windows or DOS. The answer to the question changes drastically between those two systems, and Borland has written C++ compilers for them both. :icon_rolleyes: ;)
[/edit]

Sturm commented: Are you really that stupid? +0
Narue 5,707 Bad Cop Team Colleague

>is it possible to input unlimited amount of nummbers nut just number 1 + number 2?
Yes. You need a running sum, a count of how many numbers there are, and one variable for the number. Then you'd do this:

int count = 0;
int sum = 0;
int x;

while ( cin>> x ) {
  sum += x;
  ++count;
}

if ( count > 0 )
  cout<<"Average: "<< sum / count <<'\n';

>how can I make it so program says that it is invalid input value
If you tell cin that you're going to type an int, but you don't, it fails. You can test for that and act accordingly:

int count = 0;
int sum = 0;
int x;

while ( cin>> x ) {
  sum += x;
  ++count;
}

if ( !cin.eof() )
  cout<<"Invalid input\n";

if ( count > 0 )
  cout<<"Average: "<< sum / count <<'\n';

If the loop terminates it means that cin failed for some reason. Most likely the reason is that you signaled end-of-file with something like ctrl+z (for Windows), and the eofbit will be set (which you can test for with the eof() member function), but if you type a non-digit then it will fail with an error and the eofbit won't be set.

Narue 5,707 Bad Cop Team Colleague

>Then it's maybe only I who see it as bad practice?
No, you're not the only one, but it is a subjective issue.

>A program should exit by telling that it's finished and it all went as planned.
The language rules say that falling off the end of main will exit as if you said return 0; , so we're not talking about the undefined behavior problem from pre-standard C++ or C89. These two programs are completely identical:

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

I can only think of two good reasons not to omit the return statement:

1) Consistency with other functions. main is a special function with special rules. I'm not sure that trying to match main with less special functions is a good idea because it trivializes the differences. This is what causes misunderstandings that produce the void main issue.

2) Symmetry when using an error return. If you're returning failure, you might find it strange not to match that with returning success:

#include <cstdlib>

int main()
{
  //...

  if ( something )
    return EXIT_FAILURE;

  //...

  return EXIT_SUCCESS;
}
Narue 5,707 Bad Cop Team Colleague

>//Yes I did put in 'return 0' because it's bad
>//practice to leave it out and yada yada yada...
You're not getting away with this one. :) Explain why it's a bad practice, please.

Narue 5,707 Bad Cop Team Colleague

>I read up on switches, and it says they're used for strings, not chars
Quite the contrary. A switch doesn't work with strings, but because char is an integral type, it works just fine as a switch case.

Narue 5,707 Bad Cop Team Colleague

>what might be the problem?
My guess is PEBKAC.

Narue 5,707 Bad Cop Team Colleague

>our professor gave an example where he said that he created a program for a school and he
>created an array for 3000 (for the # of students), but actually there were 3002 students and
>he said that the school lost the data...
If this was supposed to be a serious program and not an example or prototype, I'd say your professor is an incompetent boob when it comes to practical programming. I mean, really. The number of students is variable, so why would you think a statically sized array could possibly be sufficient? The only time you use arrays is when you have control over the size, or the array is used as a temporary in building a larger container.

>is that how sensetive the arrays allocation of memory/size is and why?
If you have 3000 slots and 3002 items, where do the other 2 go? That's the problem of fixed size containers, and it's a very simple problem, but with serious implications.

Narue 5,707 Bad Cop Team Colleague

>I usually fix the errors first before I fix the appearance of the code.
The appearance of the code helps you find errors, genius.

Narue 5,707 Bad Cop Team Colleague

>recursiveReverse(a[], ++start, --end);
You don't use [] when passing an array to a function.

Narue 5,707 Bad Cop Team Colleague

>How could I alter it to accomplish what i want?
I don't know what you want, but replacing the entire loop with this makes sense to me:

int grades;

while ( infile>> grades )
  testScores.push_back ( grades );
Narue 5,707 Bad Cop Team Colleague

>translate
It's transform, not translate. Be sure to include the <algorithm> header, and keep in mind that the way you're doing it might not work. But it probably will, so no worries at this point. Also, unless you want to change word so that it's a std::string object, you can't use the begin and end member functions. This will work (but you also have to include <cstring>:

transform ( word, word + strlen ( word ), word, tolower );
Narue 5,707 Bad Cop Team Colleague

It means that the >> operator isn't overloaded for the vector class. In other words, it won't work.

Narue 5,707 Bad Cop Team Colleague

>With regards to the errors, im pretty sure that the 'infile' statement is correct.
It is correct. The line number that a compiler gives you is where the error was noticed. It's best to look at the line you're told, and about five lines above it. If you do that, you'll see that your definition of main is missing an opening brace.

Narue 5,707 Bad Cop Team Colleague

>any ideas why i'm having these problems.
Yea, you're cutting and pasting code without knowing what you're doing, running to us when you get any easily fixed syntax error, and generally being lazy. Syntax errors are the easiest of problems to locate and fix. The compiler tells you the line number to start at, and exactly what it expected or didn't expect to find. When you know where to look (such as a line number), even a rudimentary understanding of C++ syntax means you can glance at the code and see if something is missing or shouldn't be there. You have at least that understanding, which suggests that you're just not trying.

Narue 5,707 Bad Cop Team Colleague

>My professor tactually took off 10% of my assignment
>because i didn't use the while(!infile.eof()).
What an ass. You can't reason with people like that, so just do what he wants and be sure never ever to trust what he teaches you without verifying it first. You can even add comments to the bad code to let us know that you're being forced to do it. If he takes anything off of your grade for comments (as long as they don't break any rules or laws), you can report him to the school.

Narue 5,707 Bad Cop Team Colleague

>but until i type a word that is not found it display "not found"
>but i can search anything because it said "not found"
Your loop terminates in one of two ways: A word is found and you use break to jump out early, or a word is not found and you loop all the way to end-of-file. After the loop, you seek back to the beginning of the file, but because seekg doesn't reset the eofbit, you have to call clear if you want the loop to run again. On streams with an error state set, any input request is ignored. Replace this:

dict.seekg(0);

With this:

dict.clear();
dict.seekg(0);
Narue 5,707 Bad Cop Team Colleague

>i was wondering whenever you create a file in c++ where does it go
If you don't supply a full path when opening a file, a relative path is used. That depends on how your system deals with relative paths, but I'd wager that you're on a system that uses the current working directory.

Narue 5,707 Bad Cop Team Colleague

You have a wayward closing brace here:

cout<< findbig ( num1, num2 ) <<" is the larger of "<< num1<<" and " <<num2<<"\n";
}

>I'd have loved to use your second option, but my professor
>is very particular about the format of his assignments...
I didn't expect you to use it anyway. That was in response to another poster who asked how I might solve the problem for any number of values.

>A few questions narue...what exactly does the '?' do?
It's a conditional operator, roughly equivalent to this:

if ( num1 > num2 )
  return num1;
else
  return num2;

>but seem to be having a little problem.
Your problems always seem to be strictly syntax based. Programming is all about an eye for detail, C++ programming even more so. You need to understand the rules and follow them. Here are your mistakes. Try to learn what they are, and why you keep making them:

>getnumbers(&num1, &num2)
Semicolons are consistently required. Find out where, and don't forget them.

>cout<< num2<< " is the larger of " <<num1<< " and "<< num2 "\n";
Every << single << individual << item << is << separated << by << an << insertion << operator.

>cout<< num1 <<" is the larger of "<< num1<< " and " <<num2 endl "\n";
Ditto.

else 
{
  return 0;
}

An else statement cannot exist without a matching if statement.

Narue 5,707 Bad Cop Team Colleague

>if I compile using your suggested format...i get the following error:
You made a change that broke the code. Post what you're compiling so I can see where the error is.

>widenning for many cases that is there are many nums to compare to each others, how can you solve?
Load the numbers into a suitable data structure (I'd suggest a set), and then search for the smallest. For example:

#include <iostream>
#include <set>

int findbig ( std::set<int>& numbers )
{
  return *numbers.rbegin();
}

void getnumbers ( std::set<int>& numbers )
{
  int num;

  std::cout<<"Please enter a list of numbers to compare: ";
  
  while ( std::cin>> num )
    numbers.insert ( num );
}

int main()
{
  std::set<int> numbers;

  getnumbers ( numbers );
  std::cout<< findbig ( numbers ) <<" is larger\n";
}

>I'm thinking perhaps because we're using different compilers.
Nope, it looks like we're using the same one.

Narue 5,707 Bad Cop Team Colleague

You're overcomplicating things, I think. Here's a sample without the "again" crap that should give you an idea of what's expected when functions get involved:

#include <iostream>

int findbig ( int num1, int num2 )
{
  return num1 > num2 ? num1 : num2;
}

void getnumbers ( int& num1, int& num2 )
{
  std::cout<<"Please enter two numbers to compare: ";
  std::cin>> num1 >> num2;
}

int main()
{
  int num1, num2;

  getnumbers ( num1, num2 );
  std::cout<< findbig ( num1, num2 ) <<" is larger\n";
}
GreenDay2001 commented: whoops..thanx for figuring out my folly, didn't noticed then, but my code was really crap +4
Narue 5,707 Bad Cop Team Colleague

You're not really paying attention, are you? The first two arguments to replace are always going to be the offset and the count, or a beginning and ending iterator. Note that none of those options is a string or string literal. The site I linked to gives you the declaration for every overload of every function in the string class (including types), so you shouldn't be getting these errors.