-
Dev C++ not compiling
#include<iostream>
inline float cube(const float s) {return s*s*s;}
int main() {
float side;
cin >> side;
cout << "Volume of cube with side" << side<< "is "
<< cube(side) << endl;
return 0;
}
Shouldn't that compile? I can't compile anything!! I just downloaded the new version today and it does the same thing as the old. I can't compile. Am I missing something?
-
You're missing a using namespace std; in there.
-
Didn't learn about those yet. If that is object oriented, I haven't even touched it yet.
What should I type in?
-
Simply add the line
using namespace std;
after all your includes.
-
#include<iostream>
using namespace std;
inline float cube(const float s) {return s*s*s;}
int main() {
float side;
cin >> side;
cout << "Volume of cube with side" << side<< "is "
<< cube(side) << endl;
return 0;
}
Still didn't work.
-
I always like to call namespaces some sort of collections of functions, nicely ordered in different namespaces.
In this case, it's about the std namespace.
Normally you would type
Code:
#include<iostream>
inline float cube(const float s) {return s*s*s;}
int main() {
float side;
std::cin >> side;
std::cout << "Volume of cube with side" << side<< "is "
<< cube(side) << endl;
return 0;
}
The std:: indicates that the function you're going to call is in the std namespace.
Now the using namespace ... means that the compiler assumes those functions are in the std namespace.
so:
Code:
#include<iostream>
using namespace std;
inline float cube(const float s) {return s*s*s;}
int main() {
float side;
cin >> side;
cout << "Volume of cube with side" << side<< "is "
<< cube(side) << endl;
return 0;
}
Would work since the compiler knows those cin and cout are in the std namespace.
I hope I clarified things a bit!
-
It still does not compile.
-
What errors do you get than my friend? going to eat now but I'm sure there are other people that are able to help you out!
-
Compiler: Default compiler
Building Makefile: "D:\Program files\Dev-Cpp\Makefile.win"
Executing make...
make.exe -f "D:\Program files\Dev-Cpp\Makefile.win" all
g++.exe -c Templates/main.cpp -o Templates/main.o
Templates/main.cpp:1:20: iostream: No such file or directory
Templates/main.cpp: In function `int main(int, char**)':
Templates/main.cpp:9: `cin' undeclared (first use this function)
Templates/main.cpp:9: (Each undeclared identifier is reported only once for
each function it appears in.)
Templates/main.cpp:10: `cout' undeclared (first use this function)
Templates/main.cpp:11: `endl' undeclared (first use this function)
make.exe: *** [Templates/main.o] Error 1
Execution terminated
-
Is this the newest available version of DevC++? If not, get the newest. If yes then your include paths are probably set up incorrectly. I'm sure someone here can help you with that (I've never used DevC++).
-
Isn't there a standard that compilers follow.
-
The latest version 5 is still in beta, I believe.
Maybe it is a bug or something.
That's why I'm still using version 4.:p :D
-
Standard in what way? C++ and it's libraries are standardized, the include paths are not.
And the standard changed a few years ago, so older compilers don't know the <iostream> header.
-
I should not have an old compiler. It is Dev C++ beta 5.
-
Maybe you should try degrading to version 4 as transcender is doing? Check if that helps..
-
I have 4 now but I am having another problem. It does not seem to recognize endl; or setw (5); Here is the whole thing: It compiles with no errors but does not display right.
#include <cstdlib>
using namespace std;
#include <iostream>
using namespace std;
#include <iomanip.h>
using namespace std;
int main(){
const int R=10, F=11;
int responses [R]={1, 4, 6, 4, 8, 2, 6, 1, 8, 4};
int frequency [F]={0};
for (int answer=0; answer < R; answer++)
++frequency [responses[answer]]; // This is the key to it all!!!
cout << "Number" << setw(18) << "Frequency" << endl;
for (int rating=1; rating < F; rating++)
cout<< rating << setw(11) << frequency[rating] << endl; system("PAUSE"); return 0;}
-
I found out what was wrong: I needed this as main arguments:
int argc, char *argv[]
What are they?
-
First, iomanip too is one of the standard headers, so it should be <iomanip>. <iomanip.h> and <iostream> don't work together.
Well, if you call an app like this:
myapp hello there -version -verbose
then you are passing command line arguments. Those arguments are in argc and argv. argc is the number of arguments and argv are the actual arguments. In this case argc would be 5 and argv would be
{"myapp", "hello", "there", "-version", "-verbose"}
because the actual program name is always included in the arguments.
-
I think the supposed behaviour is for argv[0] to be the program name as it was invoked. For example, on a Unix system, if it is called via a symbolic link, or on any system if you gave it the pathname to the program, or whatever :)
-
I guess I will learn more about that later.
Here is my problem now: I can't get that to break when a negative number is entered. Did I do something wrong?
for (int i=0; i<R; i++){
cin >> responses[i];
if (responses[i]<0) break;}
-
Please use [code][/code] tags when posting code and format it properly.
Code:
for (int i=0; i<R; i++) {
cin >> responses[i];
if (responses[i]<0)
break;
}
Looks ok to me. It doesn't break?
-
Now it breaks. Take a look at my other post about illegal operations. It came from there. All I changed was putting a limit in my arrays.
Would it be ok to use a letter instead of a number?
-
-
this
cin >> responses[i]; if (responses[i]<=-1) break;
to this
cin >> responses[i]; if (responses[i]<=d) break;
-
try
Code:
cin >> responses[i]; if (responses[i]<='d') break;
please always post your code here inbetween code tags [code] Code here! [/code]
-
I seldom use code tags for one line, and for multiple lines I already told him.
Whatever, you can do it, but the outcome won't be what you expect. A
int i;
cin >> i;
will only accept numbers as input, else cin will enter a fail state where it won't read anything anymore.
-
Ok, but I read in my book that letters have integer values and what does putting 'd' around d do?
-
Well, d would be a variable named d. 'd' is the letter d.
Yes, letters have integer values (their indices in the ASCII or other code tables). I said you could do it, but I said it wouldn't be what you expect.
I should rephrase.
int i;
cin >> i;
will only accept digits as input.
-
What you mean is that if I try to use the number value of a letter and trying to add that letter to a number it will not work but,
This line is ok though, right?
cout << "Enter d when finished. Next score: "; cin >> score[i];
if (score[i]==d ||!(score[i]<1000001)) break;
-
Is score a string? if it's an int it won't work as CB said...
-
No, the compiler will assume d is a variable. And if you write
'd'
then the user has to enter the ASCII code of 'd' in order to stop the loop.
Look, it's as simple as that:
int i;
cin >> i;
reads in the string representation of a number and then converts it to that number. So it might for example read in "123" and then convert that to the number 123. Look at the source code for the itoa function if you want to know how that works.
-
I made d a char and without the ' ' it works perfect. With them it loops endlessly. It did the same exact thing as an int with the ' ' but without them worked perfect.
But. . . as an int are you saying that if the user enters in d's # representation that it will inturpret it as d and break.
-
Code:
int i=0;
while(i != 'd')
{
cin >> i;
}
This loop will stop when the user enters
100
which is the ASCII code of 'd'
-