• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 00:12
CET 06:12
KST 14:12
  • 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
RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10
Community News
Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2BGE Stara Zagora 2026 announced15[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win3RSL Season 3: RO16 results & RO8 bracket13
StarCraft 2
General
Chinese SC2 server to reopen; live all-star event in Hangzhou Maestros of the Game: Live Finals Preview (RO4) BGE Stara Zagora 2026 announced Weekly Cups (Nov 24-30): MaxPax, Clem, herO win SC2 Proleague Discontinued; SKT, KT, SGK, CJ disband
Tourneys
RSL Offline Finals Info - Dec 13 and 14! Sparkling Tuna Cup - Weekly Open Tournament StarCraft Evolution League (SC Evo Biweekly) Sea Duckling Open (Global, Bronze-Diamond) $5,000+ WardiTV 2025 Championship
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 502 Negative Reinforcement Mutation # 501 Price of Progress Mutation # 500 Fright night Mutation # 499 Chilling Adaptation
Brood War
General
Foreign Brood War BW General Discussion MBCGame Torrents [ASL20] Ask the mapmakers — Drop your questions Which season is the best in ASL?
Tourneys
Small VOD Thread 2.0 [Megathread] Daily Proleagues [BSL21] RO16 Group D - Sunday 21:00 CET [BSL21] RO16 Group A - Saturday 21:00 CET
Strategy
Current Meta Game Theory for Starcraft How to stay on top of macro? PvZ map balance
Other Games
General Games
Stormgate/Frost Giant Megathread Path of Exile Nintendo Switch Thread ZeroSpace Megathread The Perfect Game
Dota 2
Official 'what is Dota anymore' discussion
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
Mafia Game Mode Feedback/Ideas TL Mafia Community Thread
Community
General
Russo-Ukrainian War Thread European Politico-economics QA Mega-thread US Politics Mega-thread Things Aren’t Peaceful in Palestine The Big Programming Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
Formula 1 Discussion 2024 - 2026 Football Thread NBA General Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Where to ask questions and add stream? The Automated Ban List
Blogs
I decided to write a webnov…
DjKniteX
Physical Exertion During Gam…
TrAiDoS
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1726 users

C++ revision help!

Blogs > konadora
Post a Reply
1 2 3 4 5 6 7 Next All
konadora *
Profile Blog Joined February 2009
Singapore66357 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
Singapore66357 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
Singapore66357 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
Singapore66357 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
Singapore66357 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
Singapore66357 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
Singapore66357 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
Korean StarCraft League
03:00
Week 83
CranKy Ducklings230
davetesta48
HKG_Chickenman24
Liquipedia
The PiG Daily
21:15
Best Games of SC
Clem vs Solar
Classic vs Cure
Reynor vs Classic
Solar vs Clem
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RuFF_SC2 164
StarCraft: Brood War
Leta 580
PianO 433
Shine 87
ZergMaN 46
Noble 28
Dota 2
monkeys_forever458
NeuroSwarm234
League of Legends
JimRising 653
Reynor37
Super Smash Bros
C9.Mang0545
hungrybox295
Mew2King86
Heroes of the Storm
Khaldor126
Other Games
summit1g9991
ViBE129
WinterStarcraft11
Organizations
Other Games
gamesdonequick1257
StarCraft: Brood War
lovetv 7
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 12 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• RayReign 27
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Lourlo963
Upcoming Events
CranKy Ducklings
4h 48m
WardiTV 2025
6h 48m
SC Evo League
7h 18m
IPSL
11h 48m
Dewalt vs ZZZero
BSL 21
14h 48m
Sziky vs OyAji
Gypsy vs eOnzErG
OSC
16h 48m
Solar vs Creator
ByuN vs Gerald
Percival vs Babymarine
Moja vs Krystianer
EnDerr vs ForJumy
sebesdes vs Nicoract
Sparkling Tuna Cup
1d 4h
WardiTV 2025
1d 6h
OSC
1d 9h
IPSL
1d 11h
Bonyth vs KameZerg
[ Show More ]
BSL 21
1d 14h
Bonyth vs StRyKeR
Tarson vs Dandy
Replay Cast
2 days
Wardi Open
2 days
StarCraft2.fi
2 days
Monday Night Weeklies
2 days
Replay Cast
2 days
WardiTV 2025
3 days
StarCraft2.fi
3 days
PiGosaur Monday
3 days
StarCraft2.fi
4 days
Tenacious Turtle Tussle
4 days
The PondCast
5 days
WardiTV 2025
5 days
StarCraft2.fi
5 days
WardiTV 2025
6 days
Liquipedia Results

Completed

Proleague 2025-11-30
RSL Revival: Season 3
Light HT

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
CSCL: Masked Kings S3
Slon Tour Season 2
Acropolis #4 - TS3
WardiTV 2025
META Madness #9
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
RSL Offline Finals
Kuram Kup
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 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.