• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 22:58
CEST 04:58
KST 11:58
  • 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
Maestros of the Game: Week 1/Play-in Preview5[ASL20] Ro24 Preview Pt2: Take-Off7[ASL20] Ro24 Preview Pt1: Runway132v2 & SC: Evo Complete: Weekend Double Feature4Team Liquid Map Contest #21 - Presented by Monster Energy9
Community News
Weekly Cups (Aug 18-24): herO dethrones MaxPax6Maestros of The Game—$20k event w/ live finals in Paris43Weekly Cups (Aug 11-17): MaxPax triples again!13Weekly Cups (Aug 4-10): MaxPax wins a triple6SC2's Safe House 2 - October 18 & 195
StarCraft 2
General
2024/25 Off-Season Roster Moves #2: Serral - Greatest Players of All Time #1: Maru - Greatest Players of All Time Maestros of the Game: Week 1/Play-in Preview Greatest Players of All Time: 2025 Update
Tourneys
Maestros of The Game—$20k event w/ live finals in Paris LiuLi Cup - September 2025 Tournaments $5,100+ SEL Season 2 Championship (SC: Evo) Kirktown Chat Brawl #8 - 4.6K max Tonight LiuLi Cup - August 2025 Tournaments
Strategy
Custom Maps
External Content
Mutation # 489 Bannable Offense Mutation # 488 What Goes Around Mutation # 487 Think Fast Mutation # 486 Watch the Skies
Brood War
General
Starcraft at lower levels TvP Post ASL20 Ro24 discussion. BGH Auto Balance -> http://bghmmr.eu/ Easiest luckies way to get out of Asl groups BW General Discussion
Tourneys
[ASL20] Ro24 Group F [IPSL] CSLAN Review and CSLPRO Reimagined! Small VOD Thread 2.0 Cosmonarchy Pro Showmatches
Strategy
Simple Questions, Simple Answers Muta micro map competition Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Nintendo Switch Thread Path of Exile Warcraft III: The Frozen Throne General RTS Discussion Thread Mechabellum
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
Russo-Ukrainian War Thread US Politics Mega-thread YouTube Thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread
Fan Clubs
The Happy Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s) Gtx660 graphics card replacement
TL Community
The Automated Ban List TeamLiquid Team Shirt On Sale
Blogs
hello world
radishsoup
Lemme tell you a thing o…
JoinTheRain
How Culture and Conflict Imp…
TrAiDoS
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
INDEPENDIENTE LA CTM
XenOsky
Customize Sidebar...

Website Feedback

Closed Threads



Active: 598 users

C++ revision help!

Blogs > konadora
Post a Reply
1 2 3 4 5 6 7 Next All
konadora *
Profile Blog Joined February 2009
Singapore66183 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
Singapore66183 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
Singapore66183 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
Singapore66183 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
Singapore66183 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
Singapore66183 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
Singapore66183 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 States3701 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
Next event in 7h 2m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
WinterStarcraft562
PiGStarcraft365
Nina 184
RuFF_SC2 152
ProTech32
StarCraft: Brood War
sSak 392
Rush 113
Sharp 57
Noble 43
NaDa 27
HiyA 8
Icarus 7
Terrorterran 6
Dota 2
monkeys_forever665
LuMiX0
League of Legends
JimRising 761
Counter-Strike
Stewie2K749
Super Smash Bros
AZ_Axe277
Other Games
summit1g10982
tarik_tv10786
shahzam758
Maynarde144
ViBE139
semphis_20
Organizations
Other Games
gamesdonequick1824
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 15 non-featured ]
StarCraft 2
• practicex 8
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• iopq 0
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Rush867
• Lourlo271
Other Games
• Scarra2327
Upcoming Events
Afreeca Starleague
7h 2m
Soulkey vs BeSt
Snow vs Light
Wardi Open
8h 2m
Monday Night Weeklies
13h 2m
Replay Cast
21h 2m
Sparkling Tuna Cup
1d 7h
PiGosaur Monday
1d 21h
LiuLi Cup
2 days
Replay Cast
2 days
The PondCast
3 days
RSL Revival
3 days
Maru vs SHIN
MaNa vs MaxPax
[ Show More ]
RSL Revival
4 days
Reynor vs Astrea
Classic vs sOs
BSL Team Wars
4 days
Team Bonyth vs Team Dewalt
CranKy Ducklings
5 days
RSL Revival
5 days
GuMiho vs Cham
ByuN vs TriGGeR
Cosmonarchy
5 days
TriGGeR vs YoungYakov
YoungYakov vs HonMonO
HonMonO vs TriGGeR
[BSL 2025] Weekly
5 days
RSL Revival
6 days
Cure vs Bunny
Creator vs Zoun
BSL Team Wars
6 days
Team Hawk vs Team Sziky
Liquipedia Results

Completed

Acropolis #4 - TS1
SEL Season 2 Championship
HCC Europe

Ongoing

Copa Latinoamericana 4
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
ASL Season 20
CSL Season 18: Qualifier 2
CSL 2025 AUTUMN (S18)
Maestros of the Game
Sisters' Call Cup
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

LASL Season 20
2025 Chongqing Offline CUP
BSL Season 21
BSL 21 Team A
Chzzk MurlocKing SC1 vs SC2 Cup #2
EC S1
BLAST Rivals Fall 2025
Skyesports Masters 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
MESA Nomadic Masters Fall
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 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.