|
|
help please?
|
|
|
|
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status:
Offline
|
|
evenin',
i am pretty sure i am missing something that is extremely simple, but can anyone help me figure out what is wrong with my code ( i know it is VERY simple and basic, but it i just started the class and sometimes things at the beginning are simple sorry! )
this work fine at school with Visual C++ (could be what the problem is?) but when i try to compile this at home in terminal with g++ or gcc, it returns that main() must return an integer. i have added a return 0; in there, but it still comes back that i must return an integer in main(). any ideas what i am missing or not seeing that i need to fix? (it is taken straight out of the book.) thanks!!
code:
//decision demo
#include<iostream.h>
void main()
{
int first, response, bigger;
cout << "Enter an integer value ";
cin >> first;
cout << "You entered " << first << endl;
cout << "Enter any number bigger than " << first << " ";
cin >> response;
if(response > first)
cout << "Good job" << endl;
else
{
cout << "You did not follow directions" << endl;
cout << response << " is not bigger than " << first << endl;
}
bigger=first+6;
cout << "Enter any number between " << first << " and " << bigger << " "<< endl;
cin >> response;
if (response > first && response < bigger)
cout << "Good job " << endl;
else
cout << "No " << response << " isn't between those numbers" << endl;
cout << "Enter any number less than " << first << " ";
cin >> response;
if (response < first)
cout << "Good job!"<< endl;
else
cout << "Nope, I am sorry. That is incorrect."<< endl;
cout << "Enter a number equivalent to what your first number (it's at the top of the screen) " << " ";
cin >> response;
if (response == first)
cout << "Good job!" << endl;
else
cout << "I am sorry, " << response << " is not equal to " << first << endl;
cout << "Enter a number that is smaller than " << first << " or greater than " << bigger << endl;
cin >> response;
if ((response < first) | (response > bigger))
cout << "Great job!" << endl;
else
cout << "I am sorry, you entered a number that is incorrect." << endl;
}
thanks again!
ps any idea what i need to do to be able to run this in XCode? i oopen it and it doesn't give me options to be able to build, compile, link or anything. thanks!
|
some people are like slinkys: they don't do much, but are fun to push down stairs.
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by zanyterp:
void main()
Declares a function which takes no arguments and returns void, not an integer.
And xCode does not work with individual files. You can only build and run a project. However, you can make a project that contains only one source file. The reason it works this way is that build settings are stored in the project.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status:
Offline
|
|
Originally posted by Chuckit:
Declares a function which takes no arguments and returns void, not an integer.
And xCode does not work with individual files. You can only build and run a project. However, you can make a project that contains only one source file. The reason it works this way is that build settings are stored in the project.
that's what i thought, and understood from the book, which is why i was wondering why it was saying that (it's what the error says). is my command wrong, then possibly?
prompt> g++ file.cpp
so to use my single source code files, since we are doing only simple console stuff right now, would be File:New, Project then copy & paste my code into the new window with main() selected?
|
some people are like slinkys: they don't do much, but are fun to push down stairs.
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by zanyterp:
that's what i thought, and understood from the book, which is why i was wondering why it was saying that (it's what the error says). is my command wrong, then possibly?
prompt> g++ file.cpp
What I'm saying is, it's not enough to add a return 0 at the end. The C++ Standard says that the main function must have the signature int main() or int main(int, char**). Visual C++ allows void main(), but it's not valid C++.
Originally posted by zanyterp:
so to use my single source code files, since we are doing only simple console stuff right now, would be File:New, Project then copy & paste my code into the new window with main() selected?
Yep. You'll want your project type to be Standard C++ Tool (or whatever it is along those lines).
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status:
Offline
|
|
Originally posted by Chuckit:
What I'm saying is, it's not enough to add a return 0 at the end. The C++ Standard says that the main function must have the signature int main() or int main(int, char**). Visual C++ allows void main(), but it's not valid C++.
Yep. You'll want your project type to be Standard C++ Tool (or whatever it is along those lines).
that's what i was looking for. thank you. i had a feeling that part of the problem could be the machines at school and the book is probably strong related to MS. . . .
but such is life. i will have to check that and see if it will work. will void int main() work or is it only one of the choices listed above or add int, char** in the ()? and i am sorry i misunderstood your first post and trying to make sure i got it. and while we're on the subject of valid C++, is main() supposed to have return 0 at the end or is that optional?
thanks for the help!
|
some people are like slinkys: they don't do much, but are fun to push down stairs.
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status:
Offline
|
|
Here's what you should be:
int main(int argc, char *argv[]) {
....type your code here....
return 0;
}
|
chown -R us:us yourbase
Dissent is not un-American.
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by zanyterp:
and while we're on the subject of valid C++, is main() supposed to have return 0 at the end or is that optional?
It isn't required as far as I'm aware (the compiler should assume 0 if no return value is specified), but I generally put it in anyway, for consistency if nothing else.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Dec 2004
Status:
Offline
|
|
I'd also point out that in the console/command line world a program that returns 0 is generally considered to have completed successfully. A non-zero return value typically indicates some sort of error occurred.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|