• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 17:52
CEST 23:52
KST 06:52
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
TL.net Map Contest #21: Voting9[ASL20] Ro4 Preview: Descent11Team TLMC #5: Winners Announced!3[ASL20] Ro8 Preview Pt2: Holding On9Maestros of the Game: Live Finals Preview (RO4)5
Community News
BSL Team A vs Koreans - Sat-Sun 16:00 CET5Weekly Cups (Oct 6-12): Four star herO85.0.15 Patch Balance Hotfix (2025-10-8)80Weekly Cups (Sept 29-Oct 5): MaxPax triples up3PartinG joins SteamerZone, returns to SC2 competition32
StarCraft 2
General
Stellar Fest: StarCraft II returns to Canada The New Patch Killed Mech! herO Talks: Poor Performance at EWC and more... TL.net Map Contest #21: Voting Revisiting the game after10 years and wow it's bad
Tourneys
SC2's Safe House 2 - October 18 & 19 $1,200 WardiTV October (Oct 21st-31st) WardiTV Mondays RSL Offline Finals Dates + Ticket Sales! SC4ALL $6,000 Open LAN in Philadelphia
Strategy
Custom Maps
External Content
Mutation # 495 Rest In Peace Mutation # 494 Unstable Environment Mutation # 493 Quick Killers Mutation # 492 Get Out More
Brood War
General
BSL Team A vs Koreans - Sat-Sun 16:00 CET BW General Discussion Question regarding recent ASL Bisu vs Larva game [Interview] Grrrr... 2024 Pros React To: BarrackS + FlaSh Coaching vs SnOw
Tourneys
[ASL20] Semifinal B SC4ALL $1,500 Open Bracket LAN [Megathread] Daily Proleagues [ASL20] Semifinal A
Strategy
BW - ajfirecracker Strategy & Training Relatively freeroll strategies Current Meta Siegecraft - a new perspective
Other Games
General Games
Stormgate/Frost Giant Megathread Dawn of War IV Path of Exile Nintendo Switch Thread ZeroSpace Megathread
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
SPIRED by.ASL Mafia {211640} TL Mafia Community Thread
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Men's Fashion Thread Sex and weight loss
Fan Clubs
The herO Fan Club! The Happy Fan Club!
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Series you have seen recently... Movie Discussion!
Sports
Formula 1 Discussion 2024 - 2026 Football Thread MLB/Baseball 2023 NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Recent Gifted Posts
Blogs
The Heroism of Pepe the Fro…
Peanutsc
Rocket League: Traits, Abili…
TrAiDoS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1239 users

C++ revision help!

Blogs > konadora
Post a Reply
1 2 3 4 5 6 7 Next All
konadora *
Profile Blog Joined February 2009
Singapore66355 Posts
Last Edited: 2009-06-28 14:47:08
June 28 2009 14:36 GMT
#1
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!!!

POGGERS
Divinek
Profile Blog Joined November 2006
Canada4045 Posts
June 28 2009 14:38 GMT
#2
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
Never attribute to malice that which can be adequately explained by stupidity.
Oh goodness me, FOX tv where do you get your sight? Can't you keep track, the puck is black. That's why the ice is white.
AoN.DimSum
Profile Blog Joined September 2008
United States2983 Posts
June 28 2009 14:39 GMT
#3
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)
by my idol krokkis : "U better hope Finland wont have WCG next year and that I wont gain shitloads of skill, cause then I will wash ur mouth with soap, little man."
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
June 28 2009 14:42 GMT
#4

#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 *
Profile Blog Joined February 2009
Singapore66355 Posts
June 28 2009 14:42 GMT
#5
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?
POGGERS
konadora *
Profile Blog Joined February 2009
Singapore66355 Posts
June 28 2009 14:44 GMT
#6
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;

}
POGGERS
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
June 28 2009 14:47 GMT
#7
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 *
Profile Blog Joined February 2009
Singapore66355 Posts
June 28 2009 14:49 GMT
#8
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?
POGGERS
Revabug
Profile Blog Joined June 2009
United Kingdom147 Posts
June 28 2009 15:01 GMT
#9
void main() should never be used as the main function should ALWAYS return a value

