|
konadora
Singapore66063 Posts
I want to start off by saying that I'm quite noob with C++ programming, and I need to revise everything in 4 hours' time. Guides online have more terms introduced than explained, so I'm hoping I'll get more luck here.
First, what's the difference between:
int main()
and
void main()
? I keep seeing int main() being used more often in examples, but in school, I've been only using void main().
Second, why is this invalid?
#include <iostream> using namespace std;
void main() { int x; cout << "Please enter an integer." << endl; cin >> x >> endl;
cout << "You have entered "x << endl;
}
I get these errors:
1>c:\users\user\documents\visual studio 2008\projects\p1\p1\p1.cpp(10) : error C2146: syntax error : missing ';' before identifier 'x' 1>c:\users\user\documents\visual studio 2008\projects\p1\p1\p1.cpp(10) : error C2563: mismatch in formal parameter list 1>c:\users\user\documents\visual studio 2008\projects\p1\p1\p1.cpp(10) : error C2568: '<<' : unable to resolve function overload
As well as this message while debugging:
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> & )' 1> with 1> [ 1> _Elem=wchar_t, 1> _Traits=std::char_traits<wchar_t> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> & )' 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> & )'
Fixed!
+ Show Spoiler [Fixed ver] +#include <iostream> using namespace std;
void main() { int x; cout << "Please enter an integer." << endl; cin >> x;
cout << "You have entered " << x << endl;
}
I need to revise right from the basics, learning how to do sorts (bubble sorts, insertion sorts, quick sorts, binary search) as well as learning how to use weights, strings etc.
This is one I did quite some time ago, but I didn't revise and completely forgot the reason behind it:
#include <iostream> #include <string> using namespace std;
void main() { char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'}; int weight[8] = { 0,2,7,6,5,4,3,2 }; string nric; int sum=0; // init cumulative sum to zero
// get NRIC input cout << "Enter NRIC: "; getline (cin,nric); cout << nric << endl; // calculate check digit for (int i=1; i<=7; i++) { sum = sum + (nric[i]-'0')*weight[i]; } cout << sum << endl; int checkdigit = 11 - (sum%11); // check if the user's NRIC check digit matches to confirm valid nric if (digit[checkdigit]==nric[8]) cout << "valid NRIC number" << endl; else cout << "Invalid NRIC number" << endl; }
Help would be greatly appreciated!!!
|
without ever doing c++ and knowledge from other languages i would say the first question is int and void are return types you know int returns an integer and void doesnt return anything
|
using int function will give the result as a integer, that is why return 0 is used a lot when using int main(); But using void main() gives no result im pretty sure. That is how i understand it. ( I think im sure)
|
#include <iostream> using namespace std;
void main() { int x; cout << "Please enter an integer." << endl; cin >> x >> endl;
cout << "You have entered "x << endl;
}
you need to put another "<<" before x on the last line. so change the last line to
cout << "You have entered " << x << endl;
|
konadora
Singapore66063 Posts
I don't really quite understand the concept of 'returning an integer', what does it mean?
Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right?
|
konadora
Singapore66063 Posts
Okay I got the short program to work, I foolishly used cin >> x >> endl; instead of cin >> x;
#include <iostream> using namespace std;
void main() { int x; cout << "Please enter an integer." << endl; cin >> x;
cout << "You have entered " << x << endl;
}
|
On June 28 2009 23:42 konadora wrote: I don't really quite understand the concept of 'returning an integer', what does it mean?
Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right?
no. if you make main's return type int, then you can return any integer you want. the point of this is so that a program that calls your main function (such as the program you are using to test it, like a terminal or visual studio) can retrieve the return value. you can use this communication to return error codes... i.e., return 0 if all goes well, or return > 0 if an error occurred.
if you make main void, then it has no return type, and will always return 0 to the caller.
|
konadora
Singapore66063 Posts
Oh I see, so it's just that int main() allows you to identify whether your program has an error or not, but void main() can't?
|
void main() should never be used as the main function should ALWAYS return a value
I thought everyone that?!
|
konadora
Singapore66063 Posts
On June 29 2009 00:01 Revabug wrote: void main() should never be used as the main function should ALWAYS return a value
I thought everyone that?! Would you mind explaining a bit more? I already stated that I'm quite noob with C++
|
When you're doing a small project, as is typical for a lot of uni work, what your main function returns basically won't matter. Albeit obviously it's better practice to use int, and bad habits are bad.
|
never use void main. Its oldschool bad programming.
|
konadora
Singapore66063 Posts
Okay, so I've got that cleared. Now, I need to tackle all these 'floats', 'doubles', 'integers', etc. What are the names of these? Are they the 'return data types'?
|
On June 28 2009 23:36 konadora wrote:Show nested quote +#include <iostream> #include <string> using namespace std;
void main() { char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'}; int weight[8] = { 0,2,7,6,5,4,3,2 }; string nric; int sum=0; // init cumulative sum to zero
// get NRIC input cout << "Enter NRIC: "; getline (cin,nric); cout << nric << endl; // calculate check digit for (int i=1; i<=7; i++) { sum = sum + (nric[i]-'0')*weight[i]; } cout << sum << endl; int checkdigit = 11 - (sum%11); // check if the user's NRIC check digit matches to confirm valid nric if (digit[checkdigit]==nric[8]) cout << "valid NRIC number" << endl; else cout << "Invalid NRIC number" << endl; } char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'}; creates an array of characters with the length of 12, digit[0] == 'x', digit[1] == 'A', ... , digit[11] == 'J' int weight[8] = { 0,2,7,6,5,4,3,2 }; same with integer values cout << "Enter NRIC: "; getline (cin,nric); here you get an input from the user, he types something on his keyboard, and the input is saved in the string (string = array of char) nric. for (int i=1; i<=7; i++) { sum = sum + (nric[i]-'0' )*weight[i]; } this for-loop iterates through the array the user has typed. Its strange, that you dont start with i = 0, since arrays in C, Java and C++ are beginning with the index 0.
|
The only thing int main does is use the int you return as a parameter for ExitProcess (or exit, which is basically a wrapper for ExitProcess). void main will always return 0.
|
konadora
Singapore66063 Posts
On June 29 2009 00:09 treason wrote:Show nested quote +On June 28 2009 23:36 konadora wrote:#include <iostream> #include <string> using namespace std;
void main() { char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'}; int weight[8] = { 0,2,7,6,5,4,3,2 }; string nric; int sum=0; // init cumulative sum to zero
// get NRIC input cout << "Enter NRIC: "; getline (cin,nric); cout << nric << endl; // calculate check digit for (int i=1; i<=7; i++) { sum = sum + (nric[i]-'0')*weight[i]; } cout << sum << endl; int checkdigit = 11 - (sum%11); // check if the user's NRIC check digit matches to confirm valid nric if (digit[checkdigit]==nric[8]) cout << "valid NRIC number" << endl; else cout << "Invalid NRIC number" << endl; } char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'};creates an array of characters with the length of 12, digit[0] == 'x', digit[1] == 'A', ... , digit[11] == 'J' int weight[8] = { 0,2,7,6,5,4,3,2 };same with integer values cout << "Enter NRIC: "; getline (cin,nric);here you get an input from the user, he types something on his keyboard, and the input is saved in the string (string = array of char) nric. for (int i=1; i<=7; i++) { sum = sum + (nric[i]-'0' )*weight[i]; }this for-loop iterates through the array the user has typed. Its strange, that you dont start with i = 0, since arrays in C, Java and C++ are beginning with the index 0. I need help with explanation on the 'arrays' part, I can't find the notes online on my school's e-learning platform. Step-by-step explanation would help so much, I would love you for life
|
On June 28 2009 23:47 AcrossFiveJulys wrote:Show nested quote +On June 28 2009 23:42 konadora wrote: I don't really quite understand the concept of 'returning an integer', what does it mean?
Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right? no. if you make main's return type int, then you can return any integer you want. the point of this is so that a program that calls your main function (such as the program you are using to test it, like a terminal or visual studio) can retrieve the return value. you can use this communication to return error codes... i.e., return 0 if all goes well, or return > 0 if an error occurred. if you make main void, then it has no return type, and will always return 0 to the caller. Just to be clear here, making main type void makes it return *no* value which is not at all the same as returning 0. At a lower level, the return value of a function is usually stored in a register, therefore, if the operating system (which in this case acts as a 'calling function' expects main to be of type int and calls it that way, it will receive an effectively random value in return if it is type void. As you might imagine, thats a bad thing that just happens to turn out okay sometimes. In short, make your main function type int.
|
On June 29 2009 00:15 tec27 wrote:Show nested quote +On June 28 2009 23:47 AcrossFiveJulys wrote:On June 28 2009 23:42 konadora wrote: I don't really quite understand the concept of 'returning an integer', what does it mean?
Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right? no. if you make main's return type int, then you can return any integer you want. the point of this is so that a program that calls your main function (such as the program you are using to test it, like a terminal or visual studio) can retrieve the return value. you can use this communication to return error codes... i.e., return 0 if all goes well, or return > 0 if an error occurred. if you make main void, then it has no return type, and will always return 0 to the caller. Just to be clear here, making main type void makes it return *no* value which is not at all the same as returning 0. At a lower level, the return value of a function is usually stored in a register, therefore, if the operating system (which in this case acts as a 'calling function' expects main to be of type int and calls it that way, it will receive an effectively random value in return if it is type void. As you might imagine, thats a bad thing that just happens to turn out okay sometimes. In short, make your main function type int.
You're wrong, the compiler automatically makes the main function return 0 when it's specified as void. (xor eax, eax before ret)
|
|
On June 29 2009 00:08 konadora wrote: Okay, so I've got that cleared. Now, I need to tackle all these 'floats', 'doubles', 'integers', etc. What are the names of these? Are they the 'return data types'?
you really need to read your textbook. float, double, int, are all variable types. this is a very basic concept, and you won't get far asking such questions here.
|
|
|
|