Sign in to follow this  
LordofMuffins

What the hell is wrong with my compiler?

Recommended Posts

I'm running Dev c++ beta and it won't compile the following:
//compute n!
#include <iostream.h>

int main() {
      int n;      //The number the user types in.
      int Result = 1;
      int i;      //Loop variable.
      
      //Get the value.
      cout << "What is the number?" endl ;
      cin >> n;
      
      //Now loop through. Each time through the loop
      //multiply the result by i. This will give
      //1*2*3...n because i starts at 1 and increases
      //until it is n.
      for (i=1; i<=n; i++) {
             Result *= i;
      }
      
      //Print the result.
      cout << "n! is " << Result << endl;
}              
[Edit: Use the [source] tag to improve legibility. See the Site FAQ for more information. - Oluseyi.] WTF? These are the errors I'm getting: 2 C:\Dev-Cpp\include\c++\3.3.1\backward\iostream.h:31, from main.cpp In file included from C:/Dev-Cpp/include/c++/3.3.1/backward/iostream.h:31, from main.cpp 2 C:\Dev-Cpp\include\c++\3.3.1\backward\backward_warning.h:32 #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated. C:\Dev-Cpp\main.cpp In function `int main()': Holy **** there are a ton of errors... I don't wanna list the rest...help? [Edited by - Oluseyi on October 2, 2004 12:16:48 PM]

Share this post


Link to post
Share on other sites
You can get rid of the compiler warnings by replacing

#include <iostream.h>

with

#include <iostream>
using namespace std;


iostream.h should not be used anymore.

[edit]
Argh, too slow [smile]
[/edit]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It is extremely irritating when people blame programming errors on compilers. As Ra pointed out, you should use <iostream> instead of <iostream.h> (note that the imported symbols will now reside in the std namespace rather than the global one); if you want help with the other errors, you will have to provide more details.

Quote:
Original post by Mushu
Well, I don't know C++, but shouldn't you have
return 0; at the end of the function?

This is optional. Every function that returns a non-void value shall include an explicit return statement, but main is an exception—in the absence of an explicit return statement, it returns 0, which generally signifies successful completion.

Share this post


Link to post
Share on other sites
Quote:
Original post by LordofMuffins
Still not working. I changed int main to void main, I thought that would work, but it doesn't...apparently that's the only problem now.

void main is not legal; main shall always return an int.

Share this post


Link to post
Share on other sites
At least he still should try to put return 0; as the last statement of main() to see what happens. I find it more safe never to assume that compilers recognize fine nuances of a language like this particular example. Nevertheless I believe DevCPP supports this; in fact it does not warn if I forget to return a value in any normal function as fall, and it is an constant source of crashes (another reason not to omit returns).

EDIT: What is the error message you got this time?

Share this post


Link to post
Share on other sites
It doesn't work with int either...
And also, for this program:

#include <iostream>
using namespace std;


switch (n) {
case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
case 3:
cout << "three";
break:
case 4:
cout << "four"l
break;
default:
cout << "unknown number";
}

It says: syntax error before switch.
Help?

Share this post


Link to post
Share on other sites
Quote:
Original post by Unwise owl


EDIT: What is the error message you got this time?


You don't even want to know. I got about 20 of them. I think they were about the int conflicting with another int.

Share this post


Link to post
Share on other sites
Quote:
Original post by LordofMuffins
Quote:
Original post by Unwise owl


EDIT: What is the error message you got this time?


You don't even want to know. I got about 20 of them. I think they were about the int conflicting with another int.


you should comment out most of the code, compile, and deal with 1 or 2 errors at a time. the uncomment some more, compile, etc... if you are getting 20 errors that is a real mess to deal with all at 1 time.

Share this post


Link to post
Share on other sites
Learn to read errors. They indicate the file, the line within the file, and then a description of the error. Read it! It gives you valuable information to help you solve your own mistakes.

No, it's not your compiler. It's you. It's (almost) always you.

Share this post


Link to post
Share on other sites


//compute n!
#include <iostream>
using namespace std;

int main() {
int n; //The number the user types in.
int Result = 1;
int i; //Loop variable.

//Get the value.
cout << "What is the number?" << endl ;
cin >> n;

//Now loop through. Each time through the loop
//multiply the result by i. This will give
//1*2*3...n because i starts at 1 and increases
//until it is n.
for (i=1; i<=n; i++) {
Result *= i;
}

//Print the result.
cout << "n! is " << Result << endl;
}


Share this post


Link to post
Share on other sites
Every function has to have exactly one implementation in {-}-braces.

Yours has 3, which wont work at all.

Share this post


Link to post
Share on other sites
Quote:
Original post by LordofMuffins
*** Source Snippet Removed ***

What's wrong with this?


What's wrong with it is that you are closing the main function with a "}" before the next two statements. Also you don't even need to put braces around the next two statements. cin >> "0" >> is not even a valid statement.


#include <iostream>

using namespace std;

int main( ) {
cout << "Hello World" << endl;
cout << "Enter a number" << endl;
int Number;
cin >> Number;
cout << "You Entered: " << Number << endl;

return 0;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by LordofMuffins
It doesn't say the "You entered" part when I run it, when I enter a number and press enter it the window closes itself.
Put cin.ignore(); cin.get(); before your return. That'll keep the window open until you hit enter.

Share this post


Link to post
Share on other sites
Quote:
Original post by LordofMuffins
It doesn't say the "You entered" part when I run it, when I enter a number and press enter it the window closes itself.

Thats because the program exits. Try doing as Ra suggested, or run your program from the command prompt.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this