I thought everyone that?!
konadora *
Profile Blog Joined February 2009
Singapore66355 Posts
June 28 2009 15:05 GMT
#10
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++
POGGERS
Tracil
Profile Blog Joined September 2005
Australia505 Posts
Last Edited: 2009-06-28 15:07:37
June 28 2009 15:06 GMT
#11
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.
Shooting
sidesprang
Profile Joined January 2009
Norway1033 Posts
June 28 2009 15:06 GMT
#12
never use void main. Its oldschool bad programming.
Float like a butterfly, sting like a bee.
konadora *
Profile Blog Joined February 2009
Singapore66355 Posts
Last Edited: 2009-06-28 15:09:06
June 28 2009 15:08 GMT
#13
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'?
POGGERS
treason
Profile Joined February 2009
Germany72 Posts
Last Edited: 2009-06-28 15:11:01
June 28 2009 15:09 GMT
#14
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.
Superbia
Profile Blog Joined April 2008
Netherlands8889 Posts
June 28 2009 15:10 GMT
#15
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.
Minimal effort.
konadora *
Profile Blog Joined February 2009
Singapore66355 Posts
June 28 2009 15:13 GMT
#16
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
POGGERS
tec27
Profile Blog Joined June 2004
United States3702 Posts
June 28 2009 15:15 GMT
#17
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.
Can you jam with the console cowboys in cyberspace?
Superbia
Profile Blog Joined April 2008
Netherlands8889 Posts
June 28 2009 15:16 GMT
#18
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)
Minimal effort.
Revabug
Profile Blog Joined June 2009
United Kingdom147 Posts
Last Edited: 2009-06-28 15:20:57
June 28 2009 15:17 GMT
#19
oops
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
June 28 2009 15:20 GMT
#20
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.
1 2 3 4 5 6 7 Next All
Please log in or register to reply.
Live Events Refresh
Safe House 2
17:00
Round Robin
ZombieGrub586
TKL 173
CranKy Ducklings100
CosmosSc2 97
EnkiAlexander 49
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
ZombieGrub586
TKL 173
Nathanias 102
CosmosSc2 97
BRAT_OK 65
UpATreeSC 64
StarCraft: Brood War
Britney 29949
ZZZero.O 202
Dewaltoss 112
Counter-Strike
Stewie2K564
Super Smash Bros
AZ_Axe0
Heroes of the Storm
Khaldor272
Other Games
Grubby2484
summit1g508
Skadoodle442
Pyrionflax239
Mew2King123
Trikslyr33
Organizations
Other Games
gamesdonequick3529
BasetradeTV162
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• musti20045 31
• RyuSc2 31
• HeavenSC 21
• Adnapsc2 15
• IndyKCrew
• AfreecaTV YouTube
• intothetv
• Kozan
• sooper7s
• LaughNgamezSOOP
• Migwel
StarCraft: Brood War
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• Ler101
Other Games
• imaqtpie1659
• WagamamaTV195
Upcoming Events
Sparkling Tuna Cup
12h 9m
Safe House 2
19h 9m
Monday Night Weeklies
1d 18h
WardiTV Invitational
2 days
WardiTV Invitational
2 days
Tenacious Turtle Tussle
4 days
The PondCast
4 days
WardiTV Invitational
5 days
Online Event
5 days
RSL Revival
6 days
[ Show More ]
RSL Revival
6 days
WardiTV Invitational
6 days
Liquipedia Results

Completed

Acropolis #4 - TS2
WardiTV TLMC #15
HCC Europe

Ongoing

BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
C-Race Season 1
IPSL Winter 2025-26
EC S1
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual

Upcoming

SC4ALL: Brood War
BSL Season 21
BSL 21 Team A
BSL 21 Non-Korean Championship
RSL Offline Finals
RSL Revival: Season 3
Stellar Fest
SC4ALL: StarCraft II
CranK Gathers Season 2: SC II Pro Teams
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.