What the hell is wrong with my compiler?

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 on other sites
Well, I don't know C++, but shouldn't you have
 return 0;
at the end of the function?

Share on other sites
You're using a deprecated header. Use #include <iostream>

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.

Argh, too slow [smile]
[/edit]

Share on other sites
It still isn't working. It's a console application. What the hell is wrong with this? Where should I put return 0? And why don't I need the .h?

Share on other sites
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 MushuWell, 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 on other sites
... Damn it. AP = yours truly.

Share on other sites
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.

Share on other sites
Quote:
 Original post by LordofMuffinsStill 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 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 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 on other sites
You cannot place program statements (such as switch statements) at global scope; they can only appear inside functions.

Share on other sites
Quote:
 Original post by Unwise owlEDIT: 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 on other sites
Quote:
Original post by LordofMuffins
Quote:
 Original post by Unwise owlEDIT: 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 on other sites
on your first cout you are missing the << before endl

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 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 on other sites
[sourcelang=cpp]#include <iostream>using namespace std;int main( ) {     cout << "Hello World" << endl;}{        cin >> "0" >> }{    return 0;}

What's wrong with this?

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

Yours has 3, which wont work at all.

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 on other sites
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.

Share on other sites
Quote:
 Original post by LordofMuffinsIt 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 on other sites
Quote:
 Original post by LordofMuffinsIt 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 on other sites

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628706
• Total Posts
2984309

• 23
• 10
• 9
• 13
• 